Jump to content
  • Advertisement
Sign in to follow this  
Alatar

Image Collision Detection

This topic is 3491 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!