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

Legacy GM [SOLVED]Bizzare physics stuff

S

Samus

Guest
I'm using Game Maker Studio's built in Physics engine to make a platformer game. Usually I use my own engine, but I decided to experiment with the one that's already there. I have two objects: obj_ground and obj_mario. Both objects have rectangle fixtures, and to make the player move I apply a force to it based on Key input. I place multiple ground blocks in the room to make the floor, and have put some more blocks elevated above the other ones so you can jump on them if desired. Strangely, moving Mario will occasionally result in him getting stuck on one of the ground blocks, even though they are all level and moving backwards and then moving forwards again will let you pass. Also, jumping on top of one of the elevated blocks and then falling off of them will result in Mario being suspended a few pixels above the ground. He can still move, but he can't jump. Here is some code:
Code:
///Mario step event
var hori = -keyboard_check(vk_left) + keyboard_check(vk_right);
physics_apply_force(x, y, hori * 30, 0);
if(keyboard_check(vk_shift))
{
    if(phy_speed_x > 2 * speed_limit) phy_speed_x = 2* speed_limit;
    if(phy_speed_x < -2 * speed_limit) phy_speed_x = -2 * speed_limit;
    if(hori != 0)
    {
        image_xscale = hori;
        sprite_index = spr_MarioNESRunning
    }
    else sprite_index = spr_MarioNES
}
else
{
    if(phy_speed_x > speed_limit) phy_speed_x = speed_limit;
    if(phy_speed_x < -speed_limit) phy_speed_x = -speed_limit;
    if(hori != 0)
    {
        image_xscale = hori;
        sprite_index = spr_MarioNESWalking
    }
    else sprite_index = spr_MarioNES
}
if(jump = 1)
{
    sprite_index = spr_MarioNESJumping;
    if(place_meeting(x, y + 1, obj_ground)) && (time = 1) jump = 0;
}
if(sign(phy_speed_x) != hori) && (hori != 0) && (jump = 0) && (!place_meeting(x + hori, y, obj_ground)) sprite_index = spr_MarioNESTurn;
Code:
//Mario Key space event
if(place_meeting(x, y + 1, obj_ground))
{
    physics_apply_force(x, y, 0, -300);
    jump = 1;
    alarm_set(0, 2);
    time = 0;
}
Code:
///Mario Alarm zero
time = 1;
None of the objects are solid, they have friction and Mario has density, all other values are 0. This doesn't make any sense to me and I can't find any documentation that helps. Thank you in advance!
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Strangely, moving Mario will occasionally result in him getting stuck on one of the ground blocks, even though they are all level and moving backwards and then moving forwards again will let you pass.
This is a known issue with the Box 2D physics library... You have two choices really, either change the fixture type to Edge or CHain and draw your levels from point-to-point, or optimise the objects you are using for collisions so that they "join" together to create larger, break-free fixtures (something like this ;) ).
 
S

Samus

Guest
I did what you said, and it no longer gets caught on blocks. It still levitates though. Is that another issue with Box2D, or is something wrong with my code?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
I did what you said, and it no longer gets caught on blocks. It still levitates though. Is that another issue with Box2D, or is something wrong with my code?
That doesn't seem like a known Box2D issue... One thing I will say, is that if you are using the physics functions, you should be using them for everything, and in your code you're using place_meeting, when you should really be using physics_test_overlap. Not sure if that'll fix this issue, but it's certainly something that I know from experience can cause problems.
 
S

Samus

Guest
I removed all instances of place meeting, now it doesn't levitate as much but still does sometimes, though now you can jump even if you are levitating. I tried defining the fixture through code instead of with the GUI, and it doesn't have the bug but it seems a lot heavier, it can't jump half as high and it moves slower. Is it because it combines the weight of the GUI defined fixture and the fixture I made? If so, how can I "un-bind" the GUI fixture?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
If you have a fixture assigned to the object in code, simply un-ticking "use physics" in the object property should be all you need to do to disable the GUI fixture settings.
 
S

Samus

Guest
Thanks! You've been very helpful.
I found out that replacing "x" and "y" with "phy_position_x" and "phy_position_y" did the trick, but I will be defining my fixtures through code from now on. Thanks again!
 
Top