# 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.

## 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 tiley 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 wetmove 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

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 9
• 9
• 14
• 12
• 10
• ### Forum Statistics

• Total Topics
633271
• Total Posts
3011162
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

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!