gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > Plattformer mechanics (slopes)

#49342 - KonVex - Thu Jul 28, 2005 11:50 pm

I was thinking about doing a plattformer prototype, but there is one concept I still need to grasp before I tackle this project.

I'm familiar with basic collision-detection, yet I'm not sure how the interaction of the player-sprite with the environment could be done in an elegant manner. Basically these cases have to be considered:
    -Player is walking on a plane
    -Player is walking against a wall
    -Player is falling down if there is no ground beneath
    -Player is walking up a slope
    -Player is walking down a slope

    (jumping is omitted for now)

The first three cases are easy, but the slopes could get really tricky.
How are slopes usually implemented in plattformers?

#49346 - sajiimori - Fri Jul 29, 2005 12:18 am

Going up slopes is easy. When the player presses to the right, an initial rightward velocity is applied. Then the collision detection says that moving directly right will hit an upward slope, and it corrects the motion vector to move along the slope instead.

Going down slopes is harder because the collision system won't hit anything and you'll repeatedly fall off as if it's a cliff. My solution is to keep track of the slope of the surface you're standing on (if any) and adjust your initial velocity to be parallel to that.

After moving along the slope, check below the character to see if the ground is still there. If not, change the ground slope type to "none" to specify that you're in the air.

The essential components of my collision system are:

- A trace function that takes an object and a motion vector and returns the first thing they would hit by moving along the vector, if anything.

- A slide function that takes an object and a motion vector and returns the way they should move if they were to slide along the surface(s) they hit. This is implemented in terms of the trace function.

- A walk function that takes an object, a motion vector, and an initial ground slope. It alters the motion vector to be parallel to the ground and then calls slide. After sliding, it uses the trace function to see if there is any ground below the new position. It returns the resulting motion and a flag for whether the object is still on the ground.

- A walker object that maintains the current ground slope and a flag for whether the object is on the ground. If it's on the ground, it calls the walk function and updates its state based on the results. If it's in the air, it calls the slide function and adopts collided surfaces as the new ground when possible.