• 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] Issue with scaling fixtures in a physics puzzle game

S

Sweedums

Guest
Hey there folks, i'm pretty new to GMS (and programming in general) but i'm trying to make a simple top down physics based puzzle game.

The core mechanic is that I want the player to be able to squash themselves flat or thin to squeeze through tight spaces and interact with things, i also want them to be able to inflate and deflate (imagine a puffer fish) however I'm having some issues with the changing of the collision bounding box whenever the player object changes size.

I looked up a few old threads dealing with similar situations but I'm hitting a brick wall so thought i'd try a new thread with my specific issue.

to get started, i've put in a key press event which triggers the player sprite to scale to double size (sudden inflation) and I'm creating a new fixture that then matches that size.... this seems to be working, however i also then have an alarm which triggers, causing the player to shrink back down to their original size (suddenly deflating... imagine holding breath, and then after a set time having to let it all out) and at this point, the large fixture still seems to be present, and so the player object, while visually being back to its normal size, has a large invisible collision box around it. I suspect i'm simply misunderstanding how to un bind, and rebind a new fixture, so here's my code for both events:

Key press (F) "Enlarge"
Code:
image_xscale = 2
image_yscale = 2
physics_remove_fixture(o_player,fixture)
fixturefat = physics_fixture_create();
physics_fixture_set_box_shape(fixturefat, sprite_width / 2, sprite_height / 2);
physics_fixture_set_density(fixturefat, 1);
physics_fixture_set_restitution(fixturefat, 0.2);
physics_fixture_bind(fixturefat, o_player);
physics_fixture_set_linear_damping(fixturefat, 1)
physics_fixture_delete(fixturefat);

alarm[0] = 60;
Alarm[0] "Shrink"
Code:
image_xscale = 1
image_yscale = 1
physics_remove_fixture(o_player,fixturefat)
fixture = physics_fixture_create();
physics_fixture_set_box_shape(fixture, sprite_width / 2, sprite_height / 2);
physics_fixture_set_density(fixture, 1);
physics_fixture_set_restitution(fixture, 0.2);
physics_fixture_bind(fixture, o_player);
physics_fixture_set_linear_damping(fixture, 1)
physics_fixture_delete(fixture);
Now if i'm understanding this right, im applying a new, large fixture called "fixturefat" and then trying to replace it with the standard sized "fixture".... but I suspect I'm being dumb somewhere in here. I have unchecked the "Uses physics" box on the player object and created the initial fixture in the code as I read that that can cause some issues.

Thanks in advance for any help!
 

Jezla

Member
There are two issues that I see. The first is not really related to your problem, but you may have issues at some point. You are setting the linear damping after you bind the fixture, so that property is not being applied to the instance receiving the fixture.

Secondly, physics_fixture_bind returns the id of the bound fixture. You need to store that in a variable, and refer to that when you remove the bound fixture.

Right now it looks as if you are referring to the variable that holds the blueprint fixture, which is why your fixtures are not being removed.
 
S

Sweedums

Guest
Ah thanks for the headsup on the linear damping, i was actually wondering why that was not working as expected, thats another issue ticked off the list!

when you say physics_fixture_bind returns the id of the bound fixture, I'm not sure i understand exactly, how do i find out that id?
 

Jezla

Member
You don't necessarily have to find out what the ID is, you just have to store it in a variable. The manual entry for physics_fixture_bind() shows an example. You would then use that variable as the argument for the physics_remove_fixture call.

Example - store the bound fixture ID:

Code:
//define your fixture properties...
myfix = physics_fixture_bind(fix, id);
Now you can remove that fixture:

Code:
physics_remove_fixture(oPlayer, myfix);
If you haven't already, I highly recommend doing the tutorials that were recently posted on the blog. They're essential (in my opinion) for getting off on the right foot when using the physics system.
 
S

Sweedums

Guest
ah I see, thank you so much! I did not realise the significance of the last part from the manual at first, but it seems to all be working as expected now, I appreciate the help Jezla!
 
Top