SOLVED Collision bugs help

Hi again everyone. As the title suggests I've started coding collision for my game and could use some help.
I'm using the place_free method for collision and one of the bugs I've encountered is that the player sometimes spawns inside the collison mask and so cant move anymore.
For now I've gotten around this by coding a literal unsticking button. When pressed it moves the player outside the collision mask. obviously this is not the best solution but it works for now.
So could use some tips in coding rudimentary collision.
 

Rob

Member
  • Have a variable hold the horizontal speed
  • Check for a collision with place_meeting, using current y and x + horizontal speed
  • If there is a collision, use a loop like repeat to move the player 1 pixel at a time horizontally
  • Before you move the 1 pixel, check for collisions using place_meeting again, but this time using sign(x + horizontal speed) (sign returns -1, 0, or 1)
  • If there is a collision during this stage, set horizontal speed to 0;
  • If there is no original collision, just put x += horizontal speed. Horizontal speed will be either unchanged or set to 0 at this point.
  • Do the same for the vertical (below the horizontal code)
So the structure:

GML:
//set horizontal speed + vertical speed here
horizontal_speed = ( right-left ) * move_speed;

//If there's going to be a collision, we have to check per pixel
if (place_meeting(x + horizontal_speed, y, obj_something)){
    //Use a loop to check 1 pixel at a time
    repeat abs(horizontal_speed){ //abs turns a negative into a positive so -15 = 15 and 15 = 15
        //
        if (!place_meeting(x + sign(horizontal_speed), y, obj_something)){
            x += sign(horizontal_speed);
        }else{
            horizontal_speed = 0;
            break; //stop the loop as it's no longer needed
        }
    }
}
//This line will run regardless of what happened above. Either horizontal_speed was set to 0 because of collision/lack of movement or
//there is no collision and the player moves the full amount
x += horizontal_speed;
 
Last edited:
Top