Understanding Displacement with Constant Acceleration in Game Development With Godot

Alex Tavares
CapiVarious
Published in
5 min readApr 17, 2024

When diving into game development, one of the fundamental physics concepts you’ll encounter is displacement under constant acceleration. It’s a concept that might sound complex, but it’s crucial for creating realistic movements in your games. Let’s explore what this means in a simple and clear way, using the popular game engine, Godot.

Key Concepts Explained Simply

  1. Acceleration: Imagine you’re in a car that starts speeding up; the rate at which your speed increases is called acceleration. In games, this could be a character starting to run or a ball beginning to roll downhill.
  2. Velocity: This is just a fancy word for speed in a specific direction. If you throw a ball straight up, its speed and direction determine its velocity.
  3. Displacement: This is how far your character or object moves from its starting point. Unlike distance, displacement cares about direction — moving forward 10 meters and then back 10 meters means your displacement is zero because you ended up where you started.

Basic Formulas

  • Velocity Formula: v = v0 + a * t
  • Displacement Formula: s = v0 * t + 1/2 * a * t²

Where:

  • v is the final velocity.
  • v0 is the initial velocity (speed at the start).
  • a is the acceleration.
  • t is the time in seconds.
  • s is the displacement.

Practical Example: Calculating Displacement in Godot

Let’s calculate how far a character in a Godot game moves in one second if it starts from rest and speeds up at a constant rate (constant acceleration).

  1. Scenario:
  • The character is still at the start (initial velocity v0 ​= 0).
  • It accelerates at 1500 pixels per second squared (a = 1500 px/s²).
  • We want to know how far it moves in one second (time t = 1s).

2. Using the displacement formula:

s = 0 × 1 + 1/2 ​× 1500 × 1² =750pixels

This tells us the character will have moved 750 pixels after one second.

Frame-by-Frame Calculation in Godot

In a real-time game environment like Godot, physics is processed in frames. Here’s how velocity and displacement are updated frame-by-frame:

  • Velocity Update: Each frame, update the character’s velocity based on player input and acceleration using the formula velocity += acceleration × input × delta.
  • Move and Slide: Use the move_and_slide() function to apply this velocity, updating the character’s position and handling collisions.

Further Explanation: Frame-by-Frame Calculations

Given an acceleration of 1500 px/s², a frame rate of 60 FPS, and using the physics formulas for velocity and displacement, we can calculate the values for each frame as follows. Let’s assume the initial velocity (v0) is 0 (starting from rest), and each frame lasts approximately Δt = 1/60 seconds or about 0.01667 seconds.

Frame Calculations:

  • Frame 1:

Velocity Update: v = 0 + 1500 × 0.01667 = 25px/s

Displacement Update: s1 = 0 + 1/2 × 1500 × (0.01667)² ≈ 0.208px

  • Frame 2:

Velocity Update: v = 25 + 1500 × 0.01667 = 50px/s

Displacement Update: s2 = 25 × 0.01667 + 1/2 × 1500 × (0.01667)² ≈ 0.625px

Cumulative Displacement after Frame 2: S2 = s1 + s2 ≈ 0.833px

  • Frame 3:

Velocity Update: v = 50 + 1500 × 0.01667 = 75px/s

Displacement Update: s3 = 50 × 0.01667 + 1/2 × 1500 × (0.01667)² ≈ 1.25px

Cumulative Displacement after Frame 3: S3 = S2 + s3 ≈ 2.083px

  • Frame 4:

Velocity Update: v = 75 + 1500 × 0.01667 = 100px/s

Displacement Update: s4 = 75 × 0.01667 + 1/2 × 1500 × (0.01667)² ≈ 1.875px

Cumulative Displacement after Frame 4: S4 = S3 + s4 ≈ 3.958px

  • Frame 60:

Velocity Update: v = 1475 + 1500 × 0.01667 ≈ 1500px/s

Displacement Update: s60 = 1475 × 0.01667 + 1/2 × 1500 × (0.01667)² ≈ 24.71px

Cumulative Displacement after Frame 60: S60 = S59 + s60 ≈ 750px

Summary Over Multiple Frames:

Each frame, the character’s velocity increases by 25 px/s due to the constant acceleration. The displacement for each frame includes the travel from the existing velocity at the start of the frame plus the additional displacement due to acceleration during that frame. By summing the displacement of each frame, we calculate the total displacement over one second to be approximately 750 pixels, and the final velocity reaches about 1500px/s, as expected from the continuous theoretical calculations.

These calculations illustrate how, over time, the cumulative displacement can be modeled accurately by summing up each frame’s incremental displacement, leading up to a significant total over seconds, aligning with the theoretical displacement calculated using the continuous formula.

Practical Application in Godot

Let’s see how this applies in Godot with a practical example of a character controlled by simple physics:

extends CharacterBody2D

# Constants for maximum speed and acceleration.
const MAX_SPEED = 400
const ACCELERATION = 1500

# Variable to hold the current input vector.
var input = Vector2.ZERO
# Function that handles physics updates.
func _physics_process(delta):
# Update input based on player's control.
input.x = Input.get_axis("left", "right") # returns -1 if left or 1 if right
input.y = Input.get_axis("up", "down") # returns -1 if up or 1 if down
# Check if there is any input from the player.
if input == Vector2.ZERO:
# Gradually reduce the velocity to zero if there is no input.
velocity = velocity.move_toward(Vector2.ZERO, ACCELERATION * delta)
else:
# Accelerate the character in the direction of input.
velocity += (input.normalized() * ACCELERATION * delta)
# Clamp the velocity to the maximum speed.
velocity = velocity.limit_length(MAX_SPEED)
# Move the character and handle collisions.
move_and_slide()

Why It Matters

Understanding how things move in games isn’t just about making them look right; it’s also about making your game feel right. Whether it’s ensuring that jumps feel natural or that a rolling ball slows down on an uphill, getting these details right enhances the player’s experience and engagement with your game.

Conclusion

Starting with basic physics like acceleration and displacement can greatly improve how realistic your game feels and behaves. With Godot, you can apply these principles directly to bring your games to life in a way that’s both fun and scientifically sound.

--

--