Tile Based Map Arrays for Collision Detection

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.

Thanks in advance!
Quote this message in a reply
Posts: 916
Joined: 2002.10
Post: #2
are you using a 2D array, or a 1D array?
Quote this message in a reply
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.
Quote this message in a reply
Post: #4
I'm using a 2D array.
Quote this message in a reply
Post: #5
I have my own ways of implementing this, but here is a good article to give you some insight.

Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Optimize the collision detection alaslipknot 1 5,722 May 12, 2013 08:02 PM
Last Post: SethWillits
  Collision detection tutorial ThemsAllTook 7 27,349 Nov 5, 2011 05:20 PM
Last Post: SethWillits
  Help with Collision Detection..(i'm almost there) carmine 1 6,518 Jun 29, 2011 12:33 PM
Last Post: ThemsAllTook
  Time Delta, collision detection mk12 19 23,413 Sep 8, 2010 06:40 PM
Last Post: AnotherJake
  Collision detection for pinball game johncmurphy 0 6,013 Sep 6, 2009 02:46 PM
Last Post: johncmurphy