2 Replies - 454 Views - Last Post: 24 October 2012 - 03:16 PM Rate Topic: -----

#1 hwoarang69  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 171
  • Joined: 23-October 12

2d java collsion help

Posted 23 October 2012 - 11:40 PM

so i have a player and i want to code collision, so player dont fall down ground.

i have 3 classes
Main.java
Player.java
Ground.java

in my Ground.java. i want to set collions between player and ground.
this code doesnt work, i am not sure what i am doing wrong. full code at bottom.


public void update(Main m, Player p)
	{
		int playerX = p.getX();    //get player x position
		int playerY = p.getY();    //get player y poistion
 
               //y = ground y poistion  | x = ground x poistion
               //height = ground height | width = ground width
 
		if(playerY > y && playerY < y + height)
		{
			if(playerX > x && playerX < x + width)
			{
				 
				p.setY(y);
				p.setX(X);          
			}
		}
	}


so i have a player, and i have platform's which is i am calling ground.
i want it so that my player can jump on top of platform's and if it hit the bottom of platform it should bounce back down and not go through it.

1st i want to set up collision point bottom of player feet, so it can jump top of platform.
2nd i want to set up collision point top of player head, so it does'nt go though platform when u jump from bottom of platform.

i am getting player x and y poition here
int playerX = p.getX(); //get player x position
int playerY = p.getY(); //get player y poistion


here i am seting up collion so that playerY and platform height.(y+height)
if(playerY > y && playerY < y + height)


same thing here but with X postion
if(playerX > x && playerX < x + width)


here i am setting player's y and x
p.setY(y);
p.setX(X);


i am not getting any error but the problem is that if i jump on top of my platform, my player go though it. but it should stand top of platform.
and if it jump from bottom of platform, than his head should hit the platform and bonce back down.

also in ur answer try to explain as much as u can but iam new to game programming,

Thanks





main.java
public class Main extends Applet implements Runnable, /*TimerListener,*/ KeyListener
{
	//Double buffering variables
	Image dbImage;
	Graphics dbGraphics;

	Thread thread;
	Timer timer;
	
	//Class variables
	Ground ground_class;
	Player player_class;
	Platform platform_class;
	Shoot shoot_class;
	Enemy  enemy_class;

	
	
	/*** init method ***/
	@Override
	public void init() 
	{
		setSize(900, 400);
		addKeyListener(this);
	}/*** end of init method ***/
	
	

	
	/*** start method ***/
	@Override
	public void start()
	{
		ground_class = new Ground(0, this.getHeight()-20,this.getSize().width, 20); //x, y, widith, height
		player_class = new Player(10, ground_class.getY()-30);  //x, y
		platform_class = new Platform();
		shoot_class = new Shoot();
		enemy_class = new Enemy(500, 345);   //x, y
		
		thread = new Thread(this); //start run method
		thread.start();
		//timer = new Timer(1000,this); //1 sec
		//timer.start();
		//void stop()
		//void setDelay(int delay)
		//boolean isRunning()
		//int getDelay
	}/*** end of start method ***/
	
	
	
	/*** run method ***/
	
	public void run()
	{
		while(true) //player moves
		{
			ground_class.update(this, player_class);                         //ground + player collions
			player_class.PLAYER_MOVE(this, ground_class);                                  //player moves
			platform_class.update(this, player_class);
			shoot_class.PLAYER_SHOOT_MOVE(this, player_class, shoot_class);  //player shoot bullet moves
			player_class.PLAYER_ENEMY_COLLISION(this, player_class, enemy_class);          //player + enemy collision
			
			repaint();
			try
			{
				Thread.sleep(10);
			}
			catch(InterruptedException e)
			{
				e.printStackTrace();
			}
		}
	}/*** end of run method ***/
	
	
	
	
	
	
	
	/*** update method ***/
	@Override
	public void update(Graphics g) 
	{
		if(dbImage == null) //if image is empty than create new image
		{
			dbImage = createImage(this.getSize().width, this.getSize().height);
			dbGraphics = dbImage.getGraphics();
		}
		dbGraphics.setColor(getBackground());  //set the background color
		dbGraphics.fillRect(0, 0, this.getSize().width, this.getSize().height);
		dbGraphics.setColor(getForeground());
		paint(dbGraphics);     //call paint method
		g.drawImage(dbImage, 0, 0, this);
	}/*** end of update method ***/
	
	
	
	
	
	
	/*** paint method ***/
	@Override
	public void paint(Graphics g) 
	{
		ground_class.paint(g);             //call paint method from Ground class  //draw ground than player on top
		player_class.paint(g);             //call paint method from Player classr 
		platform_class.paint(g);
		shoot_class.paint(g, player_class); //draw bullet
		if(enemy_class.isDEAD() == false)  //draw enemy if it's not dead
		{
			enemy_class.paint(g);
		}
		
	}/*** end of paint method ***/
	
	
	
	
	
	
	/*** stop method ***/
	@Override
	public void stop()
	{
		
	}/*** end of stop method ***/
	
