예제 #1
0
    /// <summary>
    /// ici applique la force perpendiculaire à la rope, dans le bon sens !
    /// </summary>
    public void ApplyAirMoveForce(Vector3 dir, float force)
    {
        Debug.DrawRay(transform.position, dir, Color.green, 1f);

        //externalForce.LittleForceDir(-dir, force);
        PhysicsExt.ApplyConstForce(rb, -dir, force);
    }
    public static T FindNearestTarget <T>(Vector3 seekerPos, float searchRadius, LayerMask targetLayerMask) where T : Component
    {
        T[] targets = PhysicsExt.OverlapSphere <T>(seekerPos, searchRadius, targetLayerMask);

        if (targets.Length != 0)
        {
            T     nearTarget = null;
            float minDistSqr = float.MaxValue;

            foreach (T item in targets)
            {
                Transform target = item.transform;

                float distSqr = (target.position - seekerPos).sqrMagnitude;
                if (distSqr < minDistSqr)
                {
                    nearTarget = item;
                    minDistSqr = distSqr;
                }
            }
            return(nearTarget);
        }

        return(null);
    }
예제 #3
0
        /// <summary>
        /// Modify each nearInteractable collision with hands attached grabbables.
        /// Only can be collision between attached grabbables and other interactables if HandVR of the attached (the hand) is NOT Blocked (HandVR is Blocked when it isn't visible) and near interactable is visible from the hand
        /// </summary>
        private void SetCollisionIgnores()
        {
            for (int i = 0; i < HandsVR.Length; i++)
            {
                if (HandsVR[i].AttachedGrabbable)
                {
                    int length = nearInteractables.Count;
                    for (int j = 0; j < length; j++)
                    {
                        //Following line seems unneeded. Ignore or not collisions with itself don't affect
                        //if (nearInteractables[j].gameObject == handsVR[i].AttachedGrabbable.gameObject) return;

                        //grabbable is blocked if grabbing hand is blocked or if grabbable is overlapping.
                        //if AttachedGrabbable is blocked or NOTHING -> following is deprecated ------->>>>>>   near interactable is NOT visible from hand, can't be collisions
                        //it's not worth to calculate VisibleFrom with every Collider of the nearInteractables
                        if (HandsVR[i].AttachedGrabbable.Blocked)                        // || !nearInteractables[j].transform.VisibleFrom(HandsVR[i].transform.position, DataVR.Instance.hand.blockingElements, QueryTriggerInteraction.Ignore))
                        {
                            PhysicsExt.IgnoreCollisions(nearInteractables[j].Colliders, HandsVR[i].AttachedGrabbable.Colliders, true);
                        }
                        else
                        {
                            PhysicsExt.IgnoreCollisions(nearInteractables[j].Colliders, HandsVR[i].AttachedGrabbable.Colliders, false);
                        }
                    }
                }
            }
        }
예제 #4
0
        public void Detach(bool fromAttachMethod = false)
        {
            //Debug.Log("Detach: " + this, gameObject);

            //TrackedPoseDriver approach
            {
                DestroyTrackedPoseDriver();
            }

            transform.DOKill();

            //SLOT
            //-------------------------------------------------
            if (!fromAttachMethod)
            {
                TryStore(OwnerHandVR);
            }
            //-------------------------------------------------

            OwnerHandVR.AttachedGrabbable = null;

            //not needed, the BelongState gives if it is attached, not need to check if ownerHandVR is null like before, so ownerHandVR mainstains the last ownerHandVR when state is changed until it is attached again
            //can be useful to return grabbable to last hand
            //OwnerHandVR = null;

            //if it has been attached to a slot
            if (Stored)
            {
                //Events
                OnDetached?.Invoke(OwnerHandVR);

#if UNITY_EDITOR
                Debug.Log("Dettached and Stored Grabbable: " + this + " from HandVR: " + OwnerHandVR, gameObject);
#endif

                return;
            }

            //TrackedPoseDriver approach
            {
                transform.position = /*OwnerHandVR.transform.position*/ OwnerHandVR.PoseProvider.AttachPosition;
                transform.rotation = /*OwnerHandVR.transform.rotation*/ OwnerHandVR.PoseProvider.AttachRotation;
            }

            transform.parent = null;

            AdjustPhysicsWithPhysicsTracker(OwnerHandVR);

            state = BelongState.Free;

            PhysicsExt.IgnoreCollisions(OwnerHandVR.CharacterVR.CharacterController, Colliders, false);                //Undo Ignore Collisions between this and CharacterVR

            //Events
            OnDetached?.Invoke(OwnerHandVR);

#if UNITY_EDITOR
            Debug.Log("Dettached Grabbable: " + this + " from HandVR: " + OwnerHandVR, gameObject);
#endif
        }
