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

GameMaker Physics boxes unwanted corner collision

Hello GMC,

I have a physics question, but first, a screenshot:
1589836877599.png
My game is simple in nature, a platformer using the physics engine: my character can move and jump. When my character is touching the wall and jumping, everything works fine.
But when I jump and in this case hold left to move left, my character would bump his head mid-jump, in between both boxes that form the wall, like this:
(Also, if I would jump over the midsection then hold left, my character would float at the midsection)
1589837052831.png when the normal jump height is: 1589837090089.png


The walls physics collision shape is a box fitting the outline of the sprite and for good measure, I'll paste the characters step code below.
Can anyone help me find the solution? I'll add more info if needed.

OINL

GML:
move = keyboard_check(ord("D")) - keyboard_check(ord("A"));
jump = keyboard_check_pressed(ord("W"));
push = mouse_check_button(mb_left);
pull = mouse_check_button(mb_right);
pushclick = mouse_check_button_pressed(mb_left);
pullclick = mouse_check_button_pressed(mb_right);
var grounded = physics_test_overlap(x, y+1, 0, o_Floor) or physics_test_overlap(x, y+1, 0, obj);

if(keyboard_check_pressed(ord("R"))) room_restart();

switch (state)
{
    case s.normal:
    
    if(mouse_x <= x) image_xscale = -1;
    else image_xscale = 1;
    
    if(jump and grounded)
    {
        phy_speed_y = 0;
        physics_apply_impulse(x,y,0,-80);
    }
    if(move != 0)
    {
        phy_position_x += move*3;
        if(grounded) physics_apply_force(x,y,120*move,0);
        else physics_apply_force(x,y,50*move,0);
    }
    
    if((pushclick or pullclick) and !psy)
    {
        psy = true;
        image_index = 0;
    }
    if(psy)
    {
        if(psycounter == 0 or psycounter == 15 or psycounter == 30) force++;
        if(move != 0 and (pull or push) and grounded)
        {
            if(move == image_xscale) sprite_index = RunPsy;
            else
            {
                sprite_index = RunBackPsy;
                effect_create_below(ef_smoke,x,y+5,0,c_ltgray);
            }
        }
        else
        {
            if(push) sprite_index = PushCharge;
            else if(pull) sprite_index = PullCharge;
        }
        if(psycounter >= maxpsycounter)
        {
            effect_create_below(ef_smokeup,x+move*15,y-12,0,c_lime);
            psycounter = 0;
            force = 0;
            psy = false;
            if(move == 0) sprSwitch(Idle);
            else
            {
                if(move == image_xscale) sprite_index = Run;
                else
                {
                    sprite_index = RunBack;
                    effect_create_below(ef_smoke,x,y+5,0,c_ltgray);
                }
            }
        }
        else psycounter++;
    }
    else
    {
        if(grounded)
        {
            if(move != 0)
            {
                if(move == image_xscale) sprite_index = Run;
                else
                {
                    sprite_index = RunBack;
                    effect_create_below(ef_smoke,x,y+5,0,c_ltgray);
                }
            }
            else sprite_index = Idle;
        }
        else
        {
            if(in_range(phy_speed_y, -0.5, 0.5)) sprite_index = Midjump;
            else if(phy_speed_y < 0) sprite_index = Jump;
            else sprite_index = Fall;
        }
    }
    if(!in_range(phy_speed_x,-8,8)) phy_speed_x = clamp(phy_speed_x,-5,5);
    
    break;
}
 

Jezla

Member
You should make the wall one instance to prevent the player instance from colliding with the corners between wall block instances. If you have set the physics fixture of the wall block to the size of the sprite, then you can stretch one instance in the room editor and the fixture will be sized to match.

I also notice that in a few places you are setting the physics position directly. This breaks the simulation and you should not do it unless it's absolutely necessary. It's also not really necessary to use physics_test_overlap to check if you are grounded. Just check that your phy_speed_y is 0 and you will know that it is ok to jump.
 
Thanks for the reply Jezla,

I'll use the tips you gave me, but as for stretching an instance, i did that before posting here and it of course worked. However, the problem is still occuring when the wall is used as a platform:
1589842228709.png
In this case, I can't just stretch the instance. My character still bonks his head before reaching his max jump height. I thought I should correct the underlying issue.
Otherwise, have any idea for this specific scenario?

OINL
 

Jezla

Member
Have you turned on physics debug drawing to see what the fixtures are actually doing? It may be that the fixtures are not shaped or aligned as you expect them to be. It would also help to see your code for defining the fixtures.
 
Fixed. Taking out the "phy_position_x += move*3;" did it.
I don't know why my character got stuck in the corners, but it works fine now.

Thanks!
 
Top