• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Vertical Object Collision

netoxinaa

Member
Hello, I'm building basic platformer mechanics and I want to implement object collision but I'm having a problem. This is the code I wrote (just for vertical collision):
GML:
if (vSpeed > 0) bbox_side = bbox_bottom; else bbox_side = bbox_top;

if (place_meeting(bbox_left, bbox_side + vSpeedFinal, oCollision) || place_meeting(bbox_right, bbox_side + vSpeedFinal, oCollision))
{
    while (!place_meeting(bbox_left, bbox_side + sign(vSpeedFinal), oCollision) || !place_meeting(bbox_right, bbox_side + sign(vSpeedFinal), oCollision))
    {
        y += sign(vSpeedFinal);
    }
    vSpeedFinal = 0;
    vSpeed = 0;
}
What I intend with the while is to get the player sprite closer to the object to collide until it's one pixel away from it, but for some reason the player gets freezed way above the platform (object to collide) and doesn't fall. Am I doing something wrong?
 

Nidoking

Member
Well, bbox_side is sitting back where the player started the loop because you never update it. So if it happens once, it's an infinite loop and you just froze your game.
 

Yal

šŸ§ *penguin noises*
GMC Elder
place_meeting checks the entire sprite for collisions, when moved to the indicated coordinates. ("Place object, check for meeting"). Your logic acts as if it checks a single pixel (which you can do with position_meeting). If your origin is in the default top-left of the sprite, most of the sprite will be below the point you check, which leads to this hover effect (the collision is further down than where you check, so the object stops early). Either only use a single place_meeting check, which is at x,y, or use position_meeting for single-pixel checks.
 

netoxinaa

Member
@Yal thank you, now I understand the difference, but as @Nidoking says the problem is that the bounding box is always the same value so the loop always goes on. Do you know any other way to get my sprite one pixel away from the platform in just one frame?
 

Nidoking

Member
...

You just need to update bbox_side each time you move the instance. Or stop using a temp variable and just use bbox_top or bbox_bottom directly, since they update automatically when you move. The ?: operator can do that for you easily.
 
Top