Skip to main content
gamedev.net
🔒 Locked

Sprite Collision Methods

Started by MrDoomMaster Sep 15, 2005 at 8:51 PM 6 replies 1k views
Original Post
MrDoomMaster
MrDoomMaster
I have a side scroller 2D game (much like super mario world on the SNES). I need a reliable way to detect collision with the ground and wall. My level is made up of 32x32 tiles. Some tiles represent the "air", and some represent different angles and shapes of the surface. However, all surfaces and walls are orthogonal, none are at 45 degrees or anything weird like that. They're either horizontal or vertical. Nothing else. My sprites, however, may be *any* size. I want a method that is clean to code and is very fast. I would like to know how the collision was done in Super Mario World. It seems rather nice. Thanks for any replies!
njpaul
njpaul
One way would be this:

1) Find out what tile(s) the sprite is currently in.

2) Check the tile(s) from step 1 to see if it they can be collided with (something like a bool would work)

Of course, this isn't pixel-perfect collision, but it could be made into it by checking the alpha of the sprite and tile(s) in question.
MrDoomMaster
MrDoomMaster
You have to assume frame rates from 1 FPS to 200 FPS. It is very well possible that a large lag spike could cause your sprite's next projected movement to SKIP past a tile that is meant to be the ground. If you're only checking the present tile, it makes it rather difficult to know where the ground is AHEAD of time.

Perhaps some form of masking? Perhaps some form of line collision is in demand here.

It would be so much easier of Super Mario World was open source... :(
k0ns0l3
k0ns0l3
I know that the original super mario bros. consisted of worlds formed by rectangles and the would only check if certain points of Mario would be inside of those boxes and would resolve the colision depending of the movement and velocity.

Now, I don't remember where I've seen it... :(
timw
timw
most nintedo games function this way, the engin was inharently tiled. memmmmmmmmmmories....
rmdeboer82
rmdeboer82
you can also use collision detection by using 32-bit masks...
Be creative, don't copy...Greets from Holland!
MrDoomMaster
MrDoomMaster
Quote:
Original post by rm82
you can also use collision detection by using 32-bit masks...


Masking is what I am beginning to get interested in. I have no idea how I'd do this though. If anyone could explain that would be great. Thanks!
rmdeboer82
rmdeboer82
Quote:
Original post by MrDoomMaster
Masking is what I am beginning to get interested in. I have no idea how I'd do this though. If anyone could explain that would be great. Thanks!


The idea behind it is to use a 1 in the bit pattern in case a pixel is non-transparant and 0 for that case it's transparant.

Getting the bitmask can be achieved by using the logical operators AND & OR for each pixel. This should be done at initialization to avoid calculating at runtime.

Once you've got two bit masks for objects you can AND them to detect if they collided (of course you should take in consideration their positions). Please note that when you let them move more than 1 unit/frame it's possible not detecting them colliding (for example in case of 2 units/frame: 0101 and 1010 will become 0000) so you should take care of that also.

The best way is to implement it by using assembly.
Be creative, don't copy...Greets from Holland!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.