예제 #5
0
    /// <summary>
    /// ici tend la corde
    /// </summary>
    public void DoAirTenseRope(float boost = 1)
    {
        //Debug.Log("tend la corde !");
        Vector3 dirRope = ropeHandler.GetVectorFromPlayer(playerController.IdPlayer);

        Debug.DrawRay(transform.position, dirRope, Color.blue, 1f);

        PhysicsExt.ApplyConstForce(rb, dirRope, tenseRopeWhenGripped * boost);
        //externalForce.LittleForceDir(dirRope, tenseRopeWhenGripped * boost);
        //rb.velocity += dirRope     * Physics.gravity.y * (tenseRopeWhenGripped - 1) * Time.fixedDeltaTime;
        //Debug.Log("ici ajout velocity");
    }
예제 #6
0
        /// <summary>
        /// Removing near interactables.
        /// Important to make available collisions between them and attached grabbables again.
        /// </summary>
        /// <param name="interactable"></param>
        public void RemoveNearInteractables(Interactable interactable)
        {
            nearInteractables.Remove(interactable);

            //Reset collisions between attached interactables to hands and leaving interactable
            for (int i = 0; i < HandsVR.Length; i++)
            {
                if (HandsVR[i].AttachedGrabbable)
                {
                    PhysicsExt.IgnoreCollisions(interactable.Colliders, HandsVR[i].AttachedGrabbable.Colliders, false);
                }
            }
        }
예제 #7
0
        /// <summary>
        /// Make interactable be able to collide to nearInteractables.
        /// This is done to a interactable (grabbable) when it is detached.
        /// </summary>
        /// <param name="interactable"></param>
        public void FreeCollisionIgnores(Interactable interactable)
        {
            if (interactable == null)
            {
                return;
            }

            int length = nearInteractables.Count;

            for (int i = 0; i < length; i++)
            {
                PhysicsExt.IgnoreCollisions(nearInteractables[i].Colliders, interactable.Colliders, false);
            }
        }
예제 #8
0
    /// <summary>
    /// Applique la gravité du jump / de base !
    /// </summary>
    private void ApplyGravity()
    {
        CanApplyGravity();

        if (gravityApply[0])
        {
            //Debug.Log("ici ceilling");
            PhysicsExt.ApplyConstForce(rb, -Vector3.up, forceUpWhenCeilling);
        }
        else if (gravityApply[1])
        {
            //Debug.Log("ici gravity");
            rb.velocity += Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.fixedDeltaTime;
        }
        else if (gravityApply[2])
        {
            //Debug.Log("ici low gravity");
            rb.velocity += Vector3.up * Physics.gravity.y * (lowMultiplier - 1) * Time.fixedDeltaTime;
        }
    }
예제 #9
0
        /// <summary>
        /// Shoots a ray into the character that will impact a bone and cause the
        /// bone to react to the force.
        /// </summary>
        /// <param name="rOrigin"></param>
        /// <param name="rDirection"></param>
        /// <param name="rRange"></param>
        /// <param name="rForce"></param>
        public BoneControllerBone RaycastImpact(Vector3 rOrigin, Vector3 rVelocity, float rRange, float rMass, bool rStopIfBlocked, ref Vector3 rHitPoint)
        {
            BoneControllerBone lHitBone = null;

            //float lSpeed = rVelocity.magnitude;
            Vector3 lDirection = rVelocity.normalized;

            // Grab the bones to test
            List <BoneControllerBone> lBones = (_UseAllBones ? mSkeleton.Bones : mBones);

            if (lBones == null || lBones.Count == 0)
            {
                return(null);
            }

            // Start testing the bones
            for (int i = 0; i < lBones.Count; i++)
            {
                Vector3            lHitPoint;
                BoneControllerBone lBone = lBones[i];

                if (lBone.TestRayCollision(rOrigin, lDirection, rRange, out lHitPoint))
                {
                    lHitBone  = lBone;
                    rHitPoint = lHitPoint;

                    if (!AddTemporaryBone(lHitBone))
                    {
                        lHitBone = null;
                    }

                    if (lHitBone != null)
                    {
                        // With the bone hit, we need to apply the impact. This
                        // impact will actually happen over time (hit, react, and recover)
                        float lHitDistance = Vector3.Distance(lHitBone._Transform.position, rHitPoint);
                        //float lHitSpan = lHitDistance / lHitBone.Length;

                        Vector3 lBoneReference = lHitBone._Transform.position + (lHitBone._Transform.rotation * (lHitBone.BoneForward * lHitDistance));

                        PhysicsObject lBonePO = PhysicsObject.Allocate();
                        lBonePO.Mass     = 4.53f; // 4.5359237 kg arm
                        lBonePO.Position = lBoneReference;
                        lBonePO.Velocity = Vector3.zero;

                        PhysicsObject lRayPO = PhysicsObject.Allocate();
                        lRayPO.Mass     = rMass;
                        lRayPO.Position = rHitPoint;
                        lRayPO.Velocity = rVelocity;

                        PhysicsExt.SolveSphericalCollision(ref lBonePO, ref lRayPO, 0.5f);

                        mActiveBoneInfo[lHitBone].State  = 1;
                        mActiveBoneInfo[lHitBone].Time   = _ImpactTime;
                        mActiveBoneInfo[lHitBone].Change = lBonePO.Velocity * _Power;

                        if (lHitBone.Parent != null)
                        {
                            Vector3 lChange = mActiveBoneInfo[lHitBone].Change;
                            ApplyImpactToParent(lHitBone, lChange, _ParentSpread, _ParentDamping);
                        }

                        if (lHitBone.Children != null && lHitBone.Children.Count > 0)
                        {
                            Vector3 lChange = -mActiveBoneInfo[lHitBone].Change;
                            ApplyImpactToChildren(lHitBone, lChange, _ChildSpread, _ChildDamping);
                        }

                        // Release our objects
                        PhysicsObject.Release(lBonePO);
                        PhysicsObject.Release(lRayPO);
                    }
                }
            }

            // Report the bone that was hit
            return(lHitBone);
        }
