So, I'm making a platform-style game, and I'm not sure where to start with collision programming. I use four variables for the player: an x and y for where it is drawn, and another set for the opposite corner of the sprite. In a sense, it looks kind of like this:

For every element or other character in the game, I plan to use the same types of variables to define their bounding box. I suppose I could easily create a rectangle around the sprite, and use the build in collision methods, but based on the nature of the game, I need to know whether the player is landing on something, hitting the bottom of something when jumping, or colliding with something to the left or right.

Edit: An example of something I would need to be able to display: A character is walking left and passes through (in front) of a tree. He collides with a wall and stops because he cant pass it. He jumps on top of the wall. Even higher up there is a platform. He jumps through the bottom of the platform, and lands safely on top. Note: I know how to define these types of events, I'm more looking for a way to define the collisions from all angles based on the variables I use for the four corners of a sprite.
I'd like to try to avoid putting everything into a collision array if possible, because the idea seems too abstract to me, and because there will be a lot of elements of various sizes. This is for a school project, so I'd appreciate any and all responses with haste!

May 16th, 2011, 09:27 PM

Norm

Re: Need help with collision in a game!

Looks like an ambitious project.

May 17th, 2011, 07:47 AM

Skyhigh32

Re: Need help with collision in a game!

It is. It's a final project for school. Any ideas?

May 17th, 2011, 08:07 AM

KevinWorkman

Re: Need help with collision in a game!

...I don't see a question.

May 17th, 2011, 09:07 AM

Norm

Re: Need help with collision in a game!

Quote:

Any ideas?

Just standard stuff. Break the project up into small parts. Work out techniques with small programs and then merge them into the final program.

May 18th, 2011, 12:12 AM

helloworld922

Re: Need help with collision in a game!

If you're sticking in the 2d world, a simple grid partition works really well. Divide you're game world into a 2d grid with buckets at each grid box for holding items which are at least partially in that grid box. The size of the buckets is highly application specific, experiment with a combination of small, medium, and large bucket sizes to determine what will work best for you (or create a heuristic for having your game generate bucket sizes for you). Note that it's easiest if all grid boxes are the same size and evenly spaced.

Then place all your world items into the grid (items which overlap multiple buckets are placed in all of those buckets they lie at least partially in). Once you've got the buckets filled, simply use spacial indexing to translate which buckets a particular item of interest (for example, a player) is in, then search those buckets for items and check collision detection between those items and your original item of interest. Once you've found a collision (or possibly multiple collisions, you might be able to handle each collision independently), you can handle specific states here.

For example, if you're character is beneath a platform and you want him to jump up through the platform and land on it, you can detect that you're character has collided with the platform, but he still has an upward velocity so the collision is ignored. However, on the way down his velocity will be directed downwards, and the collision can be registered as landing on the platform. No vertical velocity component (i.e. standing/running on the platform) can probably be handled as if he was landing on the platform.

Alternatively, you could try using a hierarchical system (KD-tree, quad tree, etc.). These systems generalize to higher dimensions really well, though for most 2D collision detection I think they're overkill.