示例#1
0
    public void ComputeRootMotion()
    {
        if (currentAnimation == null || action == null)
        {
            //Debug.Log("something is wrong at time " + Time.time );

            return;
        }

        AnimationState anim = currentAnimation;

        Vector3 displacement = Vector3.zero;
        float   rotY         = 0;

        float currentTime = anim.normalizedTime;

        CompleteAnimationCurve animCurve = action.animInfo.RootCurve;

        displacement.x = animCurve.xPos.Evaluate(currentTime);
        displacement.y = animCurve.yPos.Evaluate(currentTime);
        displacement.z = animCurve.zPos.Evaluate(currentTime);

        rotY = animCurve.yRot.Evaluate(currentTime);

        transform.position = initGameObjectPos;
        transform.rotation = Quaternion.Euler(0, initGameObjectRotY, 0);

        displacement = transform.rotation * displacement;

        transform.Translate(displacement, Space.World);

        transform.Rotate(new Vector3(0, rotY, 0));

        Vector3 forwardProj = new Vector3(-root.right.x, 0, -root.right.z);
        Vector3 zVector     = new Vector3(0, 0, 1);
        float   newRotY     = AnimationAnalyzer.CalculateAngleOf2Vectors(zVector, forwardProj);

        root.RotateAround(transform.position, Vector3.up, -newRotY + initGameObjectRotY + rotY);

        root.localPosition = initLocalRootPos;

        /****/
        xPos = animCurve.xPos;
        yPos = animCurve.yPos;
        zPos = animCurve.zPos;
        yRot = animCurve.yRot;
        /****/
    }
示例#2
0
    public float ComputeCost(float meanStepSize, float mass)
    {
        /*
         *
         * //if (animInfo.type == LocomotionMode.Idle)
         * //	return 0;
         *
         * float spaceCost = animInfo.distance / meanStepSize;
         * //if (animInfo.type == LocomotionMode.Turn )
         * //	spaceCost = 0;
         *
         * float timeCost = 0;
         * if (state != null)
         * {
         *      timeCost = (state as FootstepPlanningState).currentSpeed;
         *
         *      if (animInfo.type == LocomotionMode.Turn || animInfo.type == LocomotionMode.WalkTurn)
         *              timeCost = animInfo.rotationSpeed * speed;
         * }
         *
         * //timeCost = animInfo.time*animInfo.totalLength;
         *
         * float angleCost = animInfo.angleCost;
         *
         * //return timeCost;
         * //return spaceCost;
         * return  spaceCost + timeCost;
         * //return  spaceCost + timeCost + angleCost;
         * //return 1;
         *
         */

        int samples = animInfo.Root.Length;

        float v_0 = 0;

        if (state != null)
        {
            FootstepPlanningState previousState = (state as FootstepPlanningState).previousState;

            if (previousState != null)
            {
                v_0 = previousState.currentSpeed;
            }
        }

        //v_0 = 0;

        CompleteAnimationCurve animCurve = animInfo.RootCurve;

        float timeInterval = 0.1f;

        float endTime = animInfo.time * animInfo.totalLength;

        float cost1 = (e_s) * timeInterval;
        float cost2 = (e_w * v_0 * v_0) * timeInterval;

        //Debug.Log(animInfo.name + " time = " + endTime);

        for (float t = 0.1f; t <= endTime; t += timeInterval)
        {
            float normT = t / animInfo.totalLength;

            Vector3 displacement = Vector3.zero;

            displacement.x = animCurve.xPos.Evaluate(normT);
            displacement.y = animCurve.yPos.Evaluate(normT);
            displacement.z = animCurve.zPos.Evaluate(normT);

            float v2 = displacement.sqrMagnitude / (t * t);

            //Debug.Log(animInfo.name + " v2 = " + v2 + "(at time " + t + ")");

            cost1 += (e_s) * timeInterval;
            cost2 += (e_w * v2) * timeInterval;
        }

        float cost = (cost1 + cost2) * mass;

        cost += animInfo.angleCost / 180 * 5 * mass;

        if (animInfo.type == LocomotionMode.Run)
        {
            cost *= 2;
        }

        //Debug.Log("Cost1 of " + animInfo.name + " = " + cost1);
        //Debug.Log("Cost2 of " + animInfo.name + " = " + cost2);
        //Debug.Log("Cost of " + animInfo.name + " = " + cost);

        return(cost);
    }