Basic Physics Platformer Controls

Discussion in 'Tutorials' started by tagwolf, Jul 28, 2019.

  1. tagwolf

    tagwolf Member

    Joined:
    Aug 6, 2016
    Posts:
    59
    GM Version: GMS2
    Target Platform: ALL

    Summary:
    I had made a platform control tutorial that is a bit more advanced than this one as it does not use any of gamemaker's built in physics engine. However someone suggested that I also do a physics one so here it is!
    If you're interested in the non-physics version it is available here: https://forum.yoyogames.com/index.p...fer-air-control-diagonal-collision-etc.65684/

    This tutorial is scaled for a 32x64 player, but please play around with gravity, friction, dampening, etc in order to get the right feel for your game!

    Features:
    • Uses GMS2 physics
    • Jump input buffering
    • My other tutorial has a lot more goodies to it, there are sometimes benefits to using the built in physics engine, especially for particular types of games, but if you want much tighter controls and such you should look at my other tutorial linked above. Still wanted to write this up in case it's useful to your use-case!
    Todo:

    This probably needs some adjustment to get the feel right and scale. Pixels to Meters is not working how I'd hoped. But this should get you most of the way there. Upping physics evaluations to 60fps will probably help a lot too if you need the responsiveness at the cost of cpu.

    Tutorial:
    Things to create
    • spr_Player (32x64 pixel, just fill it in with a solid color for now)
    • obj_Player
      • Check the boxes for Solid and Uses Physics
      • Density to 1, Restitution to 0, Linear Damping to 1, and you can leave the other stuff default.
    • obj_Block
      • Check the boxes for Solid and Uses Physics
      • Under the physics settings, change Density to 0. This will make the blocks immune to gravity. (Not sure why kinematic doesn't work for this purpose but hey. This works.
      • Also set Restitution to 0. (We don't want blouncy floors.)
    • rm_default
      • Create a room or in the default one, turn on physics, set Gravity to 100.
    In your room, place several obj_Block's to paint a basic level and place your player above the blocks. You don't have to put them on the ground, just make sure they won't fall forever.

    obj_Player - Collision Event (obj_Block)
    *** This blank event must exist or GameMaker will not register a collision between you and the blocks. ***
    Code:
    /// @description Placeholder
    
    // So GameMaker registers the event.
    
    obj_Player - Create Event
    Code:
    /// @description Init Vars
    
    // Forces and Speeds
    // Acceleration and jumping forces
    x_force = 1000;
    y_force = 1000;
    // How fast player can move (pixels/second)
    max_x_speed = 4;
    // Jumping is limited by y_force and gravity room setting
    
    // Input buffering
    // Adding a buffer in frames to make jumping more forgiving
    jump_buffer = 10;
    // Count placeholder (should be 0 here)
    jump_buffer_count = 0;
    
    // Controls
    control_left = ord("A");
    control_right = ord("D");
    control_jump = vk_space;
    
    // Prevent player from falling over.
    // Disable this if you're making a face dragging game.
    phy_fixed_rotation=true;
    
    
    obj_Player - Step Event
    Code:
    /// @description Player Movement
    
    // Move right
    if keyboard_check(control_right)
    {
       physics_apply_force(x, y, x_force, 0); 
    }
    
    // Move left
    if keyboard_check(control_left)
    {
       physics_apply_force(x, y, -x_force, 0); 
    }
    
    // Jump Input
    if keyboard_check_pressed(control_jump) && jump_buffer_count >= jump_buffer
    {
       jump_buffer_count = 0;
    }
    // Check / increment jump buffer
    if jump_buffer_count < jump_buffer
    {
       jump_buffer_count++;
    }
    
    // Player is standing on ground and account for jump_buffer
    if place_meeting(x, y + 1, obj_Block) && jump_buffer_count < jump_buffer
    {
       // Jump!
       physics_apply_impulse(x, y, 0, -y_force);
    }
    
    // Clamp movement speed so we don't accelerate forever
    phy_speed_x = clamp(phy_speed_x, -max_x_speed, max_x_speed);
    
     
    Last edited: Jul 28, 2019
    Amon and NeZvers like this.
  2. Amon

    Amon Member

    Joined:
    Sep 13, 2016
    Posts:
    272
    This is cool, thank you.
     
    tagwolf likes this.
  3. babfoxj

    babfoxj Member

    Joined:
    Dec 28, 2017
    Posts:
    3
    I like what you did. These is very simple code, and sometimes that the best approach. I noticed you use the A/D keys for left/right respectively, then Space for jumping. Is this a common setup for computer-based 2D platform games? Or is this your personal preference?
     
  4. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    4,526
    WSAD keys are the accepted standard for movement in keyboard controlled games, and when you have your left hand over these keys your thumb is handily pisitioned right near the Space Bar so it makes sense to use it.

    Not good for lefties though so it's usually best to add in additional controls methods such as arrow keys for movement, or even better allow the player to rebind them.
     
  5. babfoxj

    babfoxj Member

    Joined:
    Dec 28, 2017
    Posts:
    3
    Thank you for the reply! I do plan on adding functionality for defining custom controls, but at this point I only need to define defaults for my platforming game. I’m looking for a setup ideal for a classic Megaman-style game. Would you have anymore suggestions for something like that?
     
  6. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    4,526
    I'd recommend taking a look at this input library by @Juju.

    It will let you bind multiple inputs (including gamepads) to single checks in your code and is really simple to use.
     
    Juju likes this.
  7. Juju

    Juju Member

    Joined:
    Jun 20, 2016
    Posts:
    412
    Thanks @rIKmAN! I'm always improving and maintaining my libraries, all suggestions and bug reports are welcome.
     
    rIKmAN likes this.
  8. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    4,526
    You're welcome mate, all the thanks should go to you as it's an awesome little library that does exactly what it says on the tin with zero fuss and is really easy to use! :)
     
    Juju likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice