jump to navigation

Sloped platform collision July 28, 2010

Posted by Jesse in : Game Development , trackback

Recently on the XNA Creator’s Club forums there have been several posts on how to handle collisions with a sloped platform in a 2d platformer. I wrote up a short description of the system I’m using:


How to determine if a player is about to land on a line segment:

What we need:

  1. Foot position of the player. (Bottom center of the player’s collision rectangle works well.)
  2. Line segment we’re checking for collisions with, specified by its end points A and B.
  3. Player’s velocity.

Let’s get to it:

  1. Is the player moving down? If not, we can stop here.
  2. Is the player’s expected position (foot position + velocity) within the horizontal region of the segment? If not, we can stop.
  3. Calculate the interpolated Y position on the line segment of the player’s X position both before and after the move.
  4. If the Y position before moving is above the segment, and the Y position after moving is below, then the player is landing.

And today I’ve put together a quick sample project to go with that. You’re welcome to do whatever you want with the code. (License – Ms-PL.) I hope it’s helpful. Feel free to hit me with any questions.

Comments»

1. Attila G - July 29, 2010

Thank you!

2. KFR - August 4, 2010

Hi

First i have to say…THANKS!!!!! xD
i’d been working in a code to get that my player (actually only a green cube) could walking around over nonuniform ground and yes, finally i got it but requires some operations more complex that with your example and the movement that i obtain is…not really rounded T T.

Looking your code i have thought that with it i can create, and in short time, an editor and in it select all the paths(vectors) that i want and adding one attribute more to the CollisionPath class, i will be able to differentiate between ground, walls, ceiling…

3. Jesse - August 4, 2010

I’m glad it was helpful. 🙂

For my next game, I expect I’ll be writing a new editor too. With the editor I’m currently using, I just use axis-aligned rectangles to represent walls and ceilings, and the path objects (slopes) are always passable from beneath.

Good luck with your game. Do you have a website or development blog?

4. KFR - August 4, 2010

For the moment i don’t have any web or blog but for after summer i have intention of prepare something, more than anything to help de spanish people that don’t know read in english or that they do not fully understand if have to do it. Also i will do all the entries in english.

5. Bazkur - October 10, 2010

Thank you! This will come in extremely handy. Now I just got to figure out how to use this against a randomly generated (and destructible) terrain

6. Jesse - October 11, 2010

@Bazkur: If you figure that out, you should write up a tutorial or some sample code. I know that alot of people would find that very useful. 🙂