car motion

hello!

i have been fooling around with the idea of making a top down racing game (seems like a few ppl are doing that) but anyway… ive been having some trouble with the motion of the car.

so far ive got it “driving” around the screen, no track or any boundaries it just goes. i sortof got the turning down, but then it i have a bit of a problem that i dont know how to solve yet which is slowing down when turning aswell. when i take my finger off the accelerator button, but i turn aswell, it does follow the path of the turning thing, it just goes in the last direction it was when the accelerator goes. i tried several approaches to this problem, mostly involving moving around the place where i calculated the angle it should be moving, because thats what i figured i should do, but it didnt really work, at all…

so this is the current part of my code with the accelerating and angle stuff:


if (keys[KeyEvent.VK_LEFT]) {
                        pcar[T]-=5;
                  }
                  if (keys[KeyEvent.VK_RIGHT]) {
                        pcar[T]+=5;
                  }
                  if (keys[KeyEvent.VK_UP]){
                        pcar[VX] += Math.sin(Math.toRadians(pcar[T])) * 1.0;
                        pcar[VY] += -Math.cos(Math.toRadians(pcar[T])) * 1.0;
                  }
                  
                  if (keys[KeyEvent.VK_DOWN]) {
                        pcar[VX] += -Math.sin(Math.toRadians(pcar[T])) * 1.0;
                        pcar[VY] += Math.cos(Math.toRadians(pcar[T])) * 1.0;
                        
                  }
                  
                  pcar[VX] *= 0.9;
                  pcar[VY] *= 0.9;
                                                                        
                  pcar[DX] += pcar[VX];
                  pcar[DY] += pcar[VY];

dx/y is the cars position, vx/y is the velocity in that direction and T is the angle at which the car is currently rotated. this code here just has the car deccelerate in the direction it was last traveling with the accelerator pushed.

i also had the Math.sin, and Math.cos calculations in with the “friction” thing (pcar[vx] *= Math.sin(Math.toRadians(pcar[T])) * 0.9) but that didnt work.

so what im looking for is any info regarding car physics, and anything on this trigonometry stuff in general

thanks alot :slight_smile:

Here is a simple - but not accurate - algorithm

// car is defined by dx, dy (position), vx, vy (speed) and carAngle (rotation)
// turning affects the angleDirection variable
// accelerating affects accel variable

carAngle += angleDirection * deltaTime * CAR_ROT_SPEED;

// distance
double d = Math.sqrt(vx * vx + vy * vy) + accel * deltaTime;

if (d>0){
vx = d * Math.cos(carAngle);
vy = d * Math.sin(carAngle);
} else {
vx = vy = 0;
}

float newX = dx + vx* deltaTime;
float newY = dy + vy*deltaTime;

// collision detection

// no collision, update coords
dx = newX;
dy = newY;

(extracted from bubbleracer2005 - work in progress)

Lilian

this looks good, what does deltaTime represent? and where does it come from?

deltaTime is the time elapsed since the last move, in whatever unit you prefer (i prefer seconds).

double prevTime = System.nanoTime();

while(! end){
double currentTime = System.nanoTime();
double deltaTime = (currentTime - prevTime) / 1e9;
animation();
prevTime = currentTime;
}

ok that looks like it will work, ill try and implement it into what im working with

thanks for your help.