رفتن به محتوا

Platform collision and movement issues - teleporting bug fix

این محتوا هنوز به زبان شما در دسترس نیست.

💡 Having trouble with collision detection and movement physics? Need help debugging platform games? 🚀 Get Help Now

PA

PlatformDev_Alex

Posted on July 21, 2025 • Intermediate

🎮 Platform collision bug driving me crazy!

Hey everyone! I’m working on a platformer game and I’m having this really frustrating issue with collision detection. Here’s what’s happening:

  • When I jump left towards a platform, my character sometimes teleports under or below it
  • Walking left into platforms also causes weird teleporting behavior
  • The character gets instantly teleported on top of platforms when it should just stop

I’ve been working on this for hours and can’t figure out what’s wrong. The collision detection seems to work sometimes but not others. Any help would be amazing! 🙏

CM

CollisionMaster_Pro

Replied 3 hours later • ⭐ Best Answer

I see exactly what’s happening @PlatformDev_Alex! This is a super common issue with platformer collision detection. The problem is likely in your movement loop structure.

🔍 The Root Problem

When you have collision detection and movement in separate forever loops, they can execute in the wrong order, causing the teleporting effect. Here’s the proper way to structure it:

flowchart TD A[🚀 Game Start] --> B[Main Game Loop] B --> C[Handle Input] C --> D[Update X Velocity] D --> E[Move X + Check Collision] E --> F{X Collision?} F -->|Yes| G[Stop X Movement] F -->|No| H[Continue] G --> H H --> I[Update Y Velocity] I --> J[Move Y + Check Collision] J --> K{Y Collision?} K -->|Yes| L[Stop Y Movement] K -->|No| M[Continue] L --> M M --> N[Update Animation] N --> B style A fill:#e1f5fe style B fill:#f3e5f5 style F fill:#fff3e0 style K fill:#fff3e0

🔧 Step 1: Single Main Loop Structure

Replace multiple forever loops with one main loop:

    when flag clicked
forever
// Handle all movement and collision in order
handle input
move horizontally
check horizontal collision
move vertically  
check vertical collision
update animation
end
  

🏃 Step 2: Proper Horizontal Movement

Handle X-axis movement with collision detection:

    // Custom block: handle horizontal movement
define handle horizontal movement
if <key [left arrow v] pressed?> then
change [x velocity v] by [-0.5]
end
if <key [right arrow v] pressed?> then
change [x velocity v] by [0.5]
end

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

// Move and check collision
change x by (x velocity)
if <touching [Platforms v]?> then
if <(x velocity) > [0]> then
repeat until <not <touching [Platforms v]?>>
change x by [-1]
end
else
repeat until <not <touching [Platforms v]?>>
change x by [1]
end
end
set [x velocity v] to [0]
end
  

⬆️ Step 3: Proper Vertical Movement

Handle Y-axis movement with gravity and collision:

    // Custom block: handle vertical movement
define handle vertical movement
// Apply gravity
change [y velocity v] by [-0.8]

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

// Move and check collision
change y by (y velocity)
if <touching [Platforms v]?> then
if <(y velocity) > [0]> then
// Hitting ceiling
repeat until <not <touching [Platforms v]?>>
change y by [-1]
end
set [y velocity v] to [0]
else
// Landing on ground
repeat until <not <touching [Platforms v]?>>
change y by [1]
end
set [y velocity v] to [0]
set [on ground v] to [true]
end
end
  

🎯 Step 4: Complete Main Loop

Put it all together in the proper order:

    when flag clicked
set [x velocity v] to [0]
set [y velocity v] to [0]
set [on ground v] to [false]

forever
handle horizontal movement
handle vertical movement

// Optional: limit velocities to prevent glitches
if <(x velocity) > [10]> then
set [x velocity v] to [10]
end
if <(x velocity) < [-10]> then
set [x velocity v] to [-10]
end
if <(y velocity) > [15]> then
set [y velocity v] to [15]
end
if <(y velocity) < [-15]> then
set [y velocity v] to [-15]
end
end
  

The key is doing everything in the right order within a single loop. This prevents the timing issues that cause teleporting! 🎉

PA

PlatformDev_Alex

Replied 45 minutes later

@CollisionMaster_Pro OMG this is exactly what I needed! 🎉

I was indeed using separate forever loops for movement and collision detection. Your single loop approach fixed the teleporting issue completely! The movement feels so much smoother now.

Thank you so much for the detailed explanation and code examples! 🙏

PG

PlatformerGuru_Sam

Replied 1 hour later

Great solution @CollisionMaster_Pro! 👏 I’d like to add a few more tips for anyone else struggling with platformer physics:

  • Use smaller movement steps: If you’re still getting glitches, try moving in smaller increments (like 1 pixel at a time) when checking collisions
  • Separate X and Y collision detection: Always handle horizontal and vertical collisions separately to avoid corner-case bugs
  • Add coyote time: Allow jumping for a few frames after leaving a platform for better game feel
  • Use velocity limits: Cap your velocities to prevent objects from moving too fast and clipping through walls

These techniques will make your platformer feel much more polished! 🎮

VB

Vibelf_Community

Pinned Message • Moderator

🚀 Master Advanced Platformer Physics!

Excellent discussion on collision detection! For those ready to take their platformer games to the next level, our community can help you implement:

  • 🎯 Pixel-perfect collision detection
  • 🌊 Advanced physics systems (slopes, moving platforms)
  • ⚡ Performance optimization for complex levels
  • 🎮 Professional game feel techniques

📚 Related Discussions

Ready to create amazing platformer games? Get personalized guidance from our expert game development tutors in the Vibelf app!