static Bounds GetBoxBounds(Ragdoll.Element bone, Ragdoll.Element topCutoff, Ragdoll.Element[] encapsulate, bool adjustMin)
        {
            Bounds bounds = new Bounds();

            //encapsulate upper arms and upper legs positions
            for (int i = 0; i < 4; i++)
            {
                bounds.Encapsulate(bone.transform.InverseTransformPoint(encapsulate[i].transform.position));
            }

            if (adjustMin)
            {
                Vector3 min = bounds.min;
                min.y      = 0;
                bounds.min = min;
            }

            //adjust max bounds based on next bone
            Vector3 max = bounds.max;

            max.y = bone.transform.InverseTransformPoint(topCutoff.transform.position).y;

            bounds.max = max;
            return(bounds);
        }
        static Collider AddBoxCollider(Ragdoll.Element bone, Bounds bounds)
        {
            BoxCollider box = bone.AddComponent <BoxCollider>();

            box.center = bounds.center;
            box.size   = bounds.size;
            return(box);
        }
 static void AddBreastColliders(Dictionary <HumanBodyBones, Ragdoll.Element> bones)
 {
     Ragdoll.Element[] encapsulate = new Ragdoll.Element[] {
         bones[HumanBodyBones.LeftUpperArm], bones[HumanBodyBones.RightUpperArm],
         bones[HumanBodyBones.LeftUpperLeg], bones[HumanBodyBones.RightUpperLeg],
     };
     Ragdoll.Element hips  = bones[HumanBodyBones.Hips];
     Ragdoll.Element chest = bones[HumanBodyBones.Chest];
     Ragdoll.Element head  = bones[HumanBodyBones.Head];
     hips.collider  = AddBoxCollider(hips, GetBoxBounds(hips, chest, encapsulate, false));
     chest.collider = AddBoxCollider(chest, GetBoxBounds(chest, head, encapsulate, true));
 }
Exemple #4
0
        void InitializeVelocitySetValues()
        {
            animationVelocityTrackers = new VelocityTracker[Ragdoll.bonesCount];

            for (int i = 0; i < Ragdoll.bonesCount; i++)
            {
                Ragdoll.Element bone = ragdoll.GetBone(Ragdoll.humanBones[i]);

                //track position (offset by ragdoll bone's rigidbody centor of mass) of the follow target
                Vector3 massCenterOffset = bone.transform.InverseTransformPoint(bone.rigidbody.worldCenterOfMass);
                animationVelocityTrackers[i] = new VelocityTracker(bone.followTarget.transform, massCenterOffset);
            }
        }
        static void BuildCapsules(Dictionary <HumanBodyBones, Ragdoll.Element> bones)
        {
            foreach (var k in capsuleBones)
            {
                Ragdoll.Element bone = bones[k];

                int direction = k.ToString().Contains("Arm") ? 0 : 1;

                float distance;
                if (upperCapsuleBones.Contains(k))
                {
                    distance = bone.transform.InverseTransformPoint(bones[GetChildBone(k)].transform.position)[direction];
                }
                else
                {
                    Vector3 endPoint = (bone.transform.position - bones[GetParentBone(k)].transform.position) + bone.transform.position;
                    distance = bone.transform.InverseTransformPoint(endPoint)[direction];

                    if (bone.transform.GetComponentsInChildren(typeof(Transform)).Length > 1)
                    {
                        Bounds bounds = new Bounds();
                        foreach (Transform child in bone.transform.GetComponentsInChildren(typeof(Transform)))
                        {
                            bounds.Encapsulate(bone.transform.InverseTransformPoint(child.position));
                        }

                        if (distance > 0)
                        {
                            distance = bounds.max[direction];
                        }
                        else
                        {
                            distance = bounds.min[direction];
                        }
                    }
                }

                CapsuleCollider collider = bone.AddComponent <CapsuleCollider>();
                collider.direction = direction;

                Vector3 center = Vector3.zero;
                center[direction] = distance * 0.5F;
                collider.center   = center;

                collider.height = Mathf.Abs(distance);
                bone.collider   = collider;
            }
        }
        static void BuildJoints(Dictionary <HumanBodyBones, Ragdoll.Element> bones)
        {
            foreach (var k in bones.Keys)
            {
                if (k == HumanBodyBones.Hips)
                {
                    continue;
                }

                Ragdoll.Element bone = bones[k];

                bone.joint = bone.AddComponent <ConfigurableJoint>();

                // Setup connection and axis

                //bone.joint.autoConfigureConnectedAnchor = false;

                // turn off to handle degenerated scenarios, like spawning inside geometry.
                bone.joint.enablePreprocessing = false;

                bone.joint.anchor        = Vector3.zero;
                bone.joint.connectedBody = bones[GetParentBone(k)].rigidbody;

                // Setup limits
                SoftJointLimit limit = new SoftJointLimit();
                limit.contactDistance = 0; // default to zero, which automatically sets contact distance.
                limit.limit           = 0;

                bone.joint.lowAngularXLimit = bone.joint.highAngularXLimit = bone.joint.angularYLimit = bone.joint.angularZLimit = limit;

                bone.joint.xMotion        = bone.joint.yMotion = bone.joint.zMotion = ConfigurableJointMotion.Locked;
                bone.joint.angularXMotion = bone.joint.angularYMotion = bone.joint.angularZMotion = ConfigurableJointMotion.Limited;

                bone.joint.rotationDriveMode = RotationDriveMode.Slerp;

                bone.joint.projectionMode = JointProjectionMode.PositionAndRotation;
            }
        }
