No history yet

Platform Physics and Gravity

Building a Better Jump

In a platformer, movement needs to feel right. Simply changing a sprite's y-coordinate up or down when a key is pressed feels stiff and unnatural. Players expect characters to have weight, to accelerate as they fall, and to follow a natural arc when they jump. To achieve this, we'll build a simple physics engine using variables to simulate gravity and momentum.

The core of our system is a variable we'll call y-velocity. This single variable will track our sprite's vertical speed. A positive y-velocity means the sprite is moving up, a negative value means it's moving down, and zero means it's stationary on a surface. Instead of directly setting the sprite's position, we'll constantly update its position based on this velocity.

The Constant Pull of Gravity

In the real world, gravity is a constant downward acceleration. We can mimic this by creating a gravity variable (a small negative number, like -1, works well) and repeatedly subtracting its value from our y-velocity.

Inside a forever loop, we first apply the velocity to our sprite's position, and then we apply gravity to the velocity. This creates a feedback loop: the longer the sprite falls, the faster its y-velocity becomes, making it accelerate downwards.

// Set this once at the start
set [gravity v] to (-1)

// Put this inside a forever loop
change y by (y-velocity)
change [y-velocity v] by (gravity)

With this code, your sprite will now realistically accelerate towards the bottom of the screen. The next step is to give it something to land on.

Landing on Solid Ground

A falling sprite isn't much use if it falls forever. We need to implement collision detection to check if our character has landed on a platform. We can do this with a simple if block that checks if the sprite is touching a specific color or another 'ground' sprite.

When a collision is detected, we need to counteract the effect of gravity. The simplest way is to set y-velocity to 0. This stops the downward motion instantly. We also need to move the sprite just out of the platform it touched to prevent it from getting stuck.

// This goes inside the forever loop
// after changing the y position
if <touching [ground v]?> then
  set [y-velocity v] to (0)
  // This loop nudges the sprite up until it's no longer inside the ground
  repeat until <not <touching [ground v]?>>
    change y by (1)
  end
end

Defying Gravity with a Jump

Now for the fun part: jumping. A jump is simply a sudden, large increase in y-velocity. When the player presses the jump key, we just need to set the y-velocity to a high positive number, like 15. The gravity code we already wrote will handle the rest, slowing the sprite's ascent and pulling it back down.

The key is to only allow a jump when the character is already on the ground. Otherwise, the player could jump infinitely in mid-air.

We can enforce this by adding a condition to our jump code. The sprite can only jump if the jump key is pressed and it is currently touching the ground. This combination of velocity, gravity, and conditional jumping forms the foundation of almost every platformer.

// This also goes inside the forever loop
if <<key [up arrow v] pressed?> and <touching [ground v]?>> then
  set [y-velocity v] to (15)
end
Quiz Questions 1/6

In a platformer game, what is the primary purpose of using a 'y-velocity' variable instead of directly changing the sprite's y-coordinate?

Quiz Questions 2/6

If a character in the game is falling downwards, what would you expect the value of its 'y-velocity' variable to be?

With these blocks in place, you have a robust system for vertical movement. You can tweak the gravity and jump y-velocity values to make your character feel heavy and sluggish or light and floaty.