protected virtual void simulateFrame(Frame frame)
        {
            _activityManager.UpdateState(frame);

            var active = _activityManager.ActiveBehaviours;

            for (int i = 0; i < active.Count; i++)
            {
                active[i].NotifyPreSolve();
            }

            dispatchOnHandsHoldingAll(frame, isPhysics: true);

            //Apply soft contacts from both hands in unified solve
            //(this will clear softContacts and originalVelocities as well)
            if (softContacts.Count > 0)
            {
                using (new ProfilerSample("Apply Soft Contacts", this)) {
                    PhysicsUtility.applySoftContacts(softContacts, originalVelocities);
                }
            }

            updateInteractionStateChanges(frame);


            for (int i = 0; i < active.Count; i++)
            {
                active[i].NotifyPostSolve();
            }
        }
        void FixedUpdate()
        {
            OnPrePhysicalUpdate();

            refreshInteractionControllers();

      #if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                return;
            }
      #endif

            using (new ProfilerSample("Interaction Manager FixedUpdate", this.gameObject)) {
                // Ensure scale information is up-to-date.
                _scale = this.transform.lossyScale.x;

                // Update each interaction controller (Leap hands or supported VR controllers).
                fixedUpdateInteractionControllers();

                // Perform each interaction object's FixedUpdateObject.
                using (new ProfilerSample("FixedUpdateObject per-InteractionBehaviour")) {
                    foreach (var interactionObj in _interactionObjects)
                    {
                        interactionObj.FixedUpdateObject();
                    }
                }

                // Apply soft contacts from all controllers in a unified solve.
                // (This will clear softContacts and originalVelocities as well.)
                using (new ProfilerSample("Apply Soft Contacts")) {
                    if (_softContacts.Count > 0)
                    {
                        PhysicsUtility.applySoftContacts(_softContacts, _softContactOriginalVelocities);
                    }
                }
            }

            OnPostPhysicalUpdate();

            updateMovingFrameOfReferenceSupport();
        }
Example #3
0
        void FixedUpdate()
        {
            OnPrePhysicalUpdate();

            // Physics should only be synced once at the beginning of the physics simulation.
            // (Will be re-set to its original value at the end of the update.)
      #if UNITY_2017_2_OR_NEWER
            var preUpdateAutoSyncTransforms = Physics.autoSyncTransforms;
            Physics.autoSyncTransforms = false;
      #endif
            try {
                refreshInteractionControllers();

        #if UNITY_EDITOR
                if (!Application.isPlaying)
                {
                    return;
                }
        #endif

                using (new ProfilerSample("Interaction Manager FixedUpdate", this.gameObject)) {
                    // Ensure scale information is up-to-date.
                    _scale = this.transform.lossyScale.x;

                    // Update each interaction controller (Leap hands or supported VR controllers).
                    fixedUpdateInteractionControllers();

                    // Perform each interaction object's FixedUpdateObject.
                    using (new ProfilerSample("FixedUpdateObject per-InteractionBehaviour")) {
                        foreach (var interactionObj in _interactionObjects)
                        {
                            interactionObj.FixedUpdateObject();
                        }
                    }

                    // Apply soft contacts from all controllers in a unified solve.
                    // (This will clear softContacts and originalVelocities as well.)
                    using (new ProfilerSample("Apply Soft Contacts")) {
                        if (_drawControllerRuntimeGizmos)
                        {
                            _softContactsToDraw = new List <PhysicsUtility.SoftContact>(_softContacts);
                        }
                        if (_softContacts.Count > 0)
                        {
                            PhysicsUtility.applySoftContacts(_softContacts, _softContactOriginalVelocities);
                        }
                    }
                }

                OnPostPhysicalUpdate();

                updateMovingFrameOfReferenceSupport();

                if (autoGenerateLayers)
                {
                    autoUpdateContactBoneLayerCollision();
                }
            }
            finally {
        #if UNITY_2017_2_OR_NEWER
                // Restore the autoSyncTransforms setting to whatever the user had it as before
                // the Manager FixedUpdate.
                Physics.autoSyncTransforms = preUpdateAutoSyncTransforms;
        #endif
            }
        }
