Image Collision Detection

Started by
3 comments, last by sebbit 14 years, 11 months ago
Hi everyone, I am working on a 2D RTS, and the map has a river running through the center. What is a good way to check for a collision so that a unit doesn't move onto the water? The river doesn't go in a straight line, so a rectangle wouldn't work very well, I don't think. Is there a way to take an image and check the colors of the image to prevent the unit from moving if it is on a certain color? For example: If this is the map: And this is the collision image: Is there a way to program it so that a unit can move only if they are on black and prevent them from moving onto white?
"All you have to decide is what to do with the time that is given to you." - Gandalf
Advertisement
Sorry, I forgot to mention that I'm using C++ and DirectX 9.
"All you have to decide is what to do with the time that is given to you." - Gandalf
You could use some simple algebra possibly to calculate the x and y around the white or get the area of the black and write something that states that if this area then can no longer move in that direction.
If you store the collision map as a bitmap you could read it into a data structure like a 2d boolean array. Then you just have to map the units world position and intended position to the array index and see if it's a legal move. I wouldn't recommend keeping the data stored as a texture for very long, as reading a specific pixel colour is just going to be a pain to debug as well as introducing performance issues.
If you want pixel-perfect collisions look into bitmask collision detection. But thats kind of slow.

What else you could do is to map a regular grid ontop of your bitmap (like 32x32 pixels cells or something like that) and for each cell store, if it's walkable or not.

If the grid based approach is to coarse for you, you could also mix both types. I.e. store per grid cell, if it's either fully walkable or partly or fully blocked. The you would only have to perform bitmask collision detection for cells that are partly blocked.

This topic is closed to new replies.

Advertisement