Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  car motion  (Read 1207 times)
0 Members and 2 Guests are viewing this topic.
Offline javatypo

Full Member
**

Posts: 143



« on: 2005-03-15 16: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 Smiley
Offline c_lilian

JGO Ninja
***

Posts: 643


Java games will probably rock someday...


« Reply #1 on: 2005-03-16 02: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

Former java games developer...
Offline javatypo

Full Member
**

Posts: 143



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

this looks good, what does deltaTime represent? and where does it come from?
Games published by our own members! Go get 'em!
Offline c_lilian

JGO Ninja
***

Posts: 643


Java games will probably rock someday...


« Reply #3 on: 2005-03-16 07: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;
}

Former java games developer...
Offline javatypo

Full Member
**

Posts: 143



« Reply #4 on: 2005-03-16 07:49:45 »

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

thanks for your help.
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.057 seconds with 19 queries.