	/*** destroy method ***/
	@Override
	public void destroy()
	{
		
	}/*** end of destroy method ***/
	
	
	
	
	
	

	
	
	
	
	
	
	/************** key *********************/
	@Override
	public void keyPressed(KeyEvent e)
	{
		int keys = e.getKeyCode();
		if(keys == KeyEvent.VK_RIGHT) 
		{
			player_class.hitRIGHT();
		}
		else if(keys == KeyEvent.VK_LEFT)
		{
			player_class.hitLEFT();
		}	
		else if (keys == KeyEvent.VK_UP)
		{
			player_class.hitJUMP(ground_class);
		}
		else if(keys == KeyEvent.VK_SPACE)
		{
			shoot_class.hitSHOOT();
		}
	}
	@Override
	public void keyReleased(KeyEvent e)
	{
		int keys = e.getKeyCode();
		if(keys == KeyEvent.VK_RIGHT) 
		{
			player_class.stopRIGHT();
		}
		else if(keys == KeyEvent.VK_LEFT)
		{
			player_class.stopLEFT();
		}	
		//else if (keys == KeyEvent.VK_UP)
		//{
			//player_class.stopJUMP();
		//}
		else if(keys == KeyEvent.VK_SPACE)
		{
			shoot_class.stopSHOOT();
		}
	}
	@Override
	public void keyTyped(KeyEvent e){}
	
	/************    MOUSE  **********/
	public void mousePressed(MouseEvent e){}
	public void mouseReleased(MouseEvent e){}
	public void mouseClicked(MouseEvent e){}
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}
}




ground.java
public class Ground 
{
	private int x;
	private int y;
	private int width;
	private int height;
	
	private static ImageIcon ground_image = new ImageIcon("Image/ground/ground.gif");
	
	
	/*** constructor Method ***/
	public Ground(int ix, int iy, int iw, int ih) 
	{
		x = ix;
		y = iy;
		width = iw;
		height = ih;
	}	
	
	
	/*** get/set method ***/
	public static Image getGIMAGE()  
	{ return ground_image.getImage(); }
	
	public int getX()
	{ return x; }
	public void setX(int value) 
	{ this.x =  value; }
	
	public int getY()
	{ return y; }
	public void setY(int value) 
	{ this.y =  value; }
	
	public int getWdth()
	{ return width; }
	public void setWidth(int value) 
	{ this.width =  value; }
	
	public int getHiehgt()
	{ return height; }
	public void setHeight(int value) 
	{ this.height =  value; }
	
	
	
	
	//player Collisions on ground
	//loop method
	public void update(Main m, Player p)
	{
		int playerX = p.getX();
		int playerY = p.getY();

		//playerx > x   so every thing after platform_top_left
		/*System.out.println("playerX="+playerX+"-"+"playerY="+playerY+"----x="+x+"-y="+y);
		if(playerY > y) //y+height  |  | left right of rect
		{
			p.setY(y-30);
		}*/
	}/*** end of update method ***/

		    
	
	/*** paint method ***/
	public void paint(Graphics g) 
	{
		g.drawImage(this.getGIMAGE(), x, y, width, height, null); 
	}/*** paint method ***/
}





player.java
public class Player 
{
	//Player movement variable
	private int sx = 10;
	private int sy = 352;
	private int x;     //current x
	private int y;     //current y
	private int dx = 2;    //speed of player
	
	private double dy = 6.0;  //go x up and down x
	private double gravity = 0.2;    
	
	
	private boolean dead = false;

	private boolean look_right = true;
	private boolean walk_right;
	private boolean walk_left;
	private boolean jump = false;
	private boolean jump_lock = false;
	private boolean fall = false;                  
	private boolean hitSPACE = false;
	
	/*** image variable ***/
	private static ImageIcon player_walk_right = new ImageIcon("Image/player/player_walk_right.gif");
	private static ImageIcon player_walk_left = new ImageIcon("Image/player/player_walk_left.gif");
	private static ImageIcon player_stand_right = new ImageIcon("Image/player/player_stand_right.gif");
	private static ImageIcon player_stand_left = new ImageIcon("Image/player/player_stand_left.gif");
	private static ImageIcon player_jump_right = new ImageIcon("Image/player/player_jump_right.gif");
	private static ImageIcon player_jump_left = new ImageIcon("Image/player/player_jump_left.gif");
	
	
	
	/*** constructor Method ***/
	public Player()
	{
	}
	/*** constructor Method2 ***/
	public Player(int ix, int iy) 
	{
		x = ix;
		y = iy;
	}	

