• 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 Changing properties on physics fixtures?

J

Jacob Snyder

Guest
Hello,

I am trying to create a system where I can change some of the properties of a physics fixture mid-game. I have read that you have to unbind, change the properties, and then rebind the fixture to do this.

However, each time I do that not only does the desired change not happen, but doing it a bunch of times causes a memory leak. Im doing what the manual very vaguely told me to do, but what am i missing? Here is the code:

GML:
physics_remove_fixture(fix_main,self);
physics_fixture_set_density(fix_main,0); //sets the density to 0 for the chargeup
physics_fixture_bind(fix_main,self);
the fixture is set up in the create event btw

Thanks!
 

Roldy

Member
Hello,

I am trying to create a system where I can change some of the properties of a physics fixture mid-game. I have read that you have to unbind, change the properties, and then rebind the fixture to do this.

However, each time I do that not only does the desired change not happen, but doing it a bunch of times causes a memory leak. Im doing what the manual very vaguely told me to do, but what am i missing? Here is the code:

GML:
physics_remove_fixture(fix_main,self);
physics_fixture_set_density(fix_main,0); //sets the density to 0 for the chargeup
physics_fixture_bind(fix_main,self);
the fixture is set up in the create event btw

Thanks!
I've only messed with the physics a little, but my understanding is when you create a fixture you will need to use the ID returned by physics_fixture_bind.

The fixture you create with physics_fixture_create is a definition. And when calling physics_fixture_bind it will make a copy. It doesn't actually bind the fixture you created. That ultimately needs to be released with physics_fixture_delete. If you aren't doing that then you probably have a memory leak.

For density supposedly you can change that without rebinding. Read here: https://docs2.yoyogames.com/source/_build/3_scripting/4_gml_reference/physics/fixtures/index.html

Those use the ID returned by physics_fixture_bind:

GML:
var _fixtureDefinition = physics_fixture_create();
// define some stuff

myFixtureID = phyiscs_fixture_bind(_fixtureDefinition, self);

// Release this if we don't need it anymore
physics_fixture_delete(_fixtureDefinition);

// Changing density on the bound fixture
physics_set_density(myFixtureID, 0);
If you wanted to change more than those few properties then you would need to remove the fixture and rebind a redefined base fixture.

At least that is what I think needs to happen.
 

Jezla

Member
Yes, to change certain properties (friction, density, and restitution) of a bound fixture, you need to have stored the bound fixture's id in a variable when you bind it to its instance. Then you can use the various physics_set_* functions to change its properties. If you want to change any other properties (or to make a dynamic object static by changing the density to 0), you will need to remove the fixture and define and bind a new one, which is what you have attempted to do in your post. However, you cannot simply call the remove and bind functions as you have done here. You must define the new fixture's properties and bind it to the instance. Keep in mind that the manual advises you to delete a fixture after binding it to prevent memory leaks, because a created fixture takes up memory. If you are doing that correctly, then you will need to create a new fixture:

GML:
///Create the original fixture
var fix = physics_fixture_create();
/*
Define the fixture as you need to, setting density, friction, damping, etc.
*/

myfix = physics_fixture_bind (fix, id);

physics_fixture_delete(fix);

//Remove the fixture and recreate it
physics_remove_fixture(id, myfix);

var new_fix = physics_fixture_create();
/*
Define the new fixture as you need to, setting density, friction, damping, etc.
*/

myfix = physics_fixture_bind(new_fix, id);

physics_fixture_delete(new_fix);
If you find you need to do this a lot, you might be better off having two objects and just changing the instance as you need to. You would have to store the phy_speed_x and _y of the old instance and set those to the new instance to avoid any discontinuity in movement, but that would be easier than constantly changing fixtures back and forth.
 
Top