Exemple #7
0
        /*
         *      Set the bone's configurable joint target to the master local rotation
         */
        void HandleJointFollow(Ragdoll.Element bone, float torque, int boneIndex)
        {
            if (!bone.joint)
            {
                return;
            }

            //setting joint torque every frame was slow, so check here if its changed
            if (torque != lastTorques[boneIndex])
            {
                lastTorques[boneIndex] = torque;

                jointDrive.positionSpring = torque;
                bone.joint.slerpDrive     = jointDrive;
            }

            //set joints target rotation
            if (torque != 0)
            {
                Quaternion targetLocalRotation = bone.followTarget.transform.localRotation;
                bone.joint.targetRotation = localToJointSpaces[boneIndex] * Quaternion.Inverse(targetLocalRotation) * startLocalRotations[boneIndex];
            }
        }
Exemple #8
0
        /*
         *      set the velocities we calculated in late update for each animated bone
         *      on the actual ragdoll
         */
        void SetPhysicsVelocities(float deltaTime)
        {
            float dot = Vector3.Dot(ragdoll.RootBone().transform.up, Vector3.up);

            float maxVelocityForGravityAdd2 = profile.maxGravityAddVelocity * profile.maxGravityAddVelocity;

            float fallDecayCurveSample = 1f - fallDecay;             //set up curves backwards... whoops

            // for each physics bone...
            for (int i = 0; i < Ragdoll.bonesCount; i++)
            {
                HumanBodyBones unityBone = Ragdoll.humanBones[i];

                Ragdoll.Element bone = ragdoll.GetBone(unityBone);

                Vector3 ragdollBoneVelocty = bone.rigidbody.velocity;

                // get the manually set bone decay value
                float boneDecay = boneDecays[unityBone];

                /*
                 *      calculate the force decay based on the overall fall decay and the bone profile's
                 *      fall force decay curve
                 */
                float forceDecay = Mathf.Clamp01(profile.bones[i].fallForceDecay.Evaluate(fallDecayCurveSample));

                //subtract manual decay
                forceDecay = Mathf.Clamp01(forceDecay - boneDecay);

                // if we're flipped to extremely, stop trying to follow anim
                // makes it look like it's 'gliding' forward in superman stance
                if (dot < profile.loseFollowDot)
                {
                    forceDecay = 0;
                }

                // if we're still using some force to follow
                if (forceDecay != 0)
                {
                    Vector3 animVelocity = animationVelocityTrackers[i].velocity;

                    /*
                     *      if animation velocity is below threshold magnitude, add some gravity to it
                     */
                    if (animVelocity.sqrMagnitude < maxVelocityForGravityAdd2)
                    {
                        animVelocity.y = Physics.gravity.y * deltaTime;
                    }

                    /*
                     *      if bone decay was manually set to make room for external velocities,
                     *      use the most extreme component of each vector as the "target animated" velocity
                     */
                    if (boneDecay != 0)
                    {
                        animVelocity = MaxAbs(ragdollBoneVelocty, animVelocity);
                    }

                    // set the velocity on the ragdoll rigidbody (based on the force decay)
                    bone.rigidbody.velocity = Vector3.Lerp(ragdollBoneVelocty, animVelocity, forceDecay);
                }

                if (i != 0)
                {
                    /*
                     *      calculate the force decay based on the overall fall decay and the bone profile's
                     *      fall force decay curve
                     */
                    float torqueDecay = Mathf.Clamp01(profile.bones[i].fallTorqueDecay.Evaluate(fallDecayCurveSample));

                    //subtract manual decay
                    torqueDecay = Mathf.Clamp01(torqueDecay - boneDecay);

                    /*
                     *      handle joints target for the ragdoll joints
                     */
                    HandleJointFollow(bone, profile.maxTorque * torqueDecay, i);
                }
            }

            CheckForFallEnd(fallDecay);
        }
        /*
         *  call to get the ragdoll bone references
         *
         *  animator component must be humanoid
         *
         *  set 'addComponents' to true to add the ragdoll components
         *
         *  if non physics is set to true, we just build the references but dont check for (or add)
         *  Rigidbodies, joints, and colliders...
         */
        //public static bool BuildRagdoll (Animator animator, RagdollProfile profile, bool addComponents, bool nonPhysics, out float initialHeadOffsetFromChest, out Ragdoll.Element[] allBones, out Dictionary<HumanBodyBones, Ragdoll.Element> physicsBones) {
        public static bool BuildRagdollElements(Animator animator, out Ragdoll.Element[] allElements, out Dictionary <HumanBodyBones, Ragdoll.Element> boneElements)
        {
            // initialHeadOffsetFromChest = -1;
            allElements  = null;
            boneElements = null;

            //check for null animator
            if (animator == null)
            {
                Debug.LogError("No animator found...(BuildRagdollFromPrebuilt");
                return(false);// null;
            }

            List <Ragdoll.Element> allBonesList = new List <Ragdoll.Element>();

            // instance ids of the bone transforms so we dont re-add them when checking all children bones below
            HashSet <int> usedPhysicsTransforms = new HashSet <int>();

            //build bones list that use physics
            boneElements = new Dictionary <HumanBodyBones, Ragdoll.Element>();

            for (int i = 0; i < Ragdoll.bonesCount; i++)
            {
                HumanBodyBones humanBodyBone = Ragdoll.humanBones[i];

                Transform boneT = animator.GetBoneTransform(humanBodyBone);

                if (boneT == null)
                {
                    Debug.LogError("Cant find physics bone: " + humanBodyBone + " on ragdoll: " + animator.name);
                    boneElements          = null;
                    usedPhysicsTransforms = null;
                    return(false);
                }

                usedPhysicsTransforms.Add(boneT.GetInstanceID());

                Ragdoll.Element ragdollBone = new Ragdoll.Element(boneT, false, true, i == 0);

                boneElements.Add(humanBodyBone, ragdollBone);
                allBonesList.Add(ragdollBone);
            }

            //build other non physics bones

            //get all transform children of the hip bone
            Transform[] allChildren = allBonesList[0].transform.GetComponentsInChildren <Transform>();

            for (int i = 0; i < allChildren.Length; i++)
            {
                Transform child = allChildren[i];
                //if its not a physics bone
                if (!usedPhysicsTransforms.Contains(child.GetInstanceID()))
                {
                    bool isPhysicsParent = child.GetComponentInChildren <Rigidbody>() != null;
                    allBonesList.Add(new Ragdoll.Element(child, isPhysicsParent, false, false));
                }
            }
            allElements = allBonesList.ToArray();

            // if (!nonPhysics) {

            //     if (addComponents) {

            //         EraseRagdoll(animator);

            //         //add capsules
            //         BuildCapsules(physicsBones);
            //         AddBreastColliders(physicsBones);
            //         AddHeadCollider(physicsBones);

            //         //add rigidbodies
            //         BuildRigidodies(physicsBones);

            //         //add joints
            //         BuildJoints(physicsBones);

            //         //add bone components
            //         BuildBones(physicsBones);
            //     }

            //     //initial head position from chest (used for resizing chest collider based on head offset)
            //     initialHeadOffsetFromChest = physicsBones[HumanBodyBones.Chest].transform.InverseTransformPoint(physicsBones[HumanBodyBones.Head].transform.position).y;

            //     // update the ragdoll to reflect it's profile values
            //     Ragdoll.UpdateBonesToProfileValues(physicsBones, profile, initialHeadOffsetFromChest);
            // }
            return(true);
        }