Say I have a car-game, and want to move the car by integrating the car's acc and vel to get the position each time step.

This is the way I would do it:

In pseudo code:

proc integrate( deltaTime )

; dv = a dt

v = a * deltaTime

; a = F / m

; a is the car's acceleration where a = ( thrust - breakForce ) / mass

; 1: ds = v dt - with fixed velocity and acceleration = 0

; 2: ds = at dt - with fixed acceleration

; s = a dt^2 / 2

s = v * deltaTime / 2

; Moves the car.

; Because I have both fixed velocity ( the vel from last time step )

; and fixed acceleration, I must use both integration formulas

; pos += v t + a t^2 / 2

add xPos, ( xVel * deltaTime ) + s * cosAngle

add yPos, ( yVel * deltaTime ) + s * sinAngle

; Adds to the car's velocity

add xVel, ( v * cosAngle )

add yVel, ( v * sinAngle )

end proc

I think this would work, but I am not sure. Is there another way to do the intergration? I have heard about Eulers method, but I don't know how it works.

This is the way I would do it:

In pseudo code:

proc integrate( deltaTime )

; dv = a dt

v = a * deltaTime

; a = F / m

; a is the car's acceleration where a = ( thrust - breakForce ) / mass

; 1: ds = v dt - with fixed velocity and acceleration = 0

; 2: ds = at dt - with fixed acceleration

; s = a dt^2 / 2

s = v * deltaTime / 2

; Moves the car.

; Because I have both fixed velocity ( the vel from last time step )

; and fixed acceleration, I must use both integration formulas

; pos += v t + a t^2 / 2

add xPos, ( xVel * deltaTime ) + s * cosAngle

add yPos, ( yVel * deltaTime ) + s * sinAngle

; Adds to the car's velocity

add xVel, ( v * cosAngle )

add yVel, ( v * sinAngle )

end proc

I think this would work, but I am not sure. Is there another way to do the intergration? I have heard about Eulers method, but I don't know how it works.

Afternoon, caudieux.

How accurate do you need this to be?:tongue:

You could just:

get the time elapsed (dt)

multiply acceleration by dt

multiply velocity by dt

add velocity and acceleration together

cos/sin the previous addition

use the two results for updating xpos and ypos.

Change the acceleration with keys/mouse (thrust/brake).

Cheers,

Scronty

How accurate do you need this to be?:tongue:

You could just:

get the time elapsed (dt)

multiply acceleration by dt

multiply velocity by dt

add velocity and acceleration together

cos/sin the previous addition

use the two results for updating xpos and ypos.

Change the acceleration with keys/mouse (thrust/brake).

Cheers,

Scronty

Hi, Scronty

This is what I think you said I should do:

I have skipped the sin/cos part

a *= dt;

carVel *= dt;

carVel += a;

pos += carVel;

When I look at the formula, i see that carVel is multiplied by dt, but wouldn't this cause a problem?

Let's say that the initial velocity of the car is 10m/s, there is no acceleration, and that dt is 0.0002s.

If i multiply 10 with 0.00125 the velocity next time step would be 0.0125m/s, and the car would stop.

I don't think I got your reply right:confused:

This is what I think you said I should do:

I have skipped the sin/cos part

a *= dt;

carVel *= dt;

carVel += a;

pos += carVel;

When I look at the formula, i see that carVel is multiplied by dt, but wouldn't this cause a problem?

Let's say that the initial velocity of the car is 10m/s, there is no acceleration, and that dt is 0.0002s.

If i multiply 10 with 0.00125 the velocity next time step would be 0.0125m/s, and the car would stop.

I don't think I got your reply right:confused:

Afternoon, caudieux.

heh.

In your proggy, you have variables holding the values for (acceleration)(velocity)(direction)

These are the *world* variables. i.e. velocity == 10 m/s .

When it comes to updating these variables, you have to take into account the current timeslice (i.e. 0.0002s).

To make it easier to understand to start with, use extra variables to hold the *current timeslice* information.

If carAccel = 0 (m/s/s)

and carVel = 10 (m/s)

plus TimeSlice = 0.0002s

then dcarAccel = carAccel * TimeSlice

and dcarVel = carVel * TimeSlice

dcarVel += dcarAccel

dcarAccel is the acceleration *added* (could be -ve for deceleration) to the velocity for this timeslice

dcarVel is the velocity of the car for this timeslice

carVel += dcarAccel

carVel also gets incremented by the acceleration timeslice

Use dcarVel for the distance travelled when calculating where to position your car (the sin/cos part:grin: )

Note that the only thing changed to the *world* variables, is the addition of any acceleration to the velocity.

To adjust the *speed* of the car, just change carAccel with keyboard/mouse.

Cheers,

Scronty

heh.

In your proggy, you have variables holding the values for (acceleration)(velocity)(direction)

These are the *world* variables. i.e. velocity == 10 m/s .

When it comes to updating these variables, you have to take into account the current timeslice (i.e. 0.0002s).

To make it easier to understand to start with, use extra variables to hold the *current timeslice* information.

If carAccel = 0 (m/s/s)

and carVel = 10 (m/s)

plus TimeSlice = 0.0002s

then dcarAccel = carAccel * TimeSlice

and dcarVel = carVel * TimeSlice

dcarVel += dcarAccel

dcarAccel is the acceleration *added* (could be -ve for deceleration) to the velocity for this timeslice

dcarVel is the velocity of the car for this timeslice

carVel += dcarAccel

carVel also gets incremented by the acceleration timeslice

Use dcarVel for the distance travelled when calculating where to position your car (the sin/cos part:grin: )

Note that the only thing changed to the *world* variables, is the addition of any acceleration to the velocity.

To adjust the *speed* of the car, just change carAccel with keyboard/mouse.

Cheers,

Scronty

Ok, thank you Scronty!

Afternoon, caudieux.

Ummm. You're welcome?!?:confused:

I'm not used to seeing many "thank you"s around here. Replies usually end up as arguments:tongue: . I've just glad hardly anyone comes to the Game Programming forum:grin: .

Is this going to be a "top-down" car game? (i.e. viewed from above). 2d/3d? GDI/DX/OpenGL?

Or you dunno yet - this is just for a learning experience?

Cheers,

Scronty

Ummm. You're welcome?!?:confused:

I'm not used to seeing many "thank you"s around here. Replies usually end up as arguments:tongue: . I've just glad hardly anyone comes to the Game Programming forum:grin: .

Is this going to be a "top-down" car game? (i.e. viewed from above). 2d/3d? GDI/DX/OpenGL?

Or you dunno yet - this is just for a learning experience?

Cheers,

Scronty

It's gonna be a car game in 2d viewed from above, and I'm making it primarily for learning experience.