Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I want to move my object with an acceleration in the proper direction. I'm not good at math so I need some help. The example in Game Maker is:

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

But I'm not working with Game Maker any more and this is the C++ right now. For making it clear, I need a formula to create a linear acceleration.

Thanks.

share|improve this question

3 Answers 3

up vote 5 down vote accepted

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

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

share|improve this answer
    
Yeah I didn't mention the time passing in my question. I'm using timeGetTime() to calculate previous frame. Thanks. –  MahanGM Jul 22 '12 at 9:59

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.

share|improve this answer
    
This is similar to other 2 answers. Thanks. –  MahanGM Jul 22 '12 at 10:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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