Hello everybody

I'm new in game programming and I'm trying to implement my first game.

I hope you can forgive the awkward question.

In my game, I have a vehicle which is controlled by the computer (the vehicle is an automatic ressource collector).

The vehicle can fly to his target (to collect or deliver ressources) on a direct path, there are no obstacles.

(Path finding is maybe something for later)

The target is represented by a 2d vector.

I accelerate my vehicle linearly towards the target.

The vehicle currently overshoots the target, but it should stop smoothly by applying linear force/deceleration in the opposite direction.

My way to go, is to calculate on every component-update call, where my vehicle whould stop, if I applying my linear deceleration from now on.

When the stop position is behind the target, I apply deceleration, otherwise I apply acceleration.

Gravity is disabled for my vehicle. The mass of the body is currently 1f.

My formula to calculate the stopping distance:

**Code:**

public static Vector2 WayByAcceleration(Vector2 acceleration, Vector2 startVelocity, Vector2 endVelocity, float mass)

{

// ToDo: Find out how mass affects acceleration and take account on it

return ((startVelocity * startVelocity - endVelocity * endVelocity)) / acceleration / 2;

}

My formula is based on:

[escaped, urls not allowed in my first post, anti spam]

[*]http jumk.de/formeln/beschleunigung-weg.shtml

[*]http stackoverflow.com/questions/1088088/how-to-calculate-deceleration-needed-to-reach-a-certain-speed-over-a-certain-dis

I'm calling this method on this way (pseudo):

**Code:**

Vector2 brakeDeceleration = new Vector2(0.2f, 0.2f);

Vector2 stopPos = GameObj.Transform.Pos.Xy;

// linear deceleration // current speed // desired speed // mass

stopPos = stopPos + WayByAcceleration(brakeDeceleration, Body.LinearVelocity, Vector2.Zero, Body.Mass);

if(stopPos overshoots target)

{

apply deceleration

}

else

{

apply acceleration

}

The result of stopPos is always in front of my vehicle, and the distance to my vehicle increases with greater speed.

But the distance is not enough, my vehicle starts deceleration in the last few frames before reaching the target and overshoots it.

It feels like I'm putting parameters with wrong units in my WayByAcceleration method.

So my question is:

- Is this the correct formula for this situation?

- Do I have convert some of the input parameters in other units?

- Do I use the vectors on the correct way?

Thank you very much

Have a nice day,

elia