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.
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;
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.
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:
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
timeGetTime()
to calculate previous frame. Thanks.
– MahanGM
Jul 22 '12 at 09:59