// Collision check for the lower resolutions model
    public bool CheckRootCollisions(FootstepPlanningState state)
    {
        Vector3 start  = state.currentPosition - new Vector3(0, 10 * analyzer.GetHeight() / 2, 0);
        Vector3 end    = start + new Vector3(0, 10 * analyzer.GetHeight() / 2, 0);
        float   radius = analyzer.GetRadius();

        if (Physics.CheckCapsule(start, end, radius, layer))
        {
            //if (Physics.CheckSphere(state.currentPosition,radius,layer))
            return(true);
        }


        if (state.previousState != null)
        {
            int samples            = analyzer.samples;
            AnotatedAnimation anim = analyzer.GetAnotatedAnimation(state.actionName);

            for (int i = 1; i < samples - 1; i++)
            {
                start = state.previousState.currentPosition + anim.Root[i].position;
                end   = start + new Vector3(0, analyzer.GetHeight() / 2, 0);

                if (Physics.CheckCapsule(start, end, radius, layer))
                {
                    //if (Physics.CheckSphere(state.previousState.currentPosition,radius,layer))
                    return(true);
                }
            }
        }


        return(false);
    }
Exemple #2
0
    public void React()
    {
        FootstepPlanningState frameState = CreateFrameState();

        animation.Stop();


        AnotatedAnimation stopAnim = analyzer.GetAnotatedAnimation("Idle");

        FootstepPlanningAction stopAction = new FootstepPlanningAction(frameState, stopAnim, 1, analyzer.meanStepSize, analyzer.mass);

        engine.InsertAction(stopAction);

        reacting = true;
    }
Exemple #3
0
    void OnDrawGizmos()
    {
        if (!debugDraw)
        {
            return;
        }

        if (planning == null)
        {
            return;
        }

        /*
         * //if(firstTime)
         * //{
         * foreach(ADAstarNode node in ADAstarPlanner.Closed.Values)
         * {
         *      if(node.action.GetType() == typeof(FootstepPlanningAction)){
         *      Vector3 center = ((node.action as FootstepPlanningAction).state as FootstepPlanningState).currentPosition;
         *      Gizmos.color = Color.blue;
         *      Gizmos.DrawSphere(center, .25f);
         *      }
         * }
         * //	firstTime = false;
         * //}
         */

        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(lastPos, analyzer.GetRadius());
        Gizmos.DrawWireSphere(lastObstacle, 1.0f);

        //if (debugOnlyCurrent)
        //	return;

        FootstepPlanningAction[] plan = planning.GetOutputPlan();

        numberOfActions = plan.Length;

        for (int i = 0; i < numberOfActions; i++)
        {
            if (plan[i] != null)
            {
                FootstepPlanningState state = plan[i].state as FootstepPlanningState;

                if (state != null)
                {
                    AnotatedAnimation animInfo = analyzer.GetAnotatedAnimation(state.actionName);

                    //Quaternion rotation = state.currentRotation;

                    Vector3 position;

                    if (animInfo.swing == Joint.LeftFoot)
                    {
                        position     = state.leftFoot;
                        position[1]  = auxHeight;
                        Gizmos.color = Color.green;
                    }
                    else
                    {
                        position     = state.rightFoot;
                        position[1]  = auxHeight;
                        Gizmos.color = Color.blue;
                    }

                    Gizmos.DrawWireSphere(state.currentPosition, analyzer.GetRadius());
                    Gizmos.DrawWireSphere(state.obstaclePos, 1.0f);
                }
                else
                {
                    GridTimeState gtState = plan[i].state as GridTimeState;

                    if (gtState != null)
                    {
                        Vector3 position = gtState.currentPosition;
                        if (i % 2 == 0)
                        {
                            Gizmos.color = Color.green;
                        }
                        else
                        {
                            Gizmos.color = Color.blue;
                        }

                        if (!debugOnlyCurrent || (debugOnlyCurrent && Mathf.Abs(Time.time - gtState.time) < 0.5f))
                        {
                            Gizmos.DrawWireSphere(gtState.currentPosition, analyzer.GetRadius());

                            Gizmos.color = Color.red;
                            Gizmos.DrawWireSphere(gtState.obstaclePos, 1.0f);
                        }
                    }
                }
            }
        }
    }
