# Create acceleration with direction

I want to move my object with an acceleration in the proper direction. For example if I have these values:

friction = 0.3;
direction = 180;
speed = 5;


For making it clear, I need a formula to create a linear acceleration.

You can calculate the invidual components by using sin and cos:

xSpeed = sin(direction)*speed;
ySpeed = cos(direction)*speed;
[...]
xPos += xSpeed;
yPos += ySpeed;


Accleration is simply increasing the speed.

xAccel = sin(accelDirection)*accleration;
yAccel = cos(accelDirection)*accleration;
// [...]
xSpeed += xAccel;
ySpeed += yAccel;

• Actually before posting this question I wrote this solution. Your answer will be completed by the Xavier Arias Botargues's answer. Thanks. Commented Jul 22, 2012 at 9:58

Acceleration is the rate at which velocity changes. If it is zero, then you will keep moving at your start velocity. If it is larger than zero, and in the same direction as the current velocity, velocity will increase.

velocity = old_velocity + acceleration;
position = old_position + velocity


If acceleration = 2, then your velocity will increase by 2 for each timestep.

• This is similar to other 2 answers. Thanks. Commented Jul 22, 2012 at 10:01

To do any movement or physics calculations you should always use the time spent since the last frame (dt or delta); this depends on the main loop implementation, but is generaly passed to a method called update as parameter. (If this is not available assume dt = 1)

To update speed with acceleration and time and position with speed and time you can:

speed += acceleration * dt;
position += speed * dt;


Note that acceleration, speed and position are vectors, so they have the operations:

speed -= speed * friction

• Yeah I didn't mention the time passing in my question. I'm using timeGetTime() to calculate previous frame. Thanks. Commented Jul 22, 2012 at 9:59