1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

GMS 2 8 directional movement and collisions?

Discussion in 'Programming' started by lamatoast, May 20, 2019.

  1. lamatoast

    lamatoast Member

    Joined:
    Aug 12, 2017
    Posts:
    42
    Hi everyone, i have made a top down control system that uses lengthdir_x and lengthdir_y to move. The system is good because you move the same speed in every direction but i am not sure how to handle collisions, please help?.
     
  2. Bentley

    Bentley Member

    Joined:
    Jun 18, 2017
    Posts:
    818
    This might work:
    Code:
    var hspd = (keyboard_check(vk_right) - keyboard_check(vk_left)) * move_speed;
    
    if (!place_meeting(x + hspd, y, o_solid))
    {
        x += hspd;    
    }
    else
    {
        while (!place_meeting(x + sign(hspd), y, o_solid))
        {
            x += sign(hspd);    
        }
        hspd = 0; // Set to 0 in case code below is based on what "hspd" is
    }
    You'd do the same for vertical movement.
     
    Last edited: May 20, 2019

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