changing body type without change of center of mass

Posted by philipp on Game Development See other posts from Game Development or by philipp
Published on 2012-09-12T13:51:46Z Indexed on 2012/09/12 15:52 UTC
Read the original article Hit count: 330

Filed under:

I have an box2d project with some bodies in it, which move around without any user interaction. But if the user selects one of the bodies, he should be able to move it around just like he wants to.

To keep it short, I want to change the type of the body to "kinematic" while the user controls it and back to "dynamic" afterwards. If I do so the rotation center of the body changes with the change of the type. How can I reset this?

The body's fixture is created of a single b2PolygonShape, with its vertices set via SetAsArray().

Greetings philipp

EDIT::

So I looked around about setting the local center of bodies. what brought me to this solution:

var md = new b2MassData();
this.body.GetMassData( md );
this.body.SetType( b2body.b2_kinematicBody );
this.body.SetMassData( md );

that did not work, so I had a look at the source and found that SetMassData() always returns if the body is not "dynamic".

So I tried this:

var md = new b2MassData();
this._body.GetMassData( md );
this._body.SetType( b2Body.b2_kinematicBody );
this._body.m_sweep.localCenter.Set( md.center.x, md.center.y ); 

what actually is modifying the private data of the body. But it works and no errors appear, but can I really do this without the risk of breaking the application, or in other words, under which circumstances could this solution might cause errors?

n.b.: I am using box2dweb of the latest release.

Greetings philipp

© Game Development or respective owner

Related posts about box2d