I'm working on a little 2D side scrolling platformer with a couple of friends. The issue I'm having is the level is completely defined by points. They are just a bunch of points which when I read in I turn into line segments, thus creating the floor in a sense. I've been having a lot of issues with this because I'm trying to make appropriate collision detection and reactions but I've never really approached Rect (which my characters sprite is defined as) to a Line (which would represent the section of the floor) collisions. Now I've already tried a few things and what I put together was I treated the character's Rect as if it were 4 Lines and did 4 Line to Line collisions then finding the points where the line's collided I then used some vector math to push out the line from the box. Now as far as that goes it works pretty well IF I'm colliding with one line at a time running into it straight and not on the end points. I'm really wondering if this was a good way to approach it at all really. What I did for the reaction was make a vector from the center of the character's Rect to the two points then I added them together to make a combined vector then divided it by 2 (so it wasn't to long) and then pushed out by that, which works, some of the time. I can't get it to work without 2 points of intersection.

My questions are: Should I instead go for some other sort of collision detection and reactions? or Given your character's Rect and points of intersection on those lines how would you handle the reaction for this?


Ofen, in 2d games, the collision detection is made using pythagora's theorem to calculate distance between 2 points.

You could check if the character is on a platform by calculating the distance from left and right side of the platform with the bottom center of the character.

If the distance from left and right side of the platform is smaller than the length of the platform, it means your character is on the platform or close to it. You would also probably have to check if the character is above or under it. You will probably have to set some thresold and adjust the y position of your character for it to be exactly on the platform when the character is falling on it.

To calculate pythagora's theorem, you use the following formula:

distance = sqrt(abs(x1-x2) * abs(x1-x2) + abs(y1-y2) * abs(y1-y2))

where x1,y1 is the first point and x2,y2 is the second point.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy