Пример #1
0
            public void Update(IKSolverFullBodyBiped solver, float deltaTime)
            {
                float velocity = (solver.GetRoot().position - m_LastPos).magnitude / deltaTime;

                m_LastPos = solver.GetRoot().position;
                foreach (InertiaBody body in m_Bodies)
                {
                    body.Update(solver, m_Weight * m_FadeWeight, deltaTime, velocity);
                }
            }
Пример #2
0
            // Update this body, apply the offset to the effector
            public void Update(IKSolverFullBodyBiped solver, float m_Weight, float deltaTime)
            {
                m_Velocity = (solver.GetRoot().position - m_LastPosition) / deltaTime;

                // Apply position offset to the effector
                foreach (EffectorLink effectorLink in m_EffectorLinks)
                {
                    effectorLink.Apply(solver, m_Weight, m_Velocity + solver.GetRoot().forward *m_SubForward, deltaTime);
                }

                m_LastPosition = solver.GetRoot().position;
            }
Пример #3
0
                public void Apply(IKSolverFullBodyBiped solver, float m_Weight, Vector3 curentVelocity, float deltaTime)
                {
                    //Update forward tilt
                    Vector3 forwardPro     = Vector3.Project(curentVelocity, solver.GetRoot().forward);
                    float   curProVelocity = forwardPro.magnitude;

                    if (Vector3.Angle(forwardPro, solver.GetRoot().forward) > 150)
                    {
                        curProVelocity *= -1;
                    }
                    m_ProVelocity = Mathf.Lerp(m_ProVelocity, curProVelocity, m_TileAcc * deltaTime);

                    //Apply offset
                    solver.GetEffector(m_Effector).positionOffset += solver.GetRoot().TransformDirection(m_Offset * m_TileWeight.Evaluate(m_ProVelocity)) * m_Weight;
                }
Пример #4
0
            public void Update(IKSolverFullBodyBiped solver, float w, float deltaTime)
            {
                if (this.transform == null || this.relativeTo == null)
                {
                    return;
                }
                Vector3 a = this.relativeTo.InverseTransformDirection(this.transform.position - this.relativeTo.position);

                if (this.firstUpdate)
                {
                    this.lastRelativePos = a;
                    this.firstUpdate     = false;
                }
                Vector3 vector = (a - this.lastRelativePos) / deltaTime;

                this.smoothDelta = ((this.speed > 0f) ? Vector3.Lerp(this.smoothDelta, vector, deltaTime * this.speed) : vector);
                Vector3 v  = this.relativeTo.TransformDirection(this.smoothDelta);
                Vector3 a2 = V3Tools.ExtractVertical(v, solver.GetRoot().up, this.verticalWeight) + V3Tools.ExtractHorizontal(v, solver.GetRoot().up, this.horizontalWeight);

                for (int i = 0; i < this.effectorLinks.Length; i++)
                {
                    solver.GetEffector(this.effectorLinks[i].effector).positionOffset += a2 * w * this.effectorLinks[i].weight;
                }
                this.lastRelativePos = a;
            }
Пример #5
0
 public void OnModifyOffset(IKSolverFullBodyBiped solver, float weight, float deltaTime)
 {
     if (null == solver)
     {
         m_RemainingTime = 0;
         return;
     }
     if (m_RemainingTime > 0f)
     {
         float   elapseTime      = m_StepTime - m_RemainingTime;
         float   crossFadeWeight = Mathf.Clamp01(elapseTime / m_FadeTime);
         Vector3 deltaDir        = solver.GetRoot().position - m_StartPos;
         foreach (EffectortLink effect in m_Effects)
         {
             effect.OnModifyOffset(solver, m_EffectDir, elapseTime / m_StepTime, weight * crossFadeWeight * m_Weight, deltaDir);
         }
         m_RemainingTime -= deltaTime;
     }
             #if UNITY_EDITOR
     if (m_Test)
     {
         m_Test = false;
         DoEffect(solver, m_EffectDir, m_Weight);
     }
             #endif
 }
