Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  car motion  (Read 1644 times)
0 Members and 1 Guest are viewing this topic.
Offline javatypo

Junior Member





« Posted 2005-03-15 22: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

Senior Member


Projects: 1


Java games will probably rock someday...


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

Offline javatypo

Junior Member





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

this looks good, what does deltaTime represent? and where does it come from?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline c_lilian

Senior Member


Projects: 1


Java games will probably rock someday...


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

Offline javatypo

Junior Member





« Reply #4 - Posted 2005-03-16 13: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]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (7 views)
2014-07-30 21:08:39

Riven (20 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (29 views)
2014-07-23 21:07:15

Riven (30 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!