Results 1 to 6 of 6
  1. #1
    sur4j is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default Need help setting up collisions for a Java game

    I have been learning about 2D state change games and with help from tutorials have managed to create a simple game. Its not exactly a game yet, just a character that can move around a map but I need to set up the collisions as there is a house on my map which I do not want the player to walk through, I have set up boundaries for the outside of the map as they were simply saying change the speed to - if you get to this point on the x or y axis but am having trouble setting up collisions for objects like the house, I understand that you have to create a rectangle around the house and around the player and say if blockA intersects blockB do this but I just dont know how to put it into my code and am hoping some of the more experienced coders will be able to help me.

    The coordinates of the house are the following:

    Top left: -115, -100
    Top right: -360, -100
    Bottom right: -360, -280
    Bottom left: -115, -280

    The player is a 40x40 png image.

    The main code and menu code are made and are working, I dont think they will be needed for the collisions so I am just going to paste the play code, if needed please ask.

    Java Code:
    package javagame;
    
    import org.newdawn.slick.*;
    import org.newdawn.slick.state.*;
    
    public class Play extends BasicGameState{
    		
    	Animation bucky, movingUp, movingDown, movingLeft, movingRight, movingBL, movingBR, movingFL, movingFR;
    	Image worldMap;
    	boolean quit = false;//gives user to quit the game
    	int[] duration = {200, 200};//how long frame stays up for
    	float buckyPositionX = 0;
    	float buckyPositionY = 0;
    	float shiftX = buckyPositionX + 320;//keeps user in the middle of the screem
    	float shiftY = buckyPositionY + 160;//the numbers are half of the screen size
    		
    	public Play(int state){
    	}	
    	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
    	      worldMap = new Image("res/world.png");
    	      Image[] walkUp = {new Image("res/b.png"), new Image("res/b.png")}; //these are the images to be used in the "walkUp" animation
    	      Image[] walkDown = {new Image("res/f.png"), new Image("res/f.png")};
    	      Image[] walkLeft = {new Image("res/l.png"), new Image("res/l.png")};
    	      Image[] walkRight = {new Image("res/r.png"), new Image("res/r.png")};
    	      Image[] walkBL = {new Image("res/bl.png"), new Image("res/bl.png")};
    	      Image[] walkBR = {new Image("res/br.png"), new Image("res/br.png")};
    	      Image[] walkFL = {new Image("res/fl.png"), new Image("res/fl.png")};
    	      Image[] walkFR = {new Image("res/fr.png"), new Image("res/fr.png")};
    	
    	movingUp = new Animation(walkUp, duration, false);
    	movingDown = new Animation(walkDown, duration, false);	
    	movingLeft = new Animation(walkLeft, duration, false);	
    	movingRight = new Animation(walkRight, duration, false);
    	movingBL = new Animation(walkBL, duration, false);
    	movingBR = new Animation(walkBR, duration, false);
    	movingFL = new Animation(walkFL, duration, false);
    	movingFR = new Animation(walkFR, duration, false);
    	bucky = movingDown;//facing screen initially on startup
    	}
    	
    	
    	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
    	worldMap.draw(buckyPositionX, buckyPositionY);//position 0,0
    	bucky.draw(shiftX, shiftY);//makes him appear at center of map
    	g.drawString("Suraj's X: "+buckyPositionX+"\nSuraj's Y: "+buckyPositionY,400,20);//tells us the position
    	
    	if(quit==true){
    		g.drawString("Resume(R)", 250, 100);
    		g.drawString("Main(M)", 250, 150);
    		g.drawString("Quit Game(Q)", 250, 200);
    		if(quit==false){
    			g.clear();//wipe off everything from screen
    		}
    	}
    	}
    	
    	public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException{
    	Input input = gc.getInput();
    	//up
    	if(input.isKeyDown(Input.KEY_UP)){
    		bucky = movingUp;//changes the image to his back
    		buckyPositionY += 10;;//increase the Y coordinates of bucky (move him up)
    		if(buckyPositionY>162){//if I reach the top 
    			buckyPositionY -= 10;//stops any further movement in that direction
    		}
    	}
    	
    	//down
    	if(input.isKeyDown(Input.KEY_DOWN)){
    		bucky = movingDown;
    		buckyPositionY -= 10;
    		if(buckyPositionY<-600){
    			buckyPositionY += 10;//basically change the direction if + make -
    	}}
    	//left
    	if(input.isKeyDown(Input.KEY_LEFT)){
    		bucky = movingLeft;
    		buckyPositionX += 10;
    		if(buckyPositionX>324){
    			buckyPositionX -= 10;//delta * .1f
    	}}
    	//right
    	if(input.isKeyDown(Input.KEY_RIGHT)){
    		bucky = movingRight;
    		buckyPositionX -= 10;
    		if(buckyPositionX<-840){
    			buckyPositionX += 10;
    	}}
    	
    	
    	
    	
    	//2 key combos start here
    	if(input.isKeyDown(Input.KEY_RIGHT) && input.isKeyDown(Input.KEY_UP)){
    		bucky = movingBR;
    		buckyPositionX -= delta * .1f;
    		if(buckyPositionX<-840){
    			buckyPositionX += delta * .1f;
    			if(buckyPositionY>162){
    				buckyPositionY -= delta * .1f;
    			}}}
    	if(input.isKeyDown(Input.KEY_LEFT) && input.isKeyDown(Input.KEY_UP)){
    		bucky = movingBL;
    		buckyPositionX -= delta * .1f;
    		if(buckyPositionX>324){
    			buckyPositionX -= delta * .1f;
    			if(buckyPositionY>162){
    				buckyPositionY -= delta * .1f;
    			}}}
    	if(input.isKeyDown(Input.KEY_RIGHT) && input.isKeyDown(Input.KEY_DOWN)){
    		bucky = movingFR;
    		buckyPositionX -= delta * .1f;
    		if(buckyPositionY<-600){
    			buckyPositionY += delta * .1f;
    			if(buckyPositionX<-840){
    				buckyPositionX += delta * .1f;
    			}}}
    	if(input.isKeyDown(Input.KEY_LEFT) && input.isKeyDown(Input.KEY_DOWN)){
    		bucky = movingFL;
    		buckyPositionX -= delta * .1f;
    		if(buckyPositionY<-600){
    			buckyPositionY += delta * .1f;
    			if(buckyPositionX>324){
    				buckyPositionX -= delta * .1f;
    			}}}
    	
    	
    	
    	
    	
    	
    	
    	
    	//escape
    	if(input.isKeyDown(Input.KEY_ESCAPE)){
    		quit=true;
    	}
    	//when the menu is up
    	if(quit==true){//is the menu on the screen
    		if(input.isKeyDown(Input.KEY_R)){
    			quit = false;//resumes the game, makes menu dissapear
    		}
    		if(input.isKeyDown(Input.KEY_M)){
    			sbg.enterState(0);//takes you to the main menu
    		}
    		if(input.isKeyDown(Input.KEY_Q)){
    			System.exit(0);//quits the game
    		}
    	}
    	
    }
    	
    	public int getID(){
    		return 1;
    	}
    }
    Thank you in advance for any help and I am sorry I have made this message so long but I am very stuck and really require your help.
    Last edited by sur4j; 12-06-2012 at 10:25 PM.

  2. #2
    sur4j is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default Re: Need help setting up collisions for a Java game

    Some extra information about the game:

    It is a 2D game but not like Super mario but more like Pokemon type view, like a birds eye view.

    I know I have to set up rectangles like mentioned above and even know a bit of the code:

    public Rectangle getBounds(){
    return Rectangle(bukcyPositionX, buckyPositionY, 40, 40);
    }

    I think that should set the rectangle around the character but when I try put it into my code I keep getting errors and am not sure if I am putting it into the right part of the code.

    I dont know how to fix this and also how do I get the rectangle around the house and set it up so that the player cant get through the house(collision)?

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Need help setting up collisions for a Java game

    When you get errors you need to show us what they are, and where they occur.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    sur4j is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default Re: Need help setting up collisions for a Java game

    I have not posted the errors because I am not sure if the rectangle code I have put in is ever correct or required, it is just something I thought might work, but if required here is what happened:

    I put the above rectangle code in just under my variables and the error was telling me that I can not say:

    public Rectangle getBounds(){
    return Rectangle(bukcyPositionX, buckyPositionY, 40, 40);
    }

    But instead I have to say:

    public Rectangle getBounds(){
    return Rectangle(40, 40);
    }

    when I say the above code it doesnt give me any errors.
    What I think the code is saying is make a rectangle 40x40 and it dosent know where to place it because I have not given it any coordinates or variables to place it at(like I did above with the buckyPositonX, buckyPositionY) which is strange because how is it supposed to know to place the rectangle on my player.

    Maybe this is because the variables that I put in are floats and not int variables, but I really dont know and am very confused.

  5. #5
    sur4j is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default Re: Need help setting up collisions for a Java game

    I think I might have written too much and I dont think people will want to read through it all, the shortened down version:

    I want to set a collision on my map for my player (like a boundary) so that my player can not walk through objects like a house, the house coordinates are listed above.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,661
    Blog Entries
    7
    Rep Power
    21

    Default Re: Need help setting up collisions for a Java game

    Quote Originally Posted by sur4j View Post
    Maybe this is because the variables that I put in are floats and not int variables, but I really dont know and am very confused.
    Read the API documentation for the Rectangle class; you don't have to guess then: the documentation states that four ints are accepted by the constructor.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 3
    Last Post: 02-17-2011, 02:42 AM
  2. Solving Collisions in a car game
    By Rectal Exambot in forum Advanced Java
    Replies: 1
    Last Post: 09-29-2010, 02:13 PM
  3. Inelastic circle collisions
    By Atriamax in forum New To Java
    Replies: 9
    Last Post: 08-28-2010, 05:55 PM
  4. Check The Collisions
    By Luff in forum AWT / Swing
    Replies: 7
    Last Post: 06-23-2010, 12:30 PM
  5. How do you go about setting boundaries in a game?
    By robertbob in forum New To Java
    Replies: 6
    Last Post: 05-10-2010, 11:24 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
  •