Skip to content

Creating a basic physics engine for platformer games

💡 Having trouble with Scratch block assembly? Don’t know how to implement code logic? 🚀 Get Help Now

PL

PhysicsLearner

Posted on January 22, 2024 • Intermediate

🎮 Need help creating a physics engine

Hi everyone! I’ve been trying to create a physics engine for my platformer game for about a year now, but I can’t seem to get it right. I’ve seen some amazing physics engines in other projects, but they’re too complex for me to understand.

What I’m looking for is:

  • Left and right movement for the player
  • Jumping mechanics with gravity
  • Collision detection so the player can’t pass through colors or sprites
  • Something that’s advanced but still easy to understand

I really need help with velocity and making it all work together smoothly. Any guidance would be greatly appreciated! 😊

PE

PhysicsExpert_Sam

Replied 3 hours later • ⭐ Best Answer

Great question @PhysicsLearner! Creating a physics engine can seem daunting, but let’s break it down into manageable parts. Here’s a step-by-step approach:

⚙️ Physics Engine Architecture

Here’s how a basic physics engine works in Scratch:

flowchart TD A[🚩 Game Start] --> B[📊 Initialize Physics Variables] B --> C[🔄 Main Physics Loop] C --> D[⌨️ Handle Input] D --> E[🏃 Update Horizontal Velocity] E --> F[🌍 Apply Gravity] F --> G[📍 Update Position] G --> H[🔍 Collision Detection] H --> I{🧱 Hit Wall?} I -->|Yes| J[🛑 Stop Horizontal Movement] I -->|No| K[✅ Continue Movement] J --> L{🌍 Hit Ground?} K --> L L -->|Yes| M[🛑 Stop Falling + Reset Jump] L -->|No| N[⬇️ Continue Falling] M --> O[🎮 Check Jump Input] N --> O O --> P{⌨️ Jump Pressed?} P -->|Yes + On Ground| Q[🚀 Apply Jump Force] P -->|No| R[⏱️ Wait Next Frame] Q --> R R --> C subgraph "Physics Variables" S["🏃 X Velocity"] T["⬆️ Y Velocity"] U["🌍 Gravity Force"] V["🦘 Jump Power"] W["🏃 Move Speed"] X["🛑 Friction"] end subgraph "Collision Types" Y["🧱 Wall Collision"] Z["🌍 Ground Collision"] AA["🔝 Ceiling Collision"] BB["📦 Platform Collision"] end style A fill:#e1f5fe style B fill:#f3e5f5 style F fill:#fff3e0 style H fill:#ffebee style Q fill:#c8e6c9

🔧 Step 1: Set Up Variables

First, create these variables for your player sprite:

    when flag clicked
set [x velocity v] to [0]
set [y velocity v] to [0]
set [on ground v] to [false]
set [gravity v] to [0.5]
set [jump power v] to [12]
  

🏃 Step 2: Horizontal Movement

Handle left and right movement with acceleration and friction:

    when flag clicked
forever
// Handle input
if <key [right arrow v] pressed?> then
change [x velocity v] by [1]
end
if <key [left arrow v] pressed?> then
change [x velocity v] by [-1]
end

// Apply friction
set [x velocity v] to ((x velocity) * [0.8])

// Move horizontally
change x by (x velocity)

// Check horizontal collision
if <touching color [#000000]?> then
if <(x velocity) > [0]> then
repeat until <not <touching color [#000000]?>>
change x by [-1]
end
else
repeat until <not <touching color [#000000]?>>
change x by [1]
end
end
set [x velocity v] to [0]
end
end
  

⬆️ Step 3: Vertical Movement and Gravity

Implement jumping and gravity in a separate forever loop:

    when flag clicked
forever
// Apply gravity
change [y velocity v] by ((gravity) * [-1])

// Handle jumping
if <<key [space v] pressed?> and <(on ground) = [true]>> then
set [y velocity v] to (jump power)
set [on ground v] to [false]
end

// Move vertically
change y by (y velocity)

// Check vertical collision
if <touching color [#000000]?> then
if <(y velocity) < [0]> then
// Falling - hit ground
repeat until <not <touching color [#000000]?>>
change y by [1]
end
set [on ground v] to [true]
else
// Rising - hit ceiling
repeat until <not <touching color [#000000]?>>
change y by [-1]
end
end
set [y velocity v] to [0]
else
set [on ground v] to [false]
end
end
  

🚀 Step 4: Advanced Features (Optional)

For more realistic physics, you can add these improvements:

Variable Jump Height:

    // Replace the jumping code with this
if <key [space v] pressed?> then
if <(on ground) = [true]> then
set [y velocity v] to (jump power)
set [on ground v] to [false]
end
else
// Cut jump short if space is released
if <(y velocity) > [0]> then
set [y velocity v] to ((y velocity) * [0.5])
end
end
  

Coyote Time (grace period for jumping):

    when flag clicked
set [coyote timer v] to [0]
forever
if <(on ground) = [true]> then
set [coyote timer v] to [6]
else
if <(coyote timer) > [0]> then
change [coyote timer v] by [-1]
end
end

// Allow jumping if on ground OR within coyote time
if <<key [space v] pressed?> and <(coyote timer) > [0]>> then
set [y velocity v] to (jump power)
set [coyote timer v] to [0]
end
end
  

This creates a solid foundation for a physics engine! The key is separating horizontal and vertical movement, using velocity variables, and handling collisions properly. 🎯

PL

PhysicsLearner

Replied 45 minutes later

@PhysicsExpert_Sam This is exactly what I needed! Thank you so much! 🎉

The separation of horizontal and vertical movement makes so much sense now. I got the basic version working and it feels great! One question - how do I make the movement feel less “floaty”?

GT

GameTuner_Alex

Replied 1 hour later

@PhysicsLearner Great question! To make movement feel less floaty, try these tweaks:

  • Increase gravity: Try setting gravity to 0.8 or 1.0 instead of 0.5
  • Reduce jump power: Lower values like 10 or 8 can feel more grounded
  • Adjust friction: Use 0.7 instead of 0.8 for snappier movement
  • Add terminal velocity: Cap falling speed at -15 or -20

Experiment with these values until it feels just right! 🎮

VB

Vibelf_Community

Pinned Message • Moderator

🚀 Ready to Master Game Physics?

Excellent discussion on physics engines! For those looking to create even more advanced physics systems, our community can help you implement:

  • 🌊 Fluid dynamics and water physics
  • 🎱 Realistic ball physics and bouncing
  • 🔗 Chain and rope physics
  • 💥 Particle systems and explosions

📚 Related Topics

Want to create the next amazing physics-based game? Get personalized guidance from our expert tutors!