Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    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.

  3. #3
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default

    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

  1. Sprite Movement
    By Curtiz in forum Java Gaming
    Replies: 1
    Last Post: 04-26-2010, 01:31 PM
  2. Snake game movement
    By BeerMonkey in forum New To Java
    Replies: 9
    Last Post: 11-27-2008, 12:48 PM
  3. Detecting user movement of a JFrame
    By dklett in forum AWT / Swing
    Replies: 4
    Last Post: 08-27-2008, 07:01 AM
  4. [SOLVED] File Movement/Manipulation
    By Leprechaun in forum New To Java
    Replies: 2
    Last Post: 04-23-2008, 12:39 AM
  5. Movement of balls
    By BlitzA in forum New To Java
    Replies: 8
    Last Post: 01-09-2008, 03:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •