A platformer lives or dies on how its movement feels. Getting gravity and collisions right on a <canvas> meant moving past React state updates and working out the physics directly.
Use A/D or Arrows to Move & Space to Jump (Double Jump in Mid-Air!). Reach the end flag to win!
Leap across textured platforms with smooth double jumps! Collect rotating fruits, dodge spike pits, stomp fully animated Pink Man sprites, dodge smashing Rock Heads, and jump over spiky Spike Heads to survive!
1. Simulating Physical Forces (Gravity & Inertia)
To make the mascot feel heavy and physical, I couldn't simply move it by fixed increments on a button press. Instead, I simulate continuous acceleration and friction:
- Velocity and Acceleration: Pressing left or right applies a steady force (
vx += 0.45), mimicking muscles running. - Air resistance & Friction: Multiplying velocities by
0.85on every frame simulates physical friction, causing the player to slide to a natural halt rather than stopping instantly. - Continuous Gravity: Every single frame, gravity pulls the mascot downwards (
vy += 0.5), which is resolved instantly when it makes contact with solid block hitboxes.
2. Multi-Platform Landing Collisions (AABB)
Resolving collisions on floating blocks requires Axis-Aligned Bounding Box (AABB) math. If the mascot's coordinates overlap with a floating platform, I verify whether it was falling downwards (vy > 0) and landing on top. If so, I snap its y to the platform's surface and zero out its vertical velocity, marking it as grounded: true.
3. Procedural Enemies and Squashing
The enemy patrols its platform boundaries, reversing direction when it reaches one. If the player overlaps with the enemy while falling downwards, I register a squashed state: the enemy dies, and the player receives a spring-loaded vertical boost. A hit from any other side triggers a game reset.