Hi, all
I want to change the attribute "Dynamic" of the body at runtime, can I achieve this purpose? Or can I create a new body and set it to the object? Anyway, it would be more convenient if I can set "Dynamic" to true/false at runtime. Thank you for any help if you can provide.
Comments
Currently you can't change a body type on the fly, sorry.
You can however remove a body and set a new one on the fly. If you do so, you'll be responsible for deleting that new body yourself though.
What are you trying to achieve exactly? Maybe there's an alternative to altering body types.
Thank you for your answer.
I want to make the dynamic ball be static when it collide with the walls. As your answer, I can create a static ball in the collision position and hide the dynamic ball.
By the way, why I can't change the body type on the fly, is it limit to the Box2D?
Do you know the reason that I can't change the body type on the fly? Is it limit to the Box2D?
Maybe some limitation in the integration with orx? I never really had to do something like this, so I never gave it any thoughts.
To complement Knolan's answer, it's not a limitation from Box2D per se, however it's not very well supported in the Box2D version used by orx.
Also note that switching from dynamic to static will lose the mass/inertia values (static objects don't have those), so if you used custom values, going back from static to dynamic would give you a body that wouldn't behave the same as expected as it'd have different mass/inertia parameters.
I can see three main approaches to work around this:
- switch the body on the fly yourself, but you'll be responsible for its deletion when the object gets deleted
- switch the object entirely
- use an object hierarchy: the parent only has the body and the child has everything else (graphic, sound, etc...), this is a compromise if you don't want to resetup the visual/audio part of your object (animation, playing sound, shader, etc...), however you'll have to do a bit of owner/parent juggling, nothing too bad though, probably 4-6 lines of code
I am using the way that switch the object entirely.