So I noticed that a lot of people are struggling with collision detection. I thought I might be able to help you a little.Today, 2013-12-12, I will post the part 1 of this tutorial.Tomorrow, 20130-12-13, I will post the part 2 of this tutorial.

Part 1 of this tutorial will explain how to do collision in an array of tiles.Part 2 of this tutorial will discuss the theory behind making collision with non-array collision. Only theory, no code.

If you want the code from this tutorial to work properly, you will need to add lwjgl.jar and lwjgl_util.jar files to your classpath.

The rendering code is really bad and deprecated and SHOULDN'T be used in any real game. (You can if it is enough for you)

So let's get started..

PART 1 - COLLISION IN AN ARRAY OF TILES!First things first, let's create a class for general stuff of the game. That includes making game window and running the game loop at 60UPS (Updates Per Second). Pretty basic..

Now I make a level class, which has an array of tiles. When filling the array, I make it so that 1/10 tiles would be rock tile, and the rest would be grass tile.Render method will just run through all the tiles and render them.Get tile method will return a tile with x, y in tile precision.

After making the level class, don't forget to make an object of it in your gameclass and add call render method from gameclass, so that you can actually render the level.

Here is a tile class!Tile is a class which will only store the color of the tile and a boolean whether the player can move on that tile or not.

GrassTile is a tile which has green color, and you're able to move on it.RockTile is a tile which has gray color, and you can't move on it.VoidTile is a tile which has black color, and you can't move on it.

At this point you should be able to see a grid of tiles. Some of them are gray. Next comes the player. He will have the update method, in which we will do the collision!I don't really want to explain stuff.. Just work it out yourself. It is not that hard really

Now make a player object in your GameClass and add render and tick calls for the player. You're done! Now you can move in a little level width ASDW!

So after we add level and player objects to GameClass, it looks like this. I also changed the size of the glOrtho call to make the level look bigger. From Display.getWidth(), Display.getHeight() changed to 360, 280.

Nice tutorial. I'd noticed the exact same thing and I myself am pondering on whether to post a tutorial on simple collision detection and response. It's less newb friendly but I might post my own later today or tomorrow because AFAIK, it's 100% accurate whereas this method isn't pixel perfect, especially if the sprite moves faster.

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