I want to create a 2D game with a tile based map. My main problem is collision. If there is a tree in my way, how will I make my sprite not move through the tree?
-
2Might get better results asking on http://gamedev.stackexchange.com/. A good Java based book which covers what you're looking for is Developing Games in Java by David Brackeen. It's old, but the core details of creating a 2D tile based game with 2d collision is all there, the book has a website which demonstrates what depth the game goes to. The implementation could use a bit modernization though, but you can replace old deprecated methods and patterns as you go. GameDev could probably point you in the direction of some good tutorials instead too. – NESPowerGlove Mar 21 '14 at 20:51
2 Answers
Pseudo-code:
some_event() {
if (bullet.x == monster.x && bullet.y == monster.y) {
collision_occurs();
}
}
Of course the semantics such as which event will be fired and whether or not an event handler makes more sense (i.e.: collision_occurs()
when the x and y coordinates meet, rather than if
they meet while some_event()
is fired) depend on the game.
If you were to analyze this more you would notice that the bullet and monster aren't 1 pixel each, so it would look more like:
// While approaching from the left
if ((bullet.x + (bullet.radius)) >= (monster.x + (monster.radius)))
These fine details come after. Essentially you have moving objects and these objects share coordinates. When these coordinates, as properties of their representational objects, are near enough, a "collision" occurs and some methodology follows.

- 2,540
- 1
- 14
- 31
The general idea behind things like this (and I use the term general loosely) is that each 'object' in your game (whether it be a tree, car, w/e) is a group of polygons. Now, each of these respective polygons have a set of borders to them, which have a (likely) set or known size. So really, you could look at each of the object in your game as a set of hollow polygons. for example, take this 'hollow' car and wall I just whipped up (I'm an awful artist, so bear with me):
Now, even though in reality your car and wall will have nice details, and color filling, the only things you care about as a programmer handling collisions, is the border.
In the program, you'll have a handler attached to each of the objects in the game, something that listens for changes in the program. (A car moving, for example), and it can detect things like, did a collision happen? did the game end? did the car move past me and I should no longer be displayed. Etc. etc. since you care about collisions, you would pay extra special attention to when either: A) The car listener says it collided with something or B) the wall listener says something hit it. From there you could change the logic of the program to handle whatever you want happening (stopping the cars animation motion and adding damage, showing a game-over screen, whatever).
From there, really, the best way to go about it is just to try. Java has a ton of graphical frameworks, and each one has it's own nuances. Try them all! or pick one after some research, the world is your playground.
EDIT
One example of a collision of shapes is in Javafx, and it is handled like so: (credit to jewlesea for this piece of code.)
private void checkBounds(Shape block) {
boolean collisionDetected = false;
for (Shape static_bloc : nodes) {
if (static_bloc != block) {
static_bloc.setFill(Color.GREEN);
if (block.getBoundsInParent().intersects(static_bloc.getBoundsInParent())) {
collisionDetected = true;
}
}
}
if (collisionDetected) {
block.setFill(Color.BLUE);
} else {
block.setFill(Color.GREEN);
}
}

- 1,919
- 1
- 18
- 26
-
I havent created object for each tile since it would consume high memory. I have created the tile map using a 2D array and paint method and a two dimensional loop. The array stores - at what location a particular tile is to be placed. Is making object for each tile necessary? – anonymous Mar 21 '14 at 21:37
-
In your case, no. You could simply check the bounds of the moving object (your car) and see if any of the edges have reached one of the 'tiles'. Obviously, the more tiles you have, the smaller the grid, and the more exactly you can define a valid path. (you could overlay your existing grid with another 2d array that's filled with 1's and 0's. 0 for allowable-driving area, 1 for something you'll hit.). – WillBD Mar 21 '14 at 22:36