Example #4
0
        /** Updates this hand model. */
        public override void UpdateHand()
        {
#if UNITY_EDITOR
            if (!EditorApplication.isPlaying)
            {
                return;
            }
#endif
            using (new ProfilerSample("Update InteractionBrushHand", this)) {
                using (new ProfilerSample("Update InteractionBrushBones", this)) {
                    float deadzone = DEAD_ZONE_FRACTION * _hand.Fingers[1].Bone((Bone.BoneType) 1).Width;
                    for (int fingerIndex = 0; fingerIndex < N_FINGERS; fingerIndex++)
                    {
                        for (int jointIndex = 0; jointIndex < N_ACTIVE_BONES; jointIndex++)
                        {
                            Bone bone           = _hand.Fingers[fingerIndex].Bone((Bone.BoneType)(jointIndex) + 1);
                            int  boneArrayIndex = fingerIndex * N_ACTIVE_BONES + jointIndex;
                            UpdateBone(bone, boneArrayIndex, deadzone);
                        }
                    }

                    //Update Palm
                    UpdateBone(_hand.Fingers[(int)Finger.FingerType.TYPE_MIDDLE].Bone(Bone.BoneType.TYPE_METACARPAL), N_FINGERS * N_ACTIVE_BONES, deadzone);
                }

                if (_softContactEnabled)
                {
                    //SOFT CONTACT COLLISIONS
                    using (new ProfilerSample("Update Soft Contact", this)) {
                        //Generate Contacts
                        bool softlyContacting = false;
                        using (new ProfilerSample("Generate Soft Contacts", this)) {
                            for (int fingerIndex = 0; fingerIndex < 5; fingerIndex++)
                            {
                                for (int jointIndex = 0; jointIndex < 4; jointIndex++)
                                {
                                    Bone    bone           = _hand.Fingers[fingerIndex].Bone((Bone.BoneType)(jointIndex));
                                    int     boneArrayIndex = fingerIndex * 4 + jointIndex;
                                    Vector3 boneCenter     = bone.Center.ToVector3();

                                    ////Generate and Fill softContacts with SoftContacts that are intersecting a sphere at boneCenter, with radius softContactBoneRadius
                                    bool sphereIntersecting;
                                    using (new ProfilerSample("Generate Soft Contact", this)) {
                                        if (_manager != null)
                                        {
                                            sphereIntersecting = PhysicsUtility.generateSphereContacts(boneCenter, softContactBoneRadius, (boneCenter - previousBoneCenters[boneArrayIndex]) / Time.fixedDeltaTime,
                                                                                                       1 << _manager.InteractionLayer, ref _manager.softContacts, ref _manager.originalVelocities, ref tempColliderArray);
                                        }
                                        else
                                        {
                                            sphereIntersecting = PhysicsUtility.generateSphereContacts(boneCenter, softContactBoneRadius, (boneCenter - previousBoneCenters[boneArrayIndex]) / Time.fixedDeltaTime,
                                                                                                       ~(1 << 2), ref softContacts, ref originalVelocities, ref tempColliderArray);
                                        }
                                    }
                                    softlyContacting = sphereIntersecting ? true : softlyContacting;
                                }
                            }
                        }


                        if (softlyContacting)
                        {
                            //(If we have a manager, let it handle resolving the contacts of both hands in one unified solve)
                            if (_manager == null)
                            {
                                using (new ProfilerSample("Apply Per Hand Soft Contacts", this)) {
                                    PhysicsUtility.applySoftContacts(softContacts, originalVelocities);
                                }
                            }
                            disableSoftContactEnqueued = false;
                        }
                        else
                        {
                            //If there are no detected Contacts, exit soft contact mode
                            disableSoftContact();
                        }
                    }
                }

                //Update the last positions of the bones with this frame
                for (int fingerIndex = 0; fingerIndex < 5; fingerIndex++)
                {
                    for (int jointIndex = 0; jointIndex < 4; jointIndex++)
                    {
                        Bone bone           = _hand.Fingers[fingerIndex].Bone((Bone.BoneType)(jointIndex));
                        int  boneArrayIndex = fingerIndex * 4 + jointIndex;
                        previousBoneCenters[boneArrayIndex] = bone.Center.ToVector3();
                    }
                }
            }
        }