		/*** get/set method ***/
		//need these to draw image's
		public static Image get_player_walk_right()  
		{ return player_walk_right.getImage(); }
		public static Image get_player_walk_left()  
		{ return player_walk_left.getImage(); }
		public static Image get_player_stand_right()  
		{ return player_stand_right.getImage(); }
		public static Image get_player_stand_left()  
		{ return player_stand_left.getImage(); }
		public static Image get_player_jump_right()  
		{ return player_jump_right.getImage(); }
		public static Image get_player_jump_left()  
		{ return player_jump_left.getImage(); }
		
		public boolean isDEAD()
		{ return dead; }
		public void setDEAD(boolean value) 
		{ this.dead =  value; }

		public int getX()
		{ return x; }
		public void setX(int value) 
		{ this.x =  value; }
		
		public int getY()
		{ return y; }
		public void setY(int value) 
		{ this.y =  value; }
		
		public int getDX()
		{ return dx; }
		public void setDX(int value) 
		{ this.dx =  value; }
		
		public double getDY()
		{ return dy; }
	    public void setDY(double value) 
		{ this.dy =  value; }

		

		/*** main key methods ***/
		/*** RIGHT ***/
		public void hitRIGHT()  //if user hit right button
		{ 
			walk_right = true; 
			look_right = true;
		}
		public void stopRIGHT() //if user let go of right button
		{
			walk_right = false;  //stop 
			look_right = true;
		}
		
		/*** LEFT ***/
		public void hitLEFT()  //move left
		{
			walk_left = true;
			look_right = false;
		}
		public void stopLEFT()
		{
			walk_left = false;  //stop
			look_right = false;
		}
		
		/*** JUMP ***/
		public void hitJUMP(Ground g)
		{
			if(y >= g.getY()-30) //if on ground
			{
				jump = true;
				dy = 6.0;	//reset
			}
		}

		

		
		
		/**************************/
		/****** LOOP METHODS ******/
		/**************************/
		/*** player move method ***/
		//loop method
		public void PLAYER_MOVE(Main m, Ground g) //player move's
		{
			//always same (Main m)
			//loop - collion check here 
			if(walk_right == true)
			{
					x += dx;
			}
			else if(walk_right == false)
			{
			}
			
			if(walk_left == true)  //walking left
			{
				if(x - dx < 0)  //cant go left
				{
					x = 0;
				}
				else
				{
					x -= dx;      //move left
				}
			}
			else if(walk_left == false)
			{
			}

			
			
			
			if(jump == true) 
			{
				dy -= gravity;
				y -= dy;	
				if(y >= g.getY()-29) //if on ground
				{
					jump = false;
				}
			}
		}/***end of update method ***/
		
		
		
		
	

		
		
		
		/*** player + enemy collion ***/
		public void PLAYER_ENEMY_COLLISION(Main m, Player p,Enemy e)
		{
			int enemyX = e.getX();
			int enemyY = e.getY();
			
			
		}
		/*** end of PlAYER_ENEMY_COLLISION METHOD ***/
		
		
		
		
		
		
		/*** paint method ***/
		public void paint(Graphics g)
		{
			if(jump == false)
			{
				if(look_right == true)
					{
						if(walk_right == true)
						{
							g.drawImage(this.get_player_walk_right(), x, y, null);
						}
						else if(walk_right == false)
						{
							g.drawImage(this.get_player_stand_right(), x, y, null);
						}
					}
				else if(look_right == false)
				{
					if(walk_left == true)
						{
							g.drawImage(this.get_player_walk_left(), x, y, null);
						}
					else if(walk_left == false)
					{
						g.drawImage(this.get_player_stand_left(), x, y, null);
					}
				}	
			}
			else if(jump == true)
			{
				if (look_right == true)
				{
					g.drawImage(this.get_player_jump_right(), x, y, null);
				}
				else if(look_right == false)
				{
					g.drawImage(this.get_player_jump_left(), x, y, null);
				}
			}
		}
}/*** end of class ***/


Is This A Good Question/Topic? 0
  • +

Replies To: 2d java collsion help

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1009
  • View blog
  • Posts: 4,209
  • Joined: 14-February 08

Re: 2d java collsion help

Posted 24 October 2012 - 12:50 PM

Basically you want to check if the players next position, when updating, collides with a block or platform. if it does then stay at the current position.

pseudo code:
update player()
{
      player old x = player x
      player old y = player y

      player x += player speed

      if(player x + player width > block x || player x < block x + block width)
      {
             player x = old x
      }

      player y += player speed

       if(player y + player height > block y || player y < block y + block height)
      {
             player y = old y
      }
}



This is fairly rudimentary but i'm sure you get the idea. It is definitely a good idea to split the checks up into x and y because otherwise you may have issues with getting stuck in the side of blocks from a check that returns true for x so then stops the y movement (if that makes sense).
Was This Post Helpful? 0
  • +
  • -

#3 hwoarang69  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 171
  • Joined: 23-October 12

Re: 2d java collsion help

Posted 24 October 2012 - 03:16 PM

thanks got it to working
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1