Sunday 19 September 2010

Collision detection

Making a map with lots of objects can cause problems in terms of detecting collision between scenery and the player character. Each update, every object on the map must be checked to see if the player is about to walk into it or not. If you're on a world map, that's a lot of things to check every time the player moves just one pixel! So I figure the easiest way is to have something that I'm going to dub a "collision map".

Take this small map for example (made in the map maker! woo!)

This map is tiny (10x10 tiles) and yet there are 7 objects in this scene that will need to be checked for collisions:

The tree
The tree stump
The archway
The building
Three separate rock objects moved close together to give the impression of a rock cluster

The idea behind the collision map is to allow a single check to be performed for collisions rather than one per object. To do this I'll need something that looks like this:



Black regions indicate solid areas while white regions indicate passable areas. As the game runs I can check the player position and the direction they are moving and then check to see if they will end up in a black region. One check that encompasses all objects on the map. Handy! Combine the collision checking with the graphics side of things and no longer are we restricted to checking "collision bounds" (rectangles). We can have any shape we want.


I'll have to add this functionality into the map component, so give me a few days to get it sorted =)

3 comments:

  1. Top of the arch has areas of extra white that shouldn't be there. Is it just to do with the seperate image of the arch? Though, nice arch.

    ReplyDelete
  2. Do the tree trunks/stump need to be so precise. i'm sure a square would do. As it wouldn't make a lot of difference. unless you had to climb up it

    ReplyDelete
  3. it's just where I've done a shoddy job of editing the background of the tilesets, can change em on the fly tho ;)

    And yeh the tree stumps could just be a square, but it was just an example of not needing to stick a big sqaure around everything which is what most people tend to do

    ReplyDelete