I am fairly new to Java and after studying most of the basics I started making a 2D state change game with the help of online tutorials, the game is a simple car racing game with a overhead view kind of like pokemon, with simple left right forward backward and diagonal movements. I have got everything I need and have written most of the code but am stuck with collisions. I understand the basic concept of setting up collisions (or at least I think I do) and the way I have seen it done in the past is you set a rectangle around the car and a rectangle around the object on the map and set an if statement saying if intersects change direction to - or something similar to that, but am having trouble putting this into my code and am hoping some of the more experienced users could help me out, the code for my game is shown below, I have not posted things like the menu class and the main class just because it would overcrowd things, but if needed please tell me.

publicvoidupdate(GameContainergc, StateBasedGamesbg, intdelta)throwsSlickException{Inputinput = gc.getInput();//upif(input.isKeyDown(Input.KEY_UP)){bucky = movingUp;//changes the image to his backbuckyPositionY += 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 } }

//escapeif(input.isKeyDown(Input.KEY_ESCAPE)){quit=true; }//when the menu is upif(quit==true){//is the menu on the screenif(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 } }

P0jahn then link you had posted seems very interesting and helpful but as I am a noob with Java I am having a hard time understanding exactly what I have to do, I would like to use the first code on the website (which seems to be the simplest):

Am I correct in thinking object one and object two are the two rectangles that I need to make around my player and the house on the map? If so, how exactly do I make these rectangles and where do I place the code above and the rectangle code in my code(under the render method, update method or something else)?

Also, would this code work if I wanted to set up more than one collision, for example my map contains many houses, would this code work for that or would a Recatngle getBound() type method with a if (intersects) way work better? (I dont really know either of them two very well but types of code have seen a bit of both done before)

I am sorry for asking all these questions which may seem very easy to you, I am new with Java and might take a while to understand simple things. Thank you.

Then you'd need to create a Rect for your car and a Rect for your house. You have to position the rects to match the positions and sizes of the car / house, and then you'd check collisions with the method I gave you

Thank you for helping me Matheus23, I am sorry for asking noob questions but could you please tell me how I would continue after making the Rect class please, I think I have a basic Idea of what I need to do but keep messing up, so to start off, is this how I would make the rectangles:

1 2 3

publicRectanglegetBounds() {returnnewr1(x, y, width, height);//I would set this as r1 for the car and r2 for the house? }

So I would make 2 of the above code, 1 for the house and one for the car, but would I have to make a object of the Rect class to be able to do this? Also, under what section of code would I place this, would it be under my Render method or my Update method or something else?

The I would have to place your if statement into my code, but again,where would this be placed(in the render, update, other)?

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org