Пример #6
0
 public void Apply(IKSolverFullBodyBiped solver, float weight)
 {
     for (int i = 0; i < this.effectorLinks.Length; i++)
     {
         this.effectorLinks[i].Apply(solver, weight, solver.GetRoot().rotation);
     }
 }
Пример #7
0
            public float m_SubForward;                //Addition forward state. 1: forward -1:backward

            // Reset to Transform
            public void Reset(IKSolverFullBodyBiped solver)
            {
                m_LastPosition = solver.GetRoot().position;
                foreach (EffectorLink effectorLink in m_EffectorLinks)
                {
                    effectorLink.Reset();
                }
            }
Пример #8
0
            public void Apply(IKSolverFullBodyBiped solver, float weight, Quaternion rotation)
            {
                solver.GetEffector(this.effector).positionOffset += rotation * this.offset * weight;
                Vector3 a       = solver.GetRoot().position + rotation * this.pin;
                Vector3 vector  = a - solver.GetEffector(this.effector).bone.position;
                Vector3 vector2 = this.pinWeight * Mathf.Abs(weight);

                solver.GetEffector(this.effector).positionOffset = new Vector3(Mathf.Lerp(solver.GetEffector(this.effector).positionOffset.x, vector.x, vector2.x), Mathf.Lerp(solver.GetEffector(this.effector).positionOffset.y, vector.y, vector2.y), Mathf.Lerp(solver.GetEffector(this.effector).positionOffset.z, vector.z, vector2.z));
            }
Пример #9
0
            public Vector3 pinWeight;              // Pin weight vector

            // Apply positionOffset to the effector
            public void Apply(IKSolverFullBodyBiped solver, float weight)
            {
                // Offset
                solver.GetEffector(effector).positionOffset += solver.GetRoot().rotation *offset *weight;

                // Calculating pinned position
                Vector3 pinPosition       = solver.GetRoot().position + solver.GetRoot().rotation *pin;
                Vector3 pinPositionOffset = pinPosition - solver.GetEffector(effector).bone.position;

                Vector3 pinWeightVector = pinWeight * Mathf.Abs(weight);

                // Lerping to pinned position
                solver.GetEffector(effector).positionOffset = new Vector3(
                    Mathf.Lerp(solver.GetEffector(effector).positionOffset.x, pinPositionOffset.x, pinWeightVector.x),
                    Mathf.Lerp(solver.GetEffector(effector).positionOffset.y, pinPositionOffset.y, pinWeightVector.y),
                    Mathf.Lerp(solver.GetEffector(effector).positionOffset.z, pinPositionOffset.z, pinWeightVector.z)
                    );
            }
Пример #10
0
                // Update and Apply the position offset to the effector
                public void Update(IKSolverFullBodyBiped solver, Vector3 offsetTarget, float weight)
                {
                    // Lerping offset to the offset target
                    offset = Vector3.Lerp(offset, offsetTarget * multiplier, Time.deltaTime * speed);

                    // Apply gravity
                    Vector3 g = (solver.GetRoot().up *gravity) * offset.magnitude;

                    // Apply the offset to the effector
                    solver.GetEffector(effector).positionOffset += (offset * weight * this.weight) + (g * weight);
                }
Пример #11
0
 public void DoEffect(IKSolverFullBodyBiped solver, Vector3 dir, float weight)
 {
     if (null == solver)
     {
         return;
     }
     m_EffectDir = dir;
     m_Weight    = weight;
     m_StartPos  = solver.GetRoot().position;
     //m_DeltaDir = Vector3.zero;
     m_RemainingTime = m_StepTime;
 }
Пример #12
0
            public void Update(IKSolverFullBodyBiped solver, float m_Weight, float deltaTime)
            {
                // Calculate the angular delta in character rotation
                Quaternion change     = Quaternion.FromToRotation(m_LastForward, solver.GetRoot().forward);
                float      deltaAngle = 0;
                Vector3    axis       = Vector3.zero;

                change.ToAngleAxis(out deltaAngle, out axis);
                if (axis.y > 0)
                {
                    deltaAngle = -deltaAngle;
                }

                deltaAngle *= m_TiltSensitivity * 0.01f;
                deltaAngle /= deltaTime;
                deltaAngle  = Mathf.Clamp(deltaAngle, -1f, 1f);

                m_TiltAngle = Mathf.Lerp(m_TiltAngle, deltaAngle, deltaTime * m_TiltSpeed);

                // Applying positionOffsets
                float tiltF = Mathf.Abs(m_TiltAngle) / 1f * m_Weight + Mathf.Abs(testAngle);

                if (m_TiltAngle - testAngle < 0)
                {
                    foreach (EffectorLink effectorLink in m_TurnRightEffectorLinks)
                    {
                        effectorLink.Apply(solver, tiltF, solver.GetRoot().rotation);
                    }
                }
                else
                {
                    foreach (EffectorLink effectorLink in m_TurnLeftEffectorLinks)
                    {
                        effectorLink.Apply(solver, tiltF, solver.GetRoot().rotation);
                    }
                }

                // Store current character forward axis and Time
                m_LastForward = solver.GetRoot().forward;
            }
Пример #13
0
            // Calculate offset, apply to FBBIK effectors
            protected override void OnApply(IKSolverFullBodyBiped solver, float weight)
            {
                Vector3 up = solver.GetRoot().up *force.magnitude;

                Vector3 offset = (offsetInForceDirection.Evaluate(timer) * force) + (offsetInUpDirection.Evaluate(timer) * up);

                offset *= weight;

                foreach (EffectorLink e in effectorLinks)
                {
                    e.Apply(solver, offset, crossFader);
                }
            }
Пример #14
0
            protected override void OnApply(IKSolverFullBodyBiped solver, float weight)
            {
                Vector3 a      = solver.GetRoot().up *base.force.magnitude;
                Vector3 vector = this.offsetInForceDirection.Evaluate(base.timer) * base.force + this.offsetInUpDirection.Evaluate(base.timer) * a;

                vector *= weight;
                HitReaction.HitPointEffector.EffectorLink[] array = this.effectorLinks;
                for (int i = 0; i < array.Length; i++)
                {
                    HitReaction.HitPointEffector.EffectorLink effectorLink = array[i];
                    effectorLink.Apply(solver, vector, base.crossFader);
                }
            }
Пример #15
0
                public Vector3 m_PinWeight;              // m_Pin m_Weight vector

                // Apply positionOffset to the effector
                public void Apply(IKSolverFullBodyBiped solver, float m_Weight, Quaternion rotation)
                {
                    // Offset
                    solver.GetEffector(m_Effector).positionOffset += rotation * m_Offset * m_Weight;

                    // Calculating pinned position
                    Vector3 pinPosition       = solver.GetRoot().position + rotation * m_Pin;
                    Vector3 pinPositionOffset = pinPosition - solver.GetEffector(m_Effector).bone.position;

                    Vector3 pinWeightVector = m_PinWeight * Mathf.Abs(m_Weight);

                    // Lerping to pinned position
                    solver.GetEffector(m_Effector).positionOffset = new Vector3(
                        Mathf.Lerp(solver.GetEffector(m_Effector).positionOffset.x, pinPositionOffset.x, pinWeightVector.x),
                        Mathf.Lerp(solver.GetEffector(m_Effector).positionOffset.y, pinPositionOffset.y, pinWeightVector.y),
                        Mathf.Lerp(solver.GetEffector(m_Effector).positionOffset.z, pinPositionOffset.z, pinWeightVector.z)
                        );
                }
