XNA collision detection in platformer

I was working on a simple 2D platform engine. So far I have a sprite moving (not yet animated) and 3 platforms for a game like "Jetpac" (an old ZX Spectrum game - I’m sure that if you do, you can play it in flash box).

Now I am faced with the problem of implementing collision detection, so the sprite can actually go on platforms. I think this will be the biggest job, and then it’s pretty easy to continue. But how to implement platform collision detection ?!

I have bounding fields for all platforms, as well as a character, and later there will be limited fields for sprites, but this can be processed later. Basically, what is the easiest way to let a sprite walk on the platform, rather than go through it using the Bounding Box?

A bit more info:

  • The character element controls sprite drawing and sprite updating using the Vector2 position variable updated with the Vector2 motion variable.
  • The platform class controls the drawing of the platforms (there are 3 of them), so plat1, plat2 and plat3 are all types of platforms.
  • The platforms must be completely durable on all sides, but allow the sprite to walk.

Any help?

+4
source share
2 answers

Basics for easily detecting collisions with rectangles:

Use a rectangle struct for your bounding rectangles. You can then use the intersects method to compare your platform bounding box with the bounding box of your character.

Basics for maintaining performance:

If you have large levels with many platforms, your game can become slow if you compare all your platforms with a character. You can, for example, use the horizontal alignment method to avoid this. The core of AABB is that you sort your bounding fields along the x and y axis and therefore get an approximate location of the BBs.

And last but not least:

Check out the platformer tutorial .

+4
source

Have you considered using a physics engine in your game? For a simple platformer, this may be redundant, but you can easily add additional physical effects.

The 2D physics engines that I can advise are:

  • Box2D, which has its own XNA port (see here )
  • A chipmunk who once had an XNA port but seems to be missing.
0
source

All Articles