Results 1 to 3 of 3
Thread: Car Game - Handling movement
- 08-24-2010, 01:41 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 16
- Rep Power
- 0
Car Game - Handling movement
Hello, (Yes I joined this forum so I could ask questions, sorry guys)
I am currently making a car based java game and was wondering how to handle movements? It is topdown and I am not sure exactly how to do it.
Currently I have acceleration and angle [as doubles], W is accelerate, S is break (slows the acceleration and if held when you have no acceleration you go back slowly [reverse]) and D/A changes angles.
How can I make it so based on the angle, he moves relative to the screen? Say if you hold W you move down the Y axis, but pressing the A key makes you move both up the X axis and down the Y axis. So the car goes to the bottom right of the screen?
This is what I currently have, its pretty fail I have just been testing things out and I know it doesn't work and I know why, I just don't know how to make it awesome.
Java Code:public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_W) { accelerate = true; } else if (e.getKeyCode() == KeyEvent.VK_S) { brake = true; } else if (e.getKeyCode() == KeyEvent.VK_A) { turnLeft = true; } else if (e.getKeyCode() == KeyEvent.VK_D) { turnRight = true; } } @Override public void keyReleased(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_W) { accelerate = false; } else if (e.getKeyCode() == KeyEvent.VK_S) { brake = false; } else if (e.getKeyCode() == KeyEvent.VK_A) { turnLeft = false; } else if (e.getKeyCode() == KeyEvent.VK_D) { turnRight = false; } } @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub } @Override void draw(Graphics g) { Graphics2D g2 = (Graphics2D) g; Rectangle car = new Rectangle((int) x,(int) y,10,10); g2.draw(car); } @Override void step() { if (accelerate) { acceleration += 1.5; } else if (brake) { acceleration -= 0.5; } if (angle == 3) { yVel = acceleration; xVel = 0.0; } else if (angle == 5) { yVel = 0.0; xVel = acceleration; } else if (angle == 1) { xVel = -acceleration; yVel = 0.0; } x += xVel; y += yVel;
- 08-24-2010, 02:12 AM #2
How are you changing the position of the car?
You can change the car's position (x,y) when a key is pressed by changing the value of the car's position(the x and/or y) and then calling repaint() for the new position to be shown.
- 08-24-2010, 02:23 AM #3
Member
- Join Date
- Aug 2010
- Posts
- 16
- Rep Power
- 0
I can make it so A makes the care move Left and W fowards. But since it is a car I would like W to go fowards and depending on the value of the angle he would go in that direction. I know how to redraw/repaint but I don't know how to calculate the angle, or the new x and y for the next draw/paint.
For example If I pressed D for a second, I want to go maybe y+= 1 and x+= 3 because he is now facing right instead of straight ahead, but I don't know how to calculate that.
Edit: I think I solved
x += acel(sin angle);
y += acel(sin(90-angle));
Just had to use good old sin rules...Last edited by Rectal Exambot; 08-24-2010 at 02:45 AM.
Similar Threads
-
Sprite Movement
By Curtiz in forum Java GamingReplies: 1Last Post: 04-26-2010, 01:31 PM -
Snake game movement
By BeerMonkey in forum New To JavaReplies: 9Last Post: 11-27-2008, 12:48 PM -
Detecting user movement of a JFrame
By dklett in forum AWT / SwingReplies: 4Last Post: 08-27-2008, 07:01 AM -
[SOLVED] File Movement/Manipulation
By Leprechaun in forum New To JavaReplies: 2Last Post: 04-23-2008, 12:39 AM -
Movement of balls
By BlitzA in forum New To JavaReplies: 8Last Post: 01-09-2008, 03:30 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks