Disktra Wiki

Many stuff I need or not

User Tools

Site Tools


2d_platformer_programming_notes

2D Platformer programming notes

This page is used for listing all the useful scripts for handle stuff like gravity and physic in 2D platformer games. I've found some of them on the internet by reading articles on “How to create Platform games in GameMaker” and I think it could be nice to keep them somewhere.

Before starting explain how the scripts works, I'll introduce a little function created by Matt thorsen used to shift one value to another by a specific amount:

  Approach(start, end, shift){
      if (start< end)
          return min(start+ shift, end); 
      else
          return max(start - shift, end);
  }
  

This function is used to increase or decrease a value to another one in a smoothly way. The shift variable is the value retracted or added to the start variable.

Gravity

Gravity is quite simple to handle in a program. It's a value retracted to your player's jump height and regulary decreased, so each frame the jump height is getting lower and lower to finally be negative (<0), so the player is attracted on the ground.

Note: pay attention to the origin in your program or game engine. For example, increasing the Y value in GameMaker make your player going down.

  verticalSpeed = Approach(verticalSpeed, maxVerticalSpeed, Gravity) 

Jump

  // Jump 
  if (kJump) { 
      if (onGround)
          vy = -jumpHeight;
      // Variable jumping
  } else if (kJumpRelease) { 
      if (vy < 0)
          vy *= 0.25;
  }

Collisions

1) Is there a collision one pixel in the direction that I am moving (up/down)? 2a) If no, move one pixel in that direction (up/down). 2b) If yes, rest your speed (up/down) and exit the loop. 3) Repeat step 1.

You repeat this cycle until a collision causes you to break out of the loop OR you have iterated “vy” times through the loop. The horizontal movement loop looks identical at this point.

2d_platformer_programming_notes.txt · Last modified: 2018/03/20 15:38 by disktradev