Setting an object's position programmatically

Post Reply
Starfox
Posts: 37
Joined: Wed Jul 27, 2011 12:01 am

Setting an object's position programmatically

Post by Starfox »

I have a bunch of objects with attached rigid bodies and motion states, but sometimes I want to instantly move an object (for example, teleport object X to position and orientation Y) - how do I do that with Bullet? I basically want to tell Bullet that the object's transform has changed and that it needs to get the new transform from the motion state and consider that the canonical one as a one-time thing.
Jonathan
Posts: 36
Joined: Sun Feb 10, 2013 6:52 pm

Re: Setting an object's position programmatically

Post by Jonathan »

This will probably be helpful:

http://bulletphysics.org/mediawiki-1.5. ... tionStates


You may also want to look into the CF_KINEMATIC_OBJECT flag, if you're going to be purely moving the body through setting its transform.
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Setting an object's position programmatically

Post by Flix »

The suggested way to do it is to remove/readd the btRigidBody from/to the world.

Some less-official fonts suggest doing something like this:

Code: Select all

rigibBody->setWorldTransform(newT);
rigibBody->setLinearVelocity(btVector3(0,0,0));
rigibBody->setAngularVelocity(btVector3(0,0,0));            m_dynamicsWorld->getPairCache()->cleanProxyFromPairs(rigibBody->getBroadphaseProxy(),m_dynamicsWorld->getDispatcher());
rigibBody->activate();
, just in case one does not want to retrieve the rigid body collision filter group and mask flags, that are necessary to readd the body to the world.

However both approaches have some disadvantages: other bodies sharing constraints with the one to remove/readd may be in some way affected (for example using the second approach in some cases I can experience a lag of a few frames until the other bodies snap to their new place).

So wherever possible, I think it's better to just move objects with forces/torques/linear and angular velocities (or ad-hoc world space contraints); in other cases, it's possible to fully destroy an object (with all connected parts) and recreate it from scratch.
Starfox
Posts: 37
Joined: Wed Jul 27, 2011 12:01 am

Re: Setting an object's position programmatically

Post by Starfox »

That sounds like a complexity overkill - hasn't anyone asked for a simpler way to do that to be added to Bullet? I'd assume teleporting objects would be required in a few game scenarios...
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Setting an object's position programmatically

Post by Flix »

Starfox wrote:That sounds like a complexity overkill - hasn't anyone asked for a simpler way to do that to be added to Bullet? I'd assume teleporting objects would be required in a few game scenarios...
What approach are you referring to? Both do not seem too complex to me.
For single rigid bodies both methods work without much problems...

The only problem is about constraints (i.e. warping many rigid bodies connected by constraints together): maybe disabling all the constraints with btTypedConstraint::setEnabled(false), warping all connected bodies and re-enabling them after the warp could fix the problem I experienced (not tested, but maybe we need at least one physic-step with the constraint disabled to make it work...and I don't know if over-stepping the world is a good solution...).
Post Reply