I am building a game in C# using XNA.
I am currently trying to get a character to always travel to the middle of the map which is located a point (0,0,0). I want the character to accelerate to the middle and then decelerate before it hits the middle so it will come to a stop at (or very close to) the middle.
Rate of acceleration does not change. Velocity is a Vector3 and for the purpose of this i will only worry about the X axis.
What I am trying to work out is at what point should I start decelerating to come to a stop in the middle depending on velocity and position.
I have tried to simplify the following code as much as possible
In My Update Method I basically call two methods
Move(gameTime);
HandleAcceleration();
Handle Acceleration is very simple
this.Transform.TranslateIncrement = this.Velocity;
Move method
protected void Move(GameTime gameTime)
{
//GetDirection just returns 1 or -1 to accelerate the appropriate direction
int direction = GetDirection((int)this.Transform.Translation.X);
//This Is where I have trouble With GetMinDecelerationDistance()
if (GetDistanceToMiddle() <= GetMinDecelerationDistance())
{
//Pass in -direction to decelerate(Accelerate opposite direction)
Accelerate(gameTime, AxisDirectionType.X, -direction);
}
else
{
Accelerate(gameTime, AxisDirectionType.X, direction);
}
}
My Accelerate Method
VelocityX += (this.acceleration * gameTime.ElapsedGameTime.Milliseconds) * direction;
this.Velocity = (new Vector3(VelocityX, 0, 0));
The problem I am having is how to implement the GetMinDecelerationDistance() Method. I know I need to do something with the Velocity and the position but I have no Idea what. Keep in mind the acceleration rate is always the same and the middle is at point 0.
Basically my question is how do I implement GetMinDecelerationDistance() given that I know the Velocity and position, Acceleration is constant and I want to stop at point 0.