12
\$\begingroup\$

I'm building a 2D infinite runner. In the game, character jumps from pillar to pillar and pillars are randomly generated. I have this working. But how to determine if the character failed to jump on to a pillar and has fallen into the gap between pillars? A pillar is ordinary rectangular in shape.

\$\endgroup\$
1
  • \$\begingroup\$ OT: I though this question was posted on rpg.SE and I thought "What kind of rule exploit has been found now? Infinite running how?" \$\endgroup\$
    – o0'.
    Apr 16, 2015 at 20:08

5 Answers 5

20
\$\begingroup\$

Add a rectangular region at the bottom of the level, lower than the minimum allowable height of a platform. If the player has intersected this region, they have fallen off a platform.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ And make this rectangle a child of camera so it's moving with camera and is always present? That's so easy. Thanks. \$\endgroup\$
    – SanSolo
    Apr 15, 2015 at 14:57
  • \$\begingroup\$ Even if the player shouldn't be able to fall through the level, I think this is a good safeguard to have if he somehow manages to. \$\endgroup\$
    – mucaho
    Apr 16, 2015 at 0:00
  • 5
    \$\begingroup\$ If you're literally checking intersection, make sure that your character can't fall so fast as to clip through the surface (i.e., one from the character's entirely above, and the next frame entirely below). \$\endgroup\$
    – wchargin
    Apr 16, 2015 at 14:13
  • 2
    \$\begingroup\$ Yes, the rect should extend as far down as necessary to avoid this. Also just checking the height will work, but this gives you the flexibility of resizing the "kill region" depending on the level. \$\endgroup\$
    – mklingen
    Apr 16, 2015 at 19:07
  • 6
    \$\begingroup\$ To avoid "clipping" through the rectangle, why not a simple if (playerPos.x < minHeight)? \$\endgroup\$
    – Cole Tobin
    Apr 17, 2015 at 5:26
24
\$\begingroup\$

If your game only scrolls horizontally, you could check whether the vertical position of the character is below the lower edge of the stage.

If your game scrolls vertically as well (probably upward), you could check whether the player has fallen below the bottom edge of the view (i.e. the area of the stage that is onscreen, or in focus of the camera).

In general, the death condition is if the character fails to remain "in camera". By controlling the movement of your camera, you'll be able to tune your gameplay. For example, if in a vertical scrolling stage you only allow your camera to "rubberband" a little bit downward, while allowing full upward motion, you will penalize deep, offscreen falls.

\$\endgroup\$
6
  • \$\begingroup\$ Yes, that's an easy way too. I'd just need to find out lowest position.y that the character can be. \$\endgroup\$
    – SanSolo
    Apr 15, 2015 at 15:00
  • 1
    \$\begingroup\$ Correct! in case of a horizontal scroller you could simply check the vertical size of the stage in your map editor. In case of a vertical scroller, you could make sure the camera only moves upward. That way, if the character falls down, he'll end up below the view of the camera. You can then simply check if the character's vertical position falls below the bottom boundary of the view. \$\endgroup\$ Apr 15, 2015 at 15:24
  • \$\begingroup\$ @JellevanCampen: You don't have to limit the camera to upward movement necessarily. You can kill characters who fail to remain "in camera", or have the camera follow them around. \$\endgroup\$ Apr 15, 2015 at 22:19
  • \$\begingroup\$ I don't like this answer as much because it ties the camera directly to level boundaries. mklingen's answer works even if the bounds of the level are more than one screen height in size. The camera can move up and down to focus on the character, but there's no risk that the character will fall faster than the camera, stay in the level, but still collide with the boundary box attached directly to the camera. \$\endgroup\$
    – Kevin
    Apr 16, 2015 at 0:30
  • 2
    \$\begingroup\$ @Kevin: I agree for general games, but not for procedurally generated infinite scrollers. For example, in a vertical scrollers, you would have to either keep on spawning new boxes and despawn offscreen boxes, or you would have to keep updating the boxes as the camera moves around. As the death condition is directly tied to "failing to remain in camera", it makes sense to tie the condition to the camera. It also also more performant to do a camera boundary check, rather than to spawn new objects and resort to collision checking. \$\endgroup\$ Apr 17, 2015 at 11:24
2
\$\begingroup\$

Alternatively to checking the vertical coordinate, you could just check the vertical velocity. If the character is falling down faster than some threshold, conclude they're dead (even if there is some platform low enough to still reach, the impact would be fatal).

Of course, this only works if your game properly implements basic free-fall physics, i.e. the downwards velocity actually increases during the fall, as it does in reality (up until terminal velocity).

\$\endgroup\$
1
\$\begingroup\$

If you have height of pillars, you can find lowest existing, and compare it to vertical position of player. If he is below lowest pillar, there is no chance he can get back. You can limit this check only to pillars behind and in front of player.

Example: We have 3 pillars, with heights [10, 20, 40]. Player jumped is on pillar with height 40, he jumps forward to 20. He felt in gap between 40 and 20, he's now at height 15. That means he felt. For less strict check, wait till he is below 10.

\$\endgroup\$
1
  • \$\begingroup\$ All pillars are same size. It's the horizontal gap between two pillars that varies :) \$\endgroup\$
    – SanSolo
    Apr 18, 2015 at 8:19
0
\$\begingroup\$

Keep a count of how long a character has fallen. (i.e. how long they've been moving "down"). Once they've fallen more than a screenful of time, they by definition have fallen off the stage, yes?

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .