Archived

This topic is now archived and is closed to further replies.

Tile Based Collision Detection

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

Ive nearly completed a rather simplistic top-down, tile-based, 2D, 2-player shooter. My current problem though is the players and bullets can travel thru walls. I currently am using a 1024x768 resolution with a 2d array[32][24] full of 1s (Ground) and 2s (Wall) and the tiles themselves are 32x32. As I said before Im having a porblem stopping the player walking through the walls, Ive tried a number of ideas... unsuccesfully. Can anyone out there give me some ideas, point me to a website or tell me how to solve this problem. Crash, We Must Move Forwards NOT Backwards, Sideways NOT Forwards And Always Twirling Twirling Towards Success

Share this post


Link to post
Share on other sites
The way I have been doing it and it would work with you array for your map is like this.

Since your tiles are 32x32 I am assuming your character's width is 32. What you would do is, get the x and y coordinate of where the player currently is on the map, in map cooridinates, not screen cooridinates. Then divide those cooridinates by 32 to get where it would be in the array. Then if the player hits up, you can then check whether the tile above him is a wall or not by just subtracting 1 to his current y tile location. You can repeat this for the other directions.

I hope this made some sense.

Edited by - Lunatic Raven on June 27, 2001 2:58:22 PM

Share this post


Link to post
Share on other sites
What Lunatic Raven said is correct, but here are a few additions:

1. The player does not have to be 32x32 - pick an offset that you want to do collision detection from (maybe his feet?) and calculate from there - just divide x,y by 32 and check that in the array

2. Instead of adding 1 to the player''s x,y, position, add the players delta_x (or x_velocity) and the delta_y, so they don''t "Jump" inside a tile if they are moving fast. This also means you don''t have to worry about direction since the velocity vector encompasses direction.

3. Just a little thing.. I would suggest have your ground as 0 not 1, cos then you can just go
if ( !array[x][y] ) blah blah blah
not sure if this is speedier or anything, but personally I like the look of it better .

4. You probably aren''t doing this, but don''t bother storing the player''s tile coordinates and then his offset in that tile, it is a waste of time. More often than not you just want the world coordinates, and if you need the offset then just use the modulo % operator.


er.. yes. A totally biased method which most likely has more holes than your current method.

** takes a bow **

Share this post


Link to post
Share on other sites
Thanks that worked, I had been trying something like that but instead of adding the velocity I added 1. Oh well Ive learnt now. Thanks alot.

Crash,



We Must Move Forwards NOT Backwards, Sideways NOT Forwards And Always Twirling Twirling Towards Success

Share this post


Link to post
Share on other sites