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:
- Character class controls drawing the sprite and updating the sprite using a Vector2 position variable, updated with a Vector2 motion variable.
- A Platform class controls the drawing of the platforms (there are 3), so plat1, plat2 an开发者_C百科d plat3 are all types of Platform.
- The platforms need to be fully solid on all sides, but allow the sprite to walk on.
Any help?
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.
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.
精彩评论