GML Sprite clipping into wall by a single pixel? [solved]

Discussion in 'Programming' started by Setteduetto, Oct 11, 2018.

  1. Setteduetto

    Setteduetto Member

    Joined:
    Sep 7, 2017
    Posts:
    13
    I'm making a simple platformer game, but the blue square I'm using as a player avatar is clipping into a wall by a single pixel.

    The collision code is solid and works perfectly. The sprite's bounding box is literally just the automated rectangle and the origin is set to middle center. The walls also have an automated rectangular shape.

    At first I thought the issue had something to do with my resizing the walls. I'm using a 16x16 square and scaling it up for however big I need the walls to be, and I thought that might be the issue. However I've since tried running it with the walls at the accurate size and the issue persists.

    Also, it only seems to happen when I hit a left wall, not a right one. And it doesn't register my character as colliding with the wall because if it did the character wouldn't be able to move horizontally, but the game works fine.
     
  2. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    484
    EDIT: Oh, I misread. So collisions are not the issue, your square is just overlapping the left walls by 1 pixel. We are still going to need to see some code to help. Could you post your collision code?
     
    Last edited: Oct 11, 2018
  3. Setteduetto

    Setteduetto Member

    Joined:
    Sep 7, 2017
    Posts:
    13
    That's correct, collision isn't the problem. In fact, I've tested it and I'm 100% sure the objects aren't colliding. But here's the collision code anyway:

    Code:
    //horizontal collision
    if (place_meeting(x+hsp,y,obj_floor))
    {
    while (!place_meeting(x+sign(hsp), y, obj_floor))
    {
    x = x + sign(hsp)
    }
    hsp = 0
    vaulting = 0

    }
    //vertical collision
    if (place_meeting(x,y+vsp,obj_floor))
    {
    while (!place_meeting(x, y+sign(vsp), obj_floor))
    {
    y = y + sign(vsp)
    }
    vsp = 0
    vaulting = 0
    }
    //final collision safety net
    if (place_meeting(x+hsp,y+vsp,obj_floor))
    {
    vsp = 0
    hsp = 0
    vaulting = 0
    }

    You can ignore the vaulting variable since it's used for something else. vsp = vertical speed, hsp = horizontal speed. I move the object by adding these to the x and y values raw.

    Even though it's not a collision problem, I feel like it might be some sort of issue with the bounding box, but I didn't set it manually so I don't know how that could be the case. I'm going to try editing that and seeing if it makes a difference.

    EDIT:
    So I figured out what was wrong. I had gravity set to 0.8 and my sprite was moving by amounts that were less than one. For some reason this made the sprite get unaligned with the bounding box by a single pixel. I fixed this by adding this to all of my collision checks:

    move_snap(1, 1)

    basically this snaps the movement of the object to a single pixel grid every time it collides with a wall or a ground, and fixes the clipping issues.
     
    Last edited: Oct 11, 2018
  4. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    484
    I haven't used move_snap too much, but I thought it was more used with the built in speed and motion function of GMS. If you are snapping after a collision with (1,1) I would of thought there was a chance you can snap inside a wall. If it's working for you though, great.
     

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