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

[Solved] Having difficulty with tile collisions smaller then the player.

Discussion in 'Programming' started by snowy1996, Nov 27, 2017.

Tags:
  1. snowy1996

    snowy1996 Member

    Joined:
    Sep 9, 2017
    Posts:
    15
    I've been having issues getting accurate tile collision between my player object and my tile set. I've been using the code used by Shaun in his video on the subject, linked here:
    It works well enough when the tiles are the same size or larger then the object colliding with it, but while trying to use tiles smaller then the player object the object will always be placed farther back then it should after detecting collision with the tile set.

    The player object sprite is a 32x32 box, with a full body mask with its origin placed int he middle center. The tile set is sized as 8x8.
    My code is as follows.
    Code:
    if (hsp > 0) bbox_side = bbox_right; else bbox_side = bbox_left;
    if (tilemap_get_at_pixel(tilemap,bbox_side+hsp,bbox_top) != 0) or (tilemap_get_at_pixel(tilemap,bbox_side+hsp,bbox_bottom) != 0)
    {
        if (hsp > 0) x = x - (x mod 8) + 7 - (bbox_right - x);
        else x = x - (x mod 8) - (bbox_left - x);
        hsp = 0;
    }
    x += hsp;
    
    if (vsp > 0) bbox_side = bbox_bottom; else bbox_side = bbox_top;
    if (tilemap_get_at_pixel(tilemap,bbox_left,bbox_side+vsp) != 0) or (tilemap_get_at_pixel(tilemap,bbox_right,bbox_side+vsp) != 0)
    {
        if (vsp > 0) y = y - (y mod 8) + 7 - (bbox_bottom - y);
        else y = y - (y mod 8) - (bbox_top - y);
        vsp = 0;
    }
    y += vsp;
    
    It's the same code used in Shaun's video, using what I think is the appropriate values for my tile set dimensions. I'm at a loss as for what the issue is.
     
  2. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,026
    I'm on my phone and unable to watch Shaun's video, but what was the default code he used? I'm just wondering if the sevens in your code are supposed to be 31s?
     
  3. snowy1996

    snowy1996 Member

    Joined:
    Sep 9, 2017
    Posts:
    15
    In the video Shaun's using objects and a tile set going by 32x32, and so the line of code is:
    In my case my tile set is 8x8, and so I've changed some of the numbers in accordance to what I believe is the correct specifications, if I'm understanding Shaun's explanation right..
     
  4. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,026
    I don't know how the hell Shaun got that to work! I noticed in the last demo he didn't try lefthand collisions, but he did have them in the previous demo. Or at least, it appeared he did. It's possible it didn't actually work for him and was just a quirk of a combination of his sprite size, tile size, and hsp vector. (And yes, his codes in his tutorials are sometimes trash.)

    I hate codes that require different behavior for one direction from the other, but with this kind of code, it's pretty much necessary. So if you're going to preemptively check collisions (e.g., bbox_side+hsp) then you need to add the hsp into the formula. You don't do that, so it doesn't work for both sides (it should be working for at least one side, though).

    x = (x + hsp & ~7) + x + 7 * (hsp > 0) - bbox_side;

    So your +7s were fine. Those weren't the issue. The whole x-(x mod 8) method is overkill to me, so I simplified it with bitwise notation to (x & ~7). But like I said, hsp needs to be factored in because we're not just moving to the grid, we're moving in the direction we're headed to the grid. So hsp is added to x first. Then because of the stupid little issue with x being too far to the left when our hsp is positive, we only add 7 when hsp is positive. ...A lot of people hate inline codes like this, but I find them to sometimes be a tad neater.
     
    Last edited: Nov 28, 2017
  5. snowy1996

    snowy1996 Member

    Joined:
    Sep 9, 2017
    Posts:
    15
    Alright, after some quick copy paste your code is working very nicely in 3 out of 4 directions with my 16x24 mask (which is where I want it, as that's the size of my intended character sprite), but i'm still having the same issue when testing with a 32x32 mask. I should think I could fix the issue I'm still getting in when colliding in the 4th direction (moving up in this case), but I'm just having a bit of trouble understanding your code. I'm still rather new at this.

    x = (x + hsp & ~7) + x + 7 * (hsp > 0) - bbox_side;

    Would you explain the parts in bold again? I don't understand how they function exactly.
    ~ doesn't show up in the user manual, and I'm not sure how an & symbol is used in the calculation.
    * being the symbol used for multiplication, so is it 7 times hsp, but only if hsp is greater then 0?

    Thanks, you're being a great help.
     
  6. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,026
    x & 7 is the same as x mod 8. x & ~7 is the same as x-(x mod 8). Research binary and bitwise operations.

    x+7*(hsp>0) is the same as
    if hsp>0 x+7 else x+0

    Conditionals are either 0 or 1. The parentheses are necessary because conditionals have the second-lowest priority.
     
  7. snowy1996

    snowy1996 Member

    Joined:
    Sep 9, 2017
    Posts:
    15
    Alright! After some small adjustments the collision is now working seemingly flawlessly. Thanks a ton, I'd been stuck on this for a week!
     
  8. loadedword

    loadedword Member

    Joined:
    Oct 17, 2018
    Posts:
    1
    hey snowy1996 I've got something going on similar to your problem. I was curious as to what your code solution was.
     

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