collision failing in gamemaker 2.0

1.1k Views Asked by At

Here's my collision code. (I got it from FriendlyCosmonaut but added the debug message for my own benefit):

// COLLISION CHECK
//Horizontal

if (place_meeting(x+moveX,y,obj_solid)){
    repeat(abs(moveX)){
        if(!place\_meeting(x+sign(moveX),y,obj\_solid)){
            x = x + sign(moveX);
        } else {
            break;
        }
    }
    moveX = 0;
    show_debug_message("collision");
}

//Vertical
if (place_meeting(x,y+moveY,obj_solid)){
    repeat(abs(moveY)){
        if(!place\_meeting(x,y+sign(moveY),obj\_solid)) {
            y = y + sign(moveY);
        } else {
            break;
        }
    }
    moveY = 0;
    show_debug_message("collision");
}

see pic, red border is part of sprite for clarity

enter image description here

game is allowing a 32px overlap before actually setting moveY to 0; the collision mask starts 16px down from the top of the sprite. (origin is set to top left, which is outside the collision mask range.) it then "traps" me there, and i cannot move in any direction. and the debug output registers like 100 "collision"s...

based on yoyo's documentation I thought place_meeting was for mutual precise masks only, which admittedly I've not using here, but the code won't work even if I do (though would probably give different coordinates and output--I didn't check all scenarios).

I really have no clue why this is broken and have tried other codes to the same effect. this is what I've selected as a troubleshooting starting point, and I appreciate any help I can get! please don't come for me bc i didn't provide every necessary detail or fkd something up. I'm a novice, and I'm here to learn. :)

Update: Apparently the collision does happen with other types of collision masks (precise and diamond for instance). But that does not create the look I want--it really needs to be a manual rectangle.

1

There are 1 best solutions below

0
On

you need to replace the repeat with a while.

    if (place_meeting(x+moveX,y,obj_solid)){
      while(!place_meeting(x+sign(moveX),y,obj_solid)){
         x = x + sign(moveX);
      }
      moveX = 0;
    }
    x += moveX;

same for the vertical collision, this scheme should be more clean and immediate.