Results 1 to 6 of 6
- 12-06-2012, 10:12 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 4
- Rep Power
- 0
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.
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.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; } }Last edited by sur4j; 12-06-2012 at 10:25 PM.
- 12-07-2012, 11:44 AM #2
Member
- Join Date
- Dec 2012
- Posts
- 4
- Rep Power
- 0
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)?
- 12-07-2012, 12:02 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,451
- Rep Power
- 16
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.
- 12-07-2012, 12:56 PM #4
Member
- Join Date
- Dec 2012
- Posts
- 4
- Rep Power
- 0
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.
- 12-08-2012, 12:40 PM #5
Member
- Join Date
- Dec 2012
- Posts
- 4
- Rep Power
- 0
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.
- 12-08-2012, 02:12 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,397
- Blog Entries
- 7
- Rep Power
- 17
Re: Need help setting up collisions for a Java game
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Help with setting up a GUI for a Rock, Paper, Scissors game
By thorobred in forum New To JavaReplies: 3Last Post: 02-17-2011, 02:42 AM -
Solving Collisions in a car game
By Rectal Exambot in forum Advanced JavaReplies: 1Last Post: 09-29-2010, 02:13 PM -
Inelastic circle collisions
By Atriamax in forum New To JavaReplies: 9Last Post: 08-28-2010, 05:55 PM -
Check The Collisions
By Luff in forum AWT / SwingReplies: 7Last Post: 06-23-2010, 12:30 PM -
How do you go about setting boundaries in a game?
By robertbob in forum New To JavaReplies: 6Last Post: 05-10-2010, 11:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks