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

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

## 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 on other sites
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 on other sites
T*H*A*N*K*S!!!!!
I really really appreciate your help.
It's exactly what I need.

Regards

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634107
• Total Posts
3015553
×