예제 #10
0
        public void Attach(HandVR handVR, bool iterateAttachmentPoint = false)
        {
            //Debug.Log("Attach: " + this, gameObject);

            // TO DO: Test if not necessary I did it because maybe detach events are important. But attach should let things as they should be.
            if (Attached)
            {
                //True to avoid possible attach to Slot. Imagine you get from one hand to the other but the from hand is hovering a slot. then the detach will attach grabbable to the slot and then attach to the new hand making a strange effect
                Detach(true);
            }
            else if (Stored)
            {
                TryUnstore(OwnerSlot);
            }

            if (iterateAttachmentPoint)
            {
                currentAttachmentPointIndex++;
            }
            if (currentAttachmentPointIndex == attachmentPoints.Count)
            {
                currentAttachmentPointIndex = 0;
            }

            transform.DOKill();
            transform.parent         = handVR.transform;
            OwnerHandVR              = handVR; //Important to do this and next line before real physic attach (position and rotation) to be able to detach from it if other hand steal it on the fly to the hand
            handVR.AttachedGrabbable = this;
            DisableColliders();                //Disable collider to avoid not desired collisions on attach

            Vector3    attachPositionOffset = Vector3.zero;
            Quaternion attachRotationOffset = Quaternion.identity;

            GetLocalAttachmentPositionAndRotation(handVR.transform, out attachPositionOffset, out attachRotationOffset, handVR.AttachmentPointName.name, iterateAttachmentPoint);

            float attachTime = DataVR.Instance.grabbable.attachTime;

            transform.DOLocalMove(-attachPositionOffset, attachTime).SetEase(Ease.OutSine);
            transform.DOLocalRotate(-attachRotationOffset.eulerAngles, attachTime).SetEase(Ease.OutSine).OnComplete
            (
                () =>
            {
                PhysicsExt.IgnoreCollisions(OwnerHandVR.CharacterVR.CharacterController, Colliders); //Ignore Collisions between this and CharacterVR
                EnableColliders();                                                                   //Enable collider (disabled above, before tween attach) WARNING. it is detected by ontriggerenter of physicsproximityAdjust

                //TrackedPoseDriver approach
                {
                    ////Set final position
                    ////-------------------------------------------------
                    //if (attachmentPoints.Count == 0 || (handVR.AttachmentPointName.name == "" && iterateAttachmentPoint == false))
                    //{
                    //	transform.localPosition = Vector3.zero;
                    //	transform.localRotation = Quaternion.identity;
                    //}
                    //else
                    //{
                    //	transform.localPosition = -attachPositionOffset;
                    //	transform.localRotation = Quaternion.Inverse(attachRotationOffset);
                    //}
                    ////-------------------------------------------------
                }

                //SLOT: as it could be in a slot make it kinematic again
                Rigidbody.interpolation          = RigidbodyInterpolation.None;
                Rigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousSpeculative;
                //TrackedPoseDriver approach
                {
                    //Original approach
                    //Rigidbody.isKinematic = true;

                    AddTrackedPoseDriver(handVR, attachPositionOffset, attachRotationOffset);
                }
                Rigidbody.useGravity = false;

                state = BelongState.Attached;

                //Events
                OnAttached?.Invoke(handVR);

#if UNITY_EDITOR
                Debug.Log("Attach Grabbable: " + this + " to HandVR: " + handVR, gameObject);
#endif
            }
            );
        }