Пример #16
0
            public void Update(AnimationCurve stepHeight, float heightOffset, float pinWeight, float correctionY, float correctionXZ, float correctionVelocity)
            {
                stepTimer += Time.deltaTime;

                bool step = stepLength > 0 && stepTimer < stepLength;

                if (!step)
                {
                    effector.positionOffset  += position - effector.bone.position;
                    muscle.state.pinWeightMlp = 0f;
                    return;
                }

                //float legLength = solver.GetChain(effectorType).nodes[0].length + solver.GetChain(effectorType).nodes[1].length;

                if (Vector3.Distance(position, thighMuscle.rigidbody.position) >= 0.5f)
                {
                    muscle.state.pinWeightMlp = 0f;
                }

                position  = Vector3.Lerp(stepFrom, stepTo, stepTimer / stepLength);
                position += solver.GetRoot().up *(stepHeight.Evaluate(stepTimer) + heightOffset);

                if (correctionY != 0f)
                {
                    float realYOffset = position.y - muscle.rigidbody.position.y - muscle.rigidbody.velocity.y * correctionVelocity;
                    if (realYOffset > 0f)
                    {
                        position += Vector3.up * realYOffset * correctionY;
                    }
                }

                if (correctionXZ != 0f)
                {
                    Vector3 realXZOffset = position - muscle.rigidbody.position - muscle.rigidbody.velocity * correctionVelocity;
                    realXZOffset.y = 0f;
                    position      += realXZOffset * correctionXZ;
                }

                effector.positionOffset += position - effector.bone.position;

                muscle.state.pinWeightMlp = pinWeight;
            }
