Skip to main content
GameDev.net gamedev.net
🔒 Locked

platformer collision detection jumping

Started by goodeddie Apr 15, 2013 at 2:29 PM 1 replies 3k views
Original Post
goodeddie
goodeddie

How do I handle platform games collision detection for jumping?

I have a method that tells you if the player collides with a block tile but I don't know how to manage jumping.

I know that if the player jumped up into a block, you just make the player fall down.

And if the player jumps up and touches the side of the block, the player still jumps up but doesn't move into the block.

KnolanCross
KnolanCross

It depends on how you are detecting collisions. But, assuming you can call a function that says tells you if the character is colliding against something, a simple (yet not so cheap, performancewise) way to do it is:

1) Save the (startingX, startingY) of the character.

2) Calculate the new position (newX, newY).

3) Check for collisions.

4) If no collision happened, finish, else try placing the char on (startingX, newY).

5) if no collision happened, go to 7, else try placing the char on (newX, startingY).

6) if no collision happened go to 7, else you must stop the jump force.

7) Adjust the jump force accordingly (in other words, you may not move on y or x axis anymore) and finish.

larsbutler
larsbutler

How do I handle platform games collision detection for jumping?

Collision detecting is rather easy; collision _response_ is the hard part, in my opinion. Just to clarify.

I have a method that tells you if the player collides with a block tile but I don't know how to manage jumping.

I know that if the player jumped up into a block, you just make the player fall down.

And if the player jumps up and touches the side of the block, the player still jumps up but doesn't move into the block.

Here is roughly the progression I followed while developing a 2D platformer. Maybe this will help.

- First, you need a way to model gravity.

- Apply the downward force of gravity to an object to make it fall.

- Put something in the way of the falling object (a platform).

- Detect the collision; the simplest method to start with is to do is to check for rectangle bounding box intersection.

- When a collision has occurred, the falling object is effectively _inside_ the platform; you need to get it out of the platform.

- Calculate how far inside the object is inside the platform and move it in the opposite direction so that it is just touching the platform. This is called "projection". (See http://www.metanetsoftware.com/technique/tutorialA.html.)

- Repeat

Collision for all basic cases (hitting walls, jumping into ceilings, falling onto floors) follow the same model. For a given axis (x or y),

- apply forces on the axis to move an object

- check for collisions

- respond to any collisions, one at a time (for example, when you something, a simple thing to do would be to 1) project out of the collision and 2) zero the object's velocity on that axis)

Hint: Do this for each axis individually. See http://gamedev.stackexchange.com/questions/30619/how-to-determine-collision-direction-between-two-rectangles/30627#30627.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.