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

GML 8 direction diagonal collision issues [SOLVED]

having a difficult time with the collision here. when the character hits a surface while moving in the upper diagonals, they fall into the surface and get stuck, only able to move on the diagonals. can someone tell what's going on here, because I'm a complete novice.

GML:
///get key inputs
hInput = keyboard_check(ord("D")) - keyboard_check(ord("A"));
vInput = keyboard_check(ord("S")) - keyboard_check(ord("W"));

///normalize diagonal inputs
if(hInput !=0 or vInput !=0){
    dir = point_direction(0,0,hInput,vInput);
    moveX = lengthdir_x(spd,dir);
    moveY = lengthdir_y(spd,dir);

///check for horizontal collision
if(place_meeting(x + moveX, y, Obj_wall)) {
    while(!place_meeting(x + sign(moveX), y, Obj_wall))
        x += sign(moveX);
    moveX = 0;
}

///check for vertical collision
if(place_meeting(x, y + moveY, Obj_wall)) {
    while(!place_meeting(x, y + sign(moveY), Obj_wall))
        x += sign(moveY);
}

///move object
    x += moveX;
    y += moveY;

///handle sprite direction
    switch(dir){
        case 0: sprite_index = spr_walkright; break;
        case 45: sprite_index = spr_walkdur; break;
        case 90: sprite_index = spr_walkup; break;
        case 135: sprite_index = spr_walkdul; break;
        case 180: sprite_index = spr_walkleft; break;
        case 225: sprite_index = spr_walkddl; break;
        case 270: sprite_index = spr_walkdown; break;
        case 315: sprite_index = spr_walkddr; break;
    }
} else {
    image_index = 0;
}
 

SoapSud39

Member
When you switch sprite index, if the pixels of all your sprites aren't exactly aligned, you'll run into this problem if you don't have a set sprite for collision mask. I'm assuming you don't, in which case the changing of your collision mask from sprite to sprite causes your object to overlap with wall objects.

So what you can do is create a sprite (maybe one frame of your character sprite) and set its mask to what you want. Then on your player object set the collision mask (it's in the object editor) to be that sprite.


Also, in your vertical collision code, that should be y += sign(moveY) and you're missing the line moveY = 0;, which might be part of the problem.
 
When you switch sprite index, if the pixels of all your sprites aren't exactly aligned, you'll run into this problem if you don't have a set sprite for collision mask. I'm assuming you don't, in which case the changing of your collision mask from sprite to sprite causes your object to overlap with wall objects.

So what you can do is create a sprite (maybe one frame of your character sprite) and set its mask to what you want. Then on your player object set the collision mask (it's in the object editor) to be that sprite.


Also, in your vertical collision code, that should be y += sign(moveY) and you're missing the line moveY = 0;, which might be part of the problem.
Perfect! thanks! I'm pretty sure it was the collision mask.
 
Top