Пример #17
0
            // Update the Body
            public void Update(IKSolverFullBodyBiped solver, float w, float deltaTime)
            {
                if (transform == null || relativeTo == null)
                {
                    return;
                }

                // Find the relative position of the transform
                Vector3 relativePos = relativeTo.InverseTransformDirection(transform.position - relativeTo.position);

                // Initiating
                if (firstUpdate)
                {
                    lastRelativePos = relativePos;
                    firstUpdate     = false;
                }

                // Find how much the relative position has changed
                Vector3 delta = (relativePos - lastRelativePos) / deltaTime;

                // Smooth the change
                smoothDelta = speed <= 0f? delta: Vector3.Lerp(smoothDelta, delta, deltaTime * speed);

                // Convert to world space
                Vector3 worldDelta = relativeTo.TransformDirection(smoothDelta);

                // Extract horizontal and vertical offset
                Vector3 offset = V3Tools.ExtractVertical(worldDelta, solver.GetRoot().up, verticalWeight) + V3Tools.ExtractHorizontal(worldDelta, solver.GetRoot().up, horizontalWeight);

                // Apply the amplitude to the effector links
                for (int i = 0; i < effectorLinks.Length; i++)
                {
                    solver.GetEffector(effectorLinks[i].effector).positionOffset += offset * w * effectorLinks[i].weight;
                }

                lastRelativePos = relativePos;
            }
        private void OnPostRead()
        {
            // Offset
            Vector3 shoulderCenter   = Vector3.Lerp(solver.leftShoulderEffector.bone.position, solver.rightShoulderEffector.bone.position, 0.5f);
            Vector3 toShoulderCenter = shoulderCenter - behaviour.puppetMaster.muscles[0].target.position;

            Vector3 newShoulderCenter = behaviour.puppetMaster.muscles[0].target.position + centerOfMass.inverseRotation * toShoulderCenter;
            Vector3 offsetTarget      = (newShoulderCenter - shoulderCenter);

            offsetTarget = Vector3.ClampMagnitude(offsetTarget * offsetMagMlp, maxOffset);
            offset       = offsetSpeed <= 0f? offsetTarget: Vector3.Lerp(offset, offsetTarget, Time.deltaTime * offsetSpeed);

            // Muscle weights
            float w = muscleWeight.GetValue(centerOfMass.angle);

            foreach (Muscle m in behaviour.puppetMaster.muscles)
            {
                if (m.props.group == Muscle.Group.Spine)
                {
                    m.state.muscleWeightMlp = w;
                }
            }

            // Body
            Vector3 bodyOffset = offset * bodyWeight.GetValue(centerOfMass.angle);

            solver.bodyEffector.positionOffset += bodyOffset;

            if (!solver.bodyEffector.effectChildNodes)
            {
                solver.leftThighEffector.positionOffset  += bodyOffset;
                solver.rightThighEffector.positionOffset += bodyOffset;
            }

            solver.leftHandEffector.maintainRelativePositionWeight  = 0f;
            solver.rightHandEffector.maintainRelativePositionWeight = 0f;

            //solver.leftShoulderEffector.positionOffset += bodyOffset;
            //solver.rightShoulderEffector.positionOffset += bodyOffset;
            //solver.leftHandEffector.positionOffset += bodyOffset;
            //solver.rightHandEffector.positionOffset += bodyOffset;

            // Shoulders
            Vector3 shoulderOffset = offset * shoulderWeight.GetValue(centerOfMass.angle);

            solver.leftShoulderEffector.positionOffset  += shoulderOffset;
            solver.rightShoulderEffector.positionOffset += shoulderOffset;

            /*
             * // Non-FBBIK version
             * if (!centerOfMass.isGrounded) return;
             * var biped = solver.GetRoot().GetComponent<FullBodyBipedIK>().references;
             *
             * Quaternion leftFootRotation = biped.leftFoot.rotation;
             * Quaternion rightFootRotation = biped.rightFoot.rotation;
             *
             * float angle = 0f;
             * Vector3 axis = Vector3.zero;
             * centerOfMass.inverseRotation.ToAngleAxis(out angle, out axis);
             *
             * // Thighs
             * float thighAngle = Mathf.Clamp(angle * thighWeight, -maxAngle, maxAngle);
             * Quaternion thighRotation = Quaternion.AngleAxis(thighAngle, axis);
             *
             * biped.leftThigh.rotation = thighRotation * biped.leftThigh.rotation;
             * biped.rightThigh.rotation = thighRotation * biped.rightThigh.rotation;
             *
             * // Calves
             * float calfAngle = Mathf.Clamp(angle * calfWeight, -maxAngle, maxAngle);
             *
             * Vector3 legAxisLeft = Vector3.Cross(biped.leftCalf.position - biped.leftThigh.position, biped.leftFoot.position - biped.leftThigh.position);
             * Vector3 axisLeft = Vector3.Project(axis, legAxisLeft);
             *
             * Vector3 legAxisRight = Vector3.Cross(biped.rightCalf.position - biped.rightThigh.position, biped.rightFoot.position - biped.rightThigh.position);
             * Vector3 axisright = Vector3.Project(axis, legAxisRight);
             *
             * Quaternion calfRotationLeft = Quaternion.AngleAxis(-calfAngle, axisLeft);
             * Quaternion calfRotationRight = Quaternion.AngleAxis(-calfAngle, axisright);
             *
             * biped.leftCalf.rotation = calfRotationLeft * biped.leftCalf.rotation;
             * biped.rightCalf.rotation = calfRotationRight * biped.rightCalf.rotation;
             */

            // Non-FBBIK version
            if (!centerOfMass.isGrounded)
            {
                return;
            }
            var biped = solver.GetRoot().GetComponent <FullBodyBipedIK>().references;

            float   angle = 0f;
            Vector3 axis  = Vector3.zero;

            centerOfMass.inverseRotation.ToAngleAxis(out angle, out axis);

            // Spine

            /*
             * float spineAngle = Mathf.Clamp((angle * spineWeight) / biped.spine.Length, -maxAngle, maxAngle);
             * Quaternion spinerotation = Quaternion.AngleAxis(spineAngle, axis);
             *
             * foreach (Transform s in biped.spine) {
             *      s.rotation = spinerotation * s.rotation;
             * }
             */

            // Head
            float      headAngle    = Mathf.Clamp(angle * headWeight, -maxAngle, maxAngle);
            Quaternion headRotation = Quaternion.AngleAxis(headAngle, axis);

            biped.head.rotation = headRotation * biped.head.rotation;
        }
