can I change the body of any object at runtime

edited May 2015 in Help request
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

  • edited May 2015
    Hi hualin,

    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.
  • edited May 2015
    Hi, iarwain
    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?
  • edited May 2015
    Yup, you can do that, in case you haven't used yet, it is a great oportunity to use the heritage system. Your config will look something like this:
    [NewBall@OldBall]
    Body = NewBallBody
    
    [NewBallBody@oldBallBody]
    Dynamic = false
    
  • edited May 2015
    Thank you, Knolan.
    Do you know the reason that I can't change the body type on the fly? Is it limit to the Box2D?
  • edited May 2015
    I don't know the reason, taking a quick look on the box2d there are a few bugs reported for doing so, but they are very old and should probably be fixed by now.

    Maybe some limitation in the integration with orx? I never really had to do something like this, so I never gave it any thoughts.
  • edited May 2015
    Sorry for the delay, I haven't touched my computers over the past 2 days.

    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
  • edited May 2015
    Thank you, iarwain and Knolan.

    I am using the way that switch the object entirely.
  • edited May 2015
    Sorry for the late answer, hope it's working well for you!
Sign In or Register to comment.