7
\$\begingroup\$

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.

\$\endgroup\$
5
\$\begingroup\$

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;
\$\endgroup\$
  • \$\begingroup\$ Actually before posting this question I wrote this solution. Your answer will be completed by the Xavier Arias Botargues's answer. Thanks. \$\endgroup\$ – MahanGM Jul 22 '12 at 9:58
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ This is similar to other 2 answers. Thanks. \$\endgroup\$ – MahanGM Jul 22 '12 at 10:01
1
\$\begingroup\$

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:

  • vector addition
  • scalar and vector multiplication

more info: http://en.wikipedia.org/wiki/Euclidean_vector#Addition_and_subtraction

To create a vector with a given angle you can use the method described by Mr. Beast.

To simulate friction you can do something like:

speed -= speed * friction

Where friction should be a number: 0 < friction < 1

\$\endgroup\$
  • \$\begingroup\$ Yeah I didn't mention the time passing in my question. I'm using timeGetTime() to calculate previous frame. Thanks. \$\endgroup\$ – MahanGM Jul 22 '12 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.