Zum Inhalt springen

How do you make walls for a platformer

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

💡 Building your first platformer game? Need help with collision detection and game physics? 🚀 Get Game Dev Help

PD

PlatformerDev88

Posted on July 18, 2025 • Beginner

🧱 Need help with wall collision

Hey everyone! I’m working on my first platformer game and I’m stuck on a basic but important feature - making solid walls that the player can’t walk through.

Even though I’ve helped friends with their games before, I’m embarrassed to say I don’t actually know how to implement proper wall collision detection. The player should bounce off or stop when hitting a wall, not just phase through it.

Any simple solutions would be greatly appreciated! 🙏

GE

GameEngine_Master

Replied 55 minutes later • ⭐ Best Answer

Great question @PlatformerDev88! Wall collision is fundamental to platformers. Let me show you several approaches from simple to advanced!

🎯 Collision Detection Flow

Here’s how collision detection works in platformers:

flowchart TD A[🏃 Player Moves] --> B[Check for Wall Collision] B --> C{Touching Wall?} C -->|No| D[✅ Continue Movement] C -->|Yes| E[🚫 Handle Collision] E --> F{Collision Type?} F -->|Simple| G[Move Back 2 Steps] F -->|Advanced| H[Calculate Exact Position] G --> I[🎮 Player Stops at Wall] H --> J[📐 Precise Wall Alignment] D --> K[🔄 Next Frame] I --> K J --> K style A fill:#e1f5fe style D fill:#e8f5e8 style E fill:#fff3e0 style I fill:#fce4ec style J fill:#f3e5f5

🔧 Method 1: Simple Collision (Easiest)

This is the most straightforward approach - just move back when touching a wall:

    when flag clicked
forever
// Handle horizontal movement
if <key [right arrow v] pressed?> then
change x by (5)
if <touching [Wall v]?> then
change x by (-5)
end
end

if <key [left arrow v] pressed?> then
change x by (-5)
if <touching [Wall v]?> then
change x by (5)
end
end
end
  

🎮 Method 2: Smooth Movement with Collision

For smoother gameplay, separate movement and collision checking:

    when flag clicked
set [speed v] to [0]
forever
// Input handling
if <key [right arrow v] pressed?> then
set [speed v] to [5]
else
if <key [left arrow v] pressed?> then
set [speed v] to [-5]
else
set [speed v] to [0]
end
end

// Apply movement with collision check
move horizontally (speed)
end

define move horizontally (dx)
change x by (dx)
if <touching [Wall v]?> then
repeat until <not <touching [Wall v]?>>
if <(dx) > [0]> then
change x by (-1)
else
change x by (1)
end
end
end
  

🚀 Method 3: Advanced Pixel-Perfect Collision

For the most precise collision detection:

    define move with collision (dx) (dy)
// Store current position
set [old x v] to (x position)
set [old y v] to (y position)

// Try horizontal movement first
change x by (dx)
if <touching [Wall v]?> then
// Binary search for exact collision point
go to x: (old x) y: (y position)
set [step v] to (dx)
repeat until <(abs of (step)) < [1]>
set [step v] to ((step) / (2))
change x by (step)
if <touching [Wall v]?> then
change x by ((0) - (step))
end
end
end

// Store horizontal result
set [new x v] to (x position)

// Try vertical movement
go to x: (old x) y: (old y)
change y by (dy)
if <touching [Wall v]?> then
// Binary search for vertical collision
go to x: (x position) y: (old y)
set [step v] to (dy)
repeat until <(abs of (step)) < [1]>
set [step v] to ((step) / (2))
change y by (step)
if <touching [Wall v]?> then
change y by ((0) - (step))
end
end
end

// Apply final position
go to x: (new x) y: (y position)
  

🏗️ Method 4: Grid-Based Collision (Most Efficient)

For tile-based platformers, use grid collision:

    define check wall at grid (grid_x) (grid_y)
// Convert grid coordinates to world position
set [world_x v] to ((grid_x) * (32)) // 32 = tile size
set [world_y v] to ((grid_y) * (32))

// Check if this grid position has a wall
set [tile_id v] to (item ((grid_y) * (level width)) + (grid_x) of [Level Data v])
set [is wall v] to <(tile_id) = [1]> // 1 = wall tile

define move player with grid collision (dx) (dy)
// Calculate target grid position
set [target_x v] to (round (((x position) + (dx)) / (32)))
set [target_y v] to (round (((y position) + (dy)) / (32)))

// Check horizontal collision
check wall at grid (target_x) (round ((y position) / (32)))
if <not <(is wall)>> then
change x by (dx)
end

// Check vertical collision  
check wall at grid (round ((x position) / (32))) (target_y)
if <not <(is wall)>> then
change y by (dy)
end
  

💡 Pro Tips for Better Collision

  • Separate X and Y: Always handle horizontal and vertical movement separately to prevent getting stuck in corners
  • Use smaller steps: For fast-moving objects, move in smaller increments to prevent tunneling through walls
  • Collision layers: Use different collision sprites for walls, platforms, and hazards
  • Visual debugging: Make collision sprites semi-transparent during development to see what’s happening

Start with Method 1 for simplicity, then upgrade to Method 2 or 3 as your game becomes more complex! 🎮

PD

PlatformerDev88

Replied 20 minutes later

@GameEngine_Master This is incredibly helpful! 🤩 I started with Method 1 and it works perfectly for my simple platformer.

I love how you explained the progression from simple to advanced - I can see exactly how to improve my collision system as my game grows more complex. The visual diagram really helped me understand the flow too!

Thanks for the pro tips about separating X and Y movement - I was wondering why my player kept getting stuck in corners! 😅

PG

PlatformerGuru

Replied 1 hour later

Excellent breakdown @GameEngine_Master! 👏 I’d like to add one more technique that’s super useful for beginners:

🎯 One-Way Platforms

For platforms you can jump through from below but land on from above:

    // In your gravity/jumping code
change y by (gravity)
if <touching [Platform v]?> then
if <(gravity) < [0]> then // Falling down
repeat until <not <touching [Platform v]?>>
change y by (1)
end
set [gravity v] to [0]
set [on ground v] to [true]
end
end
  

This lets players jump up through platforms but land on top - essential for multi-level platformers! 🚀

VB

Vibelf_Community

Pinned Message • Moderator

🎮 Build Amazing Platformer Games with Expert Guidance

Great discussion on collision detection! For aspiring game developers ready to create professional-quality platformers, our tutors can help you master:

  • 🏃 Advanced player movement and physics
  • 🎯 Complex collision systems and optimization
  • 🌟 Special effects and juice for better game feel
  • 🏆 Level design principles and player progression

📚 Related Game Development Topics

Ready to create the next great platformer? Get personalized game development coaching from our expert tutors!