## Tile Based Map Arrays for Collision Detection

paulkite
Unregistered

Post: #1
Hello all,

I'm pretty new to the C programming language in general. One of the problems I've come across is referencing my map array to base the collision detection for my sprites off of.

I understand that I need to be able to convert my array into coordinates and tag them as to whether or not they are walkable or not. I'm just not sure exactly how to do this, or really where to begin. If someone were to be able to post a small tutorial or post a few code snippets and explain how they relate to each other, I'd be grateful.

Just to explain where I am, I understand how to build my map array, read a tiled bitmap into my app and display my tiles according to my array. I just don't know where to go from there.

Moderator
Posts: 916
Joined: 2002.10
Post: #2
are you using a 2D array, or a 1D array?
Sage
Posts: 1,234
Joined: 2002.10
Post: #3
There are different ways to do this, but an easy way to think about it is to maintain two arrays. Let's say you have a 2D tile scrolling game like Super Mario. Your first array might be a 256x16 grid of block graphics. Each array value represents a graphic index that you use when drawing the blocks to the screen. A second array might be a 256x16 grid of block attributes. Each array value contains some bits that describe what that block in the map does, for example solid vs empty. Or acid, platform, door, etc.

Then if your player is at location [47, 12] in the map, you can just look at the attribute bits there (and immediately in front, behind, under etc) to figure out the movement logic.

Whether you use a 1D or 2D array for this data is a minor detail, having the compiler do the row multiply for you via [][] or doing it yourself (personally I stick with 1D arrays though.)

If your map interaction is very simple, you can also just use one array, with the attribute bits simply being the high bits of the graphic indices. Some NES games do that, but if you aren't careful you end up with duplicate graphics for "solid wall" and "secret passage wall". So the two-array approach is more generalized.
paulkite
Unregistered

Post: #4
I'm using a 2D array.
KiroNeem
Unregistered

Post: #5
I have my own ways of implementing this, but here is a good article to give you some insight.

http://greggman.com/mckids/programming_mc_kids.htm