Jump to content
  • Advertisement
Sign in to follow this  
Staryon

SDL: How to detect collisions with the objects map (not with sprites)

This topic is 4831 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

I'm following SDL Marius' tutorial from here http://cone3d.gamedev.net/cgi-bin/index.pl?page=tutorials/gfxsdl/index and it's great. It gives you a simple technique for detecting collisions with other moving sprites. My problem is how to detect collisions with walls, water, fire and all those objects that I can put in my 2D game. What do you guys suggest? I'm not sure what are the common tecniques for doing this. I have done some research, but all what I have been able to find is Sprite collisions. Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Depends how the structure of your level is done. If you make it tile-based you can make a map inside a 2-dimensional array, like


array =
{{1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,1},
{1,0,2,0,0,0,0,0,1},
{1,0,0,0,0,0,3,0,1},
{1,1,1,1,1,1,1,1,1}}



You build your map by iterating through the array.


loop y to height
loop x to width
if map[x][y] is 1 then build wall



Map the actual screen position to the position of the player in the map.


x position of player in the map = x coordinate on screen / size of one tile
y position of player in the map = y ....




Here is the actual collision detection.


move right
if x and y position of player + one tile is 1
don't move
if x and y position of player + one tile is 2
burn
if x and y position of player + one tile is 3
get wet

move left
....




If you use more than one type of obstacle, then simply other digits into the array, for example 2 is fire, 3 is water and all those objects.


I hope I could help

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!