//move left/right towards the destination node. //if hard deceleration would result in overshooting the destination, decelerate. //This is to stop the AI from going too fast and driving off the edge of a platform private void stepWalking(float dt) { if (destinationNode == null) { return; } Physics.Vector2D displacement = mainGraph.topLevelNode.GetNodePosition(destinationNode) - AIPlayer.playerBody.Position; //Get the direction of the displacement (-1 = dest is to the left) float sign = displacement.X / Math.Abs(displacement.X); //Get the displacement if the AI were to hard brake float naturalDisplacement = Physics.SuvatEquations.SFromUVA(AIPlayer.playerBody.LinearVelocity.X, 0.0f, AIPlayer.GetHorizontalAcceleration()) * -1; float currentDirectionOfMotion = 1; //force natural displacement to be positive if (AIPlayer.playerBody.LinearVelocity.X != 0) { currentDirectionOfMotion = AIPlayer.playerBody.LinearVelocity.X / Math.Abs(AIPlayer.playerBody.LinearVelocity.X); } naturalDisplacement *= currentDirectionOfMotion; //If the displacement with deceleration results in overshooting, slow down if (displacement.X * sign < naturalDisplacement * sign) { //decelerate if (sign > 0) { actionPlan.Add(new act_dur((int)ACTIONS.MOVE_LEFT, 0.0f)); } else { actionPlan.Add(new act_dur((int)ACTIONS.MOVE_RIGHT, 0.0f)); } } else { //if travelling the wrong way, or travelling below walkingSpeed, accelerate towards the destination if (currentDirectionOfMotion != sign || Math.Abs(AIPlayer.playerBody.LinearVelocity.X) < walkingSpeed) { if (sign < 0) { actionPlan.Add(new act_dur((int)ACTIONS.MOVE_LEFT, 0.0f)); } else { actionPlan.Add(new act_dur((int)ACTIONS.MOVE_RIGHT, 0.0f)); } } else { actionPlan.Add(new act_dur((int)ACTIONS.DO_NOTHING, 0.0f)); } } }