Basic Physics Platformer Controls

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

  1. tagwolf

    tagwolf Member

    Joined:
    Aug 6, 2016
    Posts:
    58
    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:
    229
    This is cool, thank you.
     
    tagwolf 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