Exemple #4
0
    void LateUpdate()
    {
        if (stored)
        {
            return;
        }

        // If analysis done remove all animations
        if (currentAnimationIndex == animationNames.Length)
        {
            if (analyzer != null)
            {
                // Stop and remove animations from the character
                analyzer.RemoveAnimations(animation);

                // Update global information
                analyzer.UpdateGlobalInfo();

                // Store analysis
                // writing an object to file
                ObjectSerializer os = new ObjectSerializer();
                os.serializedObject = analyzer.analyzedAnimations;
                os.writeObjectToFile(fileName);

                stored = true;

                if (analyzer.log)
                {
                    analyzer.ReadAnalysisFromFile(fileName);
                }
            }

            // End execution
            return;
        }

        //if (currentAnimation != null
        //    && Mathf.Abs(currentAnimation.time) < currentActionTime
        //)
        // We compute the root displacement
        //rootMotion.ComputeRootMotion();

        if (changed && currentAnimationName != null)
        {
            //currentAnimation.time = 0;
            //agent.animation.Sample();
            //rootMotion.ComputeRootMotion();

            AnotatedAnimation animInfo = analyzer.GetAnotatedAnimation(currentAnimationName);

            // analyze initial state
            analyzer.analyzeInitState(animInfo);

            currentSample = 1;
        }

        // If its the first animation or the current animation ended,
        if (currentAnimation == null ||
            Mathf.Abs(currentAnimation.time) >= currentActionTime
            )
        {
            if (currentAnimationName != null)
            {
                Debug.Log("Time difference = " + (currentAnimation.time - currentActionTime));

                // We correct the pose to the one we really want to analyze
                //currentAnimation.time = currentActionTime;
                //agent.animation.Sample();
                //rootMotion.ComputeRootMotion();


                AnotatedAnimation endedAnimInfo = analyzer.GetAnotatedAnimation(currentAnimationName);

                // Analyze end state
                analyzer.analyzeState(endedAnimInfo, analyzer.samples - 1, currentAnimation.normalizedTime);

                // Analyze the movement
                analyzer.analyzeMovement(endedAnimInfo);

                // Update global values
                analyzer.updateGlobalInfo(endedAnimInfo);
            }

            // Get new animation
            currentAnimationIndex++;

            // If there is no more animations we stop
            if (currentAnimationIndex == animationNames.Length)
            {
                return;
            }

            string animationName = animationNames[currentAnimationIndex];

            if (blending)
            {
                // We blend out the previous animation
                // if it exists and it is not the first one
                if (currentAnimation != null)
                {
                    if (currentAnimationIndex > 0)
                    {
                        agent.animation.Blend(currentAnimation.name, 0.0f, currentBlendingTime);
                    }
                    else
                    {
                        agent.animation.Stop();
                        agent.animation.Sample();
                    }
                }
            }

            // We set up the speed of the new animation
            AnimationState newAnimationState = agent.animation[animationName];
            newAnimationState.speed = 1.0f;
            // We set the animation at the beginning
            newAnimationState.time    = 0;
            newAnimationState.enabled = true;

            // We save the new animation
            currentAnimation     = newAnimationState;
            currentAnimationName = animationName;

            // We get the info of the new animation
            AnotatedAnimation animInfo = analyzer.GetAnotatedAnimation(animationName);

            // We compute the time of the action and the time of the blending
            float newActionTime = animInfo.time * animInfo.totalLength;

            float newBlendingTime = animInfo.totalLength * animInfo.footPlantLenght;
            if (animInfo.type != LocomotionMode.Walk)
            {
                newBlendingTime = 0.5f;
            }

            currentActionTime = newActionTime;

            if (previousBlendingTime < newBlendingTime)
            {
                currentBlendingTime = previousBlendingTime;
            }
            else
            {
                currentBlendingTime = newBlendingTime;
            }

            previousBlendingTime = newBlendingTime;

            if (blending)
            {
                //We play/blend in the new animation if it's not the first one
                if (currentAnimationIndex > 0)
                {
                    agent.animation.Blend(currentAnimation.name, 1.0f, currentBlendingTime);
                }
                else
                {
                    agent.animation.Play(currentAnimation.name);
                    agent.animation.Sample();
                }
            }
            else
            {
                agent.animation.Play(currentAnimation.name);
            }

            changed = true;

            currentSample = 0;
        }
        else
        {
            changed = false;

            if (currentAnimationName != null)
            {
                AnotatedAnimation animInfo = analyzer.GetAnotatedAnimation(currentAnimationName);

                float previousSampleTime = (currentSample - 1) * animInfo.time / (analyzer.samples - 1);
                previousSampleTime *= animInfo.totalLength;

                float timeBetweenSamples = animInfo.time / (analyzer.samples - 1);

                float timeDifference = Mathf.Abs(currentAnimation.time) - previousSampleTime;

                if (timeDifference >= timeBetweenSamples)
                {
                    // We correct the pose to the one we really want to analyze
                    //currentAnimation.time = currentSample*animInfo.time/(analyzer.samples-1);
                    //agent.animation.Sample();

                    analyzer.analyzeState(animInfo, currentSample, currentAnimation.normalizedTime);

                    currentSample++;
                }
            }
        }
    }
    ////////////////////////////////////////////////////////////////////////////////////


    public override FootstepPlanningAction[] GetOutputPlan()
    {
        if (outputPlan == null)
        {
            return(null);
        }

        FootstepPlanningAction[] actionList = new FootstepPlanningAction[outputPlan.Count];
        int i     = 0;
        int count = 0;

//		Debug.Log("OutputPlan: " + outputPlan.Count);

        while (i < outputPlan.Count)
        {
            DefaultAction action = outputPlan.ElementAt(i);

            //Debug.Log("we have an action " + i);

            FootstepPlanningAction fsAction = action as FootstepPlanningAction;
            if (fsAction != null)
            {
                actionList[count] = fsAction;
                count++;
                if (REPRODUCTION_MODE.REPRODUCTION_MODE.Equals(Reproduction.MODE.RECORD))
                {
                    storedPlan.Add(fsAction);
                    //Debug.Log("Recording");
                }
            }

            else
            {
                GridPlanningAction gridAction = action as GridPlanningAction;
                if (gridAction != null)
                {
                    fsAction = new FootstepPlanningAction(gridAction, analyzer.GetAnotatedAnimation("Idle"), 1.5f);

                    if (fsAction != null)
                    {
                        actionList[count] = fsAction;
                        count++;
                        if (REPRODUCTION_MODE.REPRODUCTION_MODE.Equals(Reproduction.MODE.RECORD))
                        {
                            storedPlan.Add(fsAction);
                            //Debug.Log("Recording");
                        }
                    }

                    //Debug.Log("OutputPlan at time " + Time.time + " has " + outputPlan.Count + " actions.");
                    //Debug.Log("Grid Action added at time: " + Time.time + " at pos " + count);
                }

                else
                {
                    //Debug.Log("We might add a GridTimeAction");

                    GridTimeAction gridTimeAction = action as GridTimeAction;
                    if (gridTimeAction != null)
                    {
                        if (analyzer != null)
                        {
                            fsAction = new FootstepPlanningAction(gridTimeAction, analyzer.GetAnotatedAnimation("Idle"), 1.0f);
                        }

                        if (fsAction != null)
                        {
                            //Debug.Log("Adding a gridTimeAction");

                            actionList[count] = fsAction;
                            count++;
                            if (REPRODUCTION_MODE.REPRODUCTION_MODE.Equals(Reproduction.MODE.RECORD))
                            {
                                storedPlan.Add(fsAction);
                                //Debug.Log("Recording");
                            }
                        }
                    }
                }
            }

            i++;
        }

        return(actionList);
    }