Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (538) Games in Android Showcase (132) games submitted by our members Games in WIP (600) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 car motion  (Read 1745 times) 0 Members and 1 Guest are viewing this topic.
javatypo

Junior Devvie

 « Posted 2005-03-15 21:27:14 »

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:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22 `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
c_lilian

Senior Devvie

Projects: 1

Java games will probably rock someday...

 « Reply #1 - Posted 2005-03-16 07:56:21 »

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

javatypo

Junior Devvie

 « Reply #2 - Posted 2005-03-16 12:09:07 »

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

Senior Devvie

Projects: 1

Java games will probably rock someday...

 « Reply #3 - Posted 2005-03-16 12:22:05 »

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;
}

javatypo

Junior Devvie

 « Reply #4 - Posted 2005-03-16 12:49:45 »

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