Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

In an 2D endless runner, what should happen when the player is running "too fast" up a slope and jumps?

For example, in a "normal" case:

    .O.
   . __..O_____
  . /
 . /
 O/
_/

If he is moving to the right slowly enough, he will jump upwards and land on the flat part of the surface. However, if he is moving too fast, the jump will have no effect as his forward motion will bring him back in contact with the slope before he can get high enough to pass over it. When the speed is sufficiently high, there will effectively be no jump.

     _________
    /
 .O/
 O/
_/

Are there any known ways to solve this issue? I know it's physically correct*, but are there techniques that other games use to overcome this in a reasonable manner?

As a last resort I'll have to just remove all slopes that are too slanted.

*If you constrain the player to never jumping backwards.

share|improve this question
    
It seems like the transition of flat ground to steep hill would slow the player down enough to make the jump. If they want to maintain their speed, they would jump before the slope. Just FYI, I think your question is kind of a "help me decide" type question, making it kind of opinion based. –  Byte56 Oct 23 '13 at 17:24
    
@Byte56 Perhaps some opinion may enter the mix, but I think this is still a good question. Some of the best questions on this site have an air of opinion about them. A good answer might point out shipped solutions. –  ktodisco Oct 23 '13 at 17:56
    
@ktodisco I agree, the answers can turn a borderline question into a great one. (Though the last of your examples is closed, rightly so). –  Byte56 Oct 23 '13 at 18:04
    
One possibility, which I think I've seen (I know that's not particularly convincing) might be to make a jump's direction relative to the angle of the ground underneath you- so jumping on a downward slope would gain you some forward momentum, but not jump as high, while jumping on an upward slope would send you higher with reduced forward momentum (think of the jump as pushing off the ground) –  Amadeus9 Oct 23 '13 at 19:29
    
@Amadeus9 that's a good point, but I think in this specific case I wouldn't apply. Reducing the speed when jumping will still end up creating a slope collision eventually, since the speed increases without limit. –  you786 Oct 23 '13 at 23:26

2 Answers 2

up vote 7 down vote accepted

Here are some examples of how games (runner or otherwise) have handled this situation. There is no "best" and you still have to decide which one is right for your game.

The physically "correct" way (vertical impulse)

To jump, you add a vertical impulse (red) to your movement velocity (green). The resulting velocity (blue) will always take you above the slope, so as long as your slope doesn't get too much steeper soon, your character will always jump above the slope.

tiny wings

The caveat is that if your character is racing up a steep slope, you can now jump up very high.

Perpendicular jumps (Sonic)

When jumping, your jump impulse is in a direction (red) perpendicular to the slope (green). You may remember this behaviour from the Sonic games.

Sonic

The caveat is that on very steep slopes, you often end up jumping backwards.

No jumping on slopes

Maybe you decide that slopes are kind of special and it doesn't make sense to jump on them. For example, stairs are a special kind of slope, which in some games you can't jump on at all.

castlevania 3

The caveat is that, whenever you take away control from the player like this, you need to clearly communicate the expectations, otherwise the player can get frustrated. For example, Castlevania uses a special "stair climbing" animation to make it clear that it's a special kind of motion.

No running on steep slopes

Another option is to make it impossible to climb slopes that are too steep - players slide down as if it's a wall-like obstruction. Since they are falling or sliding down, it's clear that they can't jump during this time either.

No steep (up)slopes

You could take the easy way out and entirely remove slopes that will give you such problems. This is not as bad an idea as it sounds, because walking up slopes is no fun; walking down slopes is way more exciting!

ski safari

No slopes (the Mu answer)

When it comes to gameplay, I'm a fan of minimalist design - as the saying goes, "perfection is finally attained not when there is no longer anything to add, but when there is no longer anything to take away". Do slopes serve a useful gameplay purpose, or do they just look good - i.e. are they fun? After all, it's perfectly fine to have a runner with no slopes at all.

canabalt

share|improve this answer
    
Thanks for the detailed answer. For the record, I ended up removing all up slopes that are too steep. –  you786 Nov 3 '13 at 17:29

In fact, it seems that you think in terms of speed only but if you think in terms of forces and accelerations, you'll see that pressing the jump button does have an effect and lifts your character up. In fact, there are two cases:

  • The character is already moving along the slope, slowing down due to the slope. If you press the jump button, physically, a jump must occur, although a very short-lived one if the character's speed is high. That's because the applied force adds an amount of vertical speed that makes the character leave the ground for a while.
  • The character is exactly at the intersection between the slope and the flat ground. Then there is no jump, for the reasons you give. But only then. And that's a very particular case. A frame-perfect one, supposing there's a chance that a frame lands at that point in time.

If your problem is what happens when the character lands after the very short jump, I suggest a slide with friction until

  • the character reaches the top of the slope
  • the character does not have sufficient speed to reach the top (friction takes it, he cannot run up, etc) and he slides backwards and has to jump from the bottom of the slope.

Slide

Apply this friction function in addition to the one that applies weight acceleration along the slope direction to your character.

// if this is a new collision that just happened at this frame
// OR the character is still colliding
if (colliding)
{
    // compute normal to the slope (if your slope.vec goes from the left end to right end)
    // By the way your slope.direction should already be normalized, so no need
    // for a renormalization after a simple rotation, but whatev
    Vec2D slopeNormal = Vec2D(-slope.direction[y], slope.direction[x]).normalized();

    // Keep in character.speed only the part of character.speed which is
    // along the slope direction. This should be done only if this is the first
    // frame of the collision.
    float alpha = dotProduct(character.speed, slope.direction);
    if (thisIsANewCollision)
    {
        character.speed = alpha * slope.direction;
    }

    float beta = (alpha > 0 ? -1.0 : 1.0);
    // Subtract from character.speed a vector whose direction is opposite that
    // of character.speed, using Coulomb's laws of friction
    // (very simple model indeed).
    character.speed -=
        slope.friction                               // base friction factor
        * dotProduct(character.weight, -slopeNormal) // effect of the weight
        * beta * slope.direction
        * dt; // dt is your frame duration
}

You can optimize, of course. Don't compute the normal every time, do += instead of -= and remove the minus sign in the dot product (that was just for ease of understanding), etc.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.