Пример #19
0
        void Stepping()
        {
            stepLegIndex = GetStepLegIndex();

            if (stepLegIndex != -1)
            {
                ;
                RaycastHit hit;

                Vector3 center = behaviour.puppetMaster.muscles[5].rigidbody.position;

                Vector3 velocity = behaviour.puppetMaster.muscles[5].rigidbody.velocity;
                velocity.y = 0f;
                Vector3 position = center + velocity * stepOffsetMlp.GetValue(centerOfMass.angle);

                // Add Offset
                Vector3 stepDirection = position - legs[stepLegIndex].position;
                stepDirection.y = 0f;
                position       += Quaternion.LookRotation(stepDirection) * legs[stepLegIndex].offset;

                // Avoid the other leg
                int     otherLegIndex = OtherLegIndex(stepLegIndex);
                Vector3 otherPosition = legs[otherLegIndex].position;

                float   y          = position.y;
                Vector3 toPosition = Flatten(position - legs[stepLegIndex].muscle.rigidbody.position).normalized;
                Vector3 toOtherLeg = Flatten(otherPosition - legs[stepLegIndex].muscle.rigidbody.position).normalized;
                float   dot        = Mathf.Max(Vector3.Dot(toPosition, toOtherLeg), 0f);

                float angle = dot * 20f;
                if (stepLegIndex == 1)
                {
                    angle = -angle;
                }

                Quaternion rotation = Quaternion.AngleAxis(angle, solver.GetRoot().up);
                position = SetY(legs[stepLegIndex].muscle.rigidbody.position + rotation * toPosition, y);

                // Clamp distance
                Vector3 pelvisToPosition = Flatten(position - behaviour.puppetMaster.muscles[0].rigidbody.position);
                pelvisToPosition = Vector3.ClampMagnitude(pelvisToPosition, maxStepDistance.GetValue(centerOfMass.angle));
                position         = behaviour.puppetMaster.muscles[0].rigidbody.position + pelvisToPosition;

                ray = new Ray(position, Vector3.down);

                if (Physics.Raycast(ray, out hit, 2f, groundLayers))
                {
                    Vector3 stepTo = hit.point + Vector3.up * stepHeightOffset.GetValue(centerOfMass.angle);

                    groundYOffsetTarget = Mathf.Min(hit.point.y - behaviour.puppetMaster.targetRoot.position.y, groundYOffsetTarget);
                    //groundYOffsetTarget = hit.point.y - solver.GetRoot().position.y;

                    if (Vector3.Distance(stepTo, legs[stepLegIndex].position) > stepThreshold.GetValue(centerOfMass.angle))
                    {
                        float stepLength = stepHeight[stepHeight.length - 1].time;

                        legs[stepLegIndex].Step(stepTo, stepLength, legOffsetMlp.GetValue(centerOfMass.angle), stepDelay.GetValue(centerOfMass.angle));
                    }
                }
            }

            groundYOffset = Mathf.SmoothDamp(groundYOffset, groundYOffsetTarget, ref groundYOffsetVel, 0.2f);
            behaviour.puppetMaster.muscles[0].target.position += solver.GetRoot().up *groundYOffset;

            Debug.DrawRay(ray.origin, ray.direction);

            foreach (Leg leg in legs)
            {
                leg.Update(stepHeight, stepHeightOffset.GetValue(centerOfMass.angle), footPinWeight, correctionY, correctionXZ, correctionVelocity);

                behaviour.puppetMaster.GetMuscle(leg.chain.nodes[0].transform).state.muscleWeightMlp = muscleWeightThigh.GetValue(centerOfMass.angle);
                behaviour.puppetMaster.GetMuscle(leg.chain.nodes[1].transform).state.muscleWeightMlp = muscleWeightCalf.GetValue(centerOfMass.angle);
                behaviour.puppetMaster.GetMuscle(leg.chain.nodes[2].transform).state.muscleWeightMlp = muscleWeightFoot.GetValue(centerOfMass.angle);
            }
        }
Пример #20
0
            public float testAngle;             //Addition rot state  -1<- 0 ->1

            public void Reset(IKSolverFullBodyBiped solver)
            {
                m_LastForward = solver.GetRoot().forward;
            }