It is currently Mon Oct 20, 2014 4:33 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Collision detection
PostPosted: Wed Sep 26, 2012 6:22 pm 
Offline
 Profile

Joined: Mon Jul 21, 2008 9:29 pm
Posts: 25
I've been playing around with Swift and have started writing a little platform game.

Does anyone have any tips on how to efficiently handle collision detection for the platforms? The platforms are currently stored as startX,startY,length in the level sets and can be horizontal or vertical.

I'm currently thinking of converting all the platforms into 4 co-ordinates (x1,y1,x2,y2) at the start of each level to speed up the comparisons. Other than that I haven't got many other ideas so any advice would be appreciated!

Thanks,

Rob


Top
 
 Post subject: Re: Collision detection
PostPosted: Thu Sep 27, 2012 9:28 am 
Offline
User avatar
 Profile

Joined: Mon Jan 07, 2008 6:46 pm
Posts: 380
Location: Málaga, Spain
The simple and common way, provided you can afford the memory requirements, is to build a temporary image of your map into a buffer when the level starts, and then do (x,y) lookups from there.

So, if your screen consists of 20 map tiles across by 20 map tiles down, you would reserve a 400 byte buffer for this and, upon starting the level, clear it and then iterate through all your platforms and write the 'tile index' of each one at buffer + x + y*20.

Then, during the game, you can look up a tile simply by the same method.

Chuckie Egg, amongst countless others I'm sure, does exactly this in order to read its map data quickly.

If you don't want to use this method, then you can always do it by hand. Here are a few possibilities:

1) If you're not so concerned about speed (e.g. because only the player needs to check collisions), then you could just iterate through every platform. I would store them as (top left x, top left y, width, height), and then the check for collision is just (in some pseudocode type thing):
Code:
function checkplatform(x, y):

    foreach platform p:
   
        dx = x - p.topleftx
        dy = y - p.toplefty

        if (dx >= 0 and dx < p.width and dy >= 0 and dy < p.height)
             return true
   
    return false
This would also allow you to have bigger rectangular blocks of tiles.

2) For something a bit faster, you could handle horizontal and vertical platforms separately, and use two tables: one containing 20 entries for x positions, in which a bit is set for each vertical platform which exists in that column; and another containing 20 entries for y positions, in which a bit is set for each horizontal platform which exists in that row. This would allow you to see quickly if there is anything in the row/column you are looking up, and give you all the indices of potential platforms. Then you would have a smaller set of platforms to do a more detailed check on, like the one above. Blurp does something similar for testing collisions between bullets and monsters (there's a bit more detail on my blog).

Hope that helps!
Rich


Top
 
 Post subject: Re: Collision detection
PostPosted: Thu Sep 27, 2012 1:04 pm 
Offline
 Profile

Joined: Mon Jul 21, 2008 9:29 pm
Posts: 25
Thanks Rich - that's exactly the sort of thing I wanted.

One thing I'm not clear on with the first approach is how to manage the position within tiles. Is it just a case of the horizontal platforms always being at the same vertical offset within a tile and accounting for it in the detection routine?

At the moment, I'm not using a tile based approach so the platforms can be at pretty much any position. However, there's nothing to stop me changing this.

Thanks again,

Rob


Top
 
 Post subject: Re: Collision detection
PostPosted: Thu Sep 27, 2012 4:26 pm 
Offline
User avatar
 Profile

Joined: Mon Jan 07, 2008 6:46 pm
Posts: 380
Location: Málaga, Spain
RobC wrote:
One thing I'm not clear on with the first approach is how to manage the position within tiles. Is it just a case of the horizontal platforms always being at the same vertical offset within a tile and accounting for it in the detection routine?

Not sure I understand the question. If you mean that the player can move pixel-by-pixel and meanwhile the tiles are aligned to 8 pixel units (or whatever), the normal way to do this is with two (or more) checks to see what's at different positions relative to the player, and only consider the space vacant if all of them return 'empty'.

e.g. suppose the player is 8x24 pixels, and each tile is 8x8 pixels. Also suppose the 'origin' of the player is at his top left. When we are falling, we need to check whether to move down or not, depending on whether there's a platform underneath. If all platforms are tile based, i.e. the underlying map is a 20x20 array in which each tile is 8x8 pixels, we can convert a pixel position to a tile position simply by dividing by 8 (and throwing away the remainder). So in the case of falling, we need to check 24 pixels underneath the player, i.e.

Code:
if Tile[x/8 + (y+24)/8] == empty
   fall


But this is not enough if the player is straddling two tiles, so we do two checks, one at the left edge of his bounding box, and another at the right, like this:

Code:
if Tile[x/8 + (y+24)/8] == empty and Tile[(x+7)/8 + (y+24)/8] == empty
    fall


That's vastly simplified, but hopefully you get the idea. For dealing with falling at different velocities, you have to check further than +24 from the player position, and also have to deal with resolving the collision so he lands perfectly on the platform, instead of within it or above it.

Is that any help?


Top
 
 Post subject: Re: Collision detection
PostPosted: Thu Sep 27, 2012 9:43 pm 
Offline
 Profile

Joined: Mon Jul 21, 2008 9:29 pm
Posts: 25
It was the bit about ensuring the falling player lands on top of the platform that I wasn't sure about.

I think I get the general idea now - you've given me a lot to think about so many thanks again!


Top
 
 Post subject: Re: Collision detection
PostPosted: Fri Sep 28, 2012 10:12 am 
Offline
User avatar
 Profile

Joined: Mon Jan 07, 2008 6:46 pm
Posts: 380
Location: Málaga, Spain
No worries, glad to have been some help!


Top
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: