AllFallD0wn
AllFallD0wn

Reputation: 551

XNA Collision Detection in Platformer

I've been working on a simple 2D platformer engine. So far I've got a sprite moving around (not yet animated), and 3 platforms for a "Jetpac" type game (old ZX Spectrum game - I'm sure if you google it you can play it in a flash box).

Now I'm onto the problem of implementing collision detection so the sprite can actually walk on the platforms. I think this will be the biggest job and then it's quite easy to continue. But how to implement collision detection with platforms?!

I have bounding boxes for all platforms, as well as the character, and later on there will be bounding boxes for enemy sprites, but that can be handled later. Basically, what is the easiest way to allow a sprite to walk on a platform, and not go through it using Bounding Box?

A bit more information:

Any help?

Upvotes: 4

Views: 9193

Answers (2)

Samuel Müller
Samuel Müller

Reputation: 1217

Basics for simple collision detection with rectangles:

Use the rectangle struct for your bounding boxes. You can then use the intersects method to compare your platform bounding boxes to your character bounding box.

Basics for maintaining performance:

If you have large levels with a lot of platforms your game might get slow if you compare all your platforms with the character. You can for example use the axis aligned bounding box technique to avoid this. The basics of AABB is that you sort your bounding boxes along the x and y axis and therefore will get an aproximate location of the BBs.

Last but not least:

Look at the platformer tutorial.

Upvotes: 4

Constantinius
Constantinius

Reputation: 35039

Have you considered the option of using a physics engine in your game? For a simple platformer this might be overkill, but you can easily add additional physical effects.

2D physics engines I can advise you to look into are:

  • Box2D, which has a native XNA port (see here)
  • Chipmunk, which once had a port to XNA, but it seems to have gone missing.

Upvotes: 0

Related Questions