• 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!

SOLVED Different sprites won't collide equally with same code

netoxinaa

Member
I'm trying to add tile collisions into my game, but it won't work with the sprite I want the player to have. I made many tests but nothing worked, until I changed the sprite to be just a square 32x32, then it worked right! But I don't get why, since the code uses bounding boxes so I'm sure every sprite would have to function correctly.
1601257272241.png 1601257298431.png

My tiles are 16x16
Here's the code ( I use Shaun's tutorial) :
GML:
var bbox_side;
  
    //HORIZONTAL
    if (hSpeed > 0) bbox_side = bbox_right; else bbox_side = bbox_left;
    if (tilemap_get_at_pixel(global.collisionMap, bbox_side + hSpeed, bbox_top) != 0
    || tilemap_get_at_pixel(global.collisionMap, bbox_side + hSpeed, bbox_bottom) != 0)
    {
        if (vSpeed > 0) x = x - (x mod TILE_SIZE) + 15 - (bbox_right - x);
        else x = x - (x mod TILE_SIZE) - (bbox_left - x);
  
        hSpeed = 0;
    }

    //VERTICAL
    if (vSpeed > 0) bbox_side = bbox_bottom; else bbox_side = bbox_top;
    if (tilemap_get_at_pixel(global.collisionMap, bbox_right, bbox_side + vSpeedFinal) != 0
    || tilemap_get_at_pixel(global.collisionMap, bbox_left, bbox_side + vSpeedFinal) != 0)
    {
        if (vSpeed = 0) y = y - (y mod TILE_SIZE) + 15 - (bbox_bottom - y);
        else y = y - (y mod TILE_SIZE) - (bbox_top - y);
  
        vSpeed = 0;
        vSpeedF = 0;
        vSpeedFinal = 0;
    }
Also I'm using his method for removing decimals from the vSpeed so the player just moves in integers and doesn't do the "bouncing" effect.
 

NightFrost

Member
The character seems to be sinking too much for this to be the cause, but: if you are dealing with speeds that can be decimals, you should be aware that bbox_* variables give rounded positions. So when you test for bbox coordinate plus speed, you are introducing a potential +/- 1 pixel error to the tilemap position you are testing. You should test at the precise x or y coordinate, plus distance to collision box edge, plus speed. You can deduce the box edge distances from values of sprite_get_xoffset/yoffset and sprite_get_bbox_* commands.

(If your speeds are always integers, then coordinates will never have fractions, and bbox values align precisely with them.)
 

netoxinaa

Member
, but: if you are dealing with speeds that can be decimals,
that's why I have vSpeedFinal , it's the vSpeed value but without decimals, so I'm always checking at integers. Having this scenario discarded, I can't figure out why this would happen.
 

TheouAegis

Member
Are you using the sprite's collision bounds, or are you setting a mask in the object?

Are your sprites' origins the same relative to the bottom of the bounding box?
 

netoxinaa

Member
@TheouAegis Their collision mask is in automatic, and both's origin are middle center:

1601328838004.png1601328843559.png

And as you can see, I made a shameful mistake. All this time I though the square sprite was of 32x32, until I checked again to take the screenshot I saw it is 16x16. So because of this mistake, I scale the square to a 32x32 sprite to test it and this was the result (I added a red square to look if the bounding boxes were correct):

1601329221431.png
Just as the player's sprite.

So this means that my code does not work with sprites bigger than my tile size? As far as I know, tile collisions work while the object sprite's size is no longer than 2 tiles, which in my case, the player sprite doesn't exceed that size.
 

Roldy

Member
So this means that my code does not work with sprites bigger than my tile size? As far as I know, tile collisions work while the object sprite's size is no longer than 2 tiles, which in my case, the player sprite doesn't exceed that size.
Look at the hardcoded literal numbers you have in the code... e.g. '15'. Probably because your TILE_SIZE is 16 but you are also using it to calculate offsets of the sprites.

Just step through the math and look at the results. Copying code from tutorials isn't the end, you need to look through each line and understand how it works. Keep working on it.
 

netoxinaa

Member
you need to look through each line and understand how it works
And I do understand them, but the results where very odd so that's why I asked for help.

And here is the most embarrasing mistake I've ever done: while trying to snap to grid vertically, I typed if (vSpeed = 0) instead of if (vSpeed > 0) , so it was always snapping the player downwards
 
Top