[Solved] Manually repositioning dynamic bodies...

Zeal
Posts: 47
Joined: Thu Oct 18, 2007 6:49 am

[Solved] Manually repositioning dynamic bodies...

Post by Zeal »

So I know you are not supposed to reposition dynamic bodies manually, but I am trying to do something so simple here, I just dont see why I am having problems... I create two rigid bodies (both using a simple box shape), and initialize them with a identity transform. Now, since they are both stacked ontop of each other, when I run my app one of the bodies goes flying off as expected (while the other seems to be frozen in space for some odd reason). Anyway, I know I could just set their initial transforms when I create the bodies, but shouldnt I be able to reposition them both AFTER they have been created (at 0,0,0), but BEFORE I step the simulation for the first time? Do I have no window of time to move things around before I start the simulation?

Well every time I try to reposition the bodies (again, before the simulation is stepped for the first time), the SECOND body repositions just fine, but the first just sits there frozen in space (at 0,0,0). I have tried this with and without a motion state. I have tried using every 'set transform' method I could find, and none of them seem to work...

Code: Select all

	body->setWorldTransform(trans);
	body->getMotionState()->setWorldTransform(trans);
	body->setInterpolationWorldTransform(trans);
	body->setWorldTransform(trans);
Is my only option to delete the body, recreate it, then re add it to the simulation (with the new transform). There has to be a better way.. what if you have a lot of high frequency bodies that you want to recycle without having to constantly new/delete them (like rockets or other projectiles)?

Thanks for any help
Last edited by Zeal on Tue Oct 13, 2009 1:47 am, edited 1 time in total.
nigul
Posts: 12
Joined: Tue Sep 22, 2009 1:40 pm

Re: Manually repositioning dynamic bodies...

Post by nigul »

Hello, I had the same problem in my program, where btRigidBodies had to be positionned wrt an Ogre::SceneNode rendered object

I actually do manual repositionning of dynamic bodies in the following way :

Code: Select all

	Vector3 _ogrePos = getAbsolutePosition(); 
	Quaternion _ogreQuat = getAbsoluteOrientation();

	BulletManager::sharedManager()->bulletWorld()->removeRigidBody(_body);

       // where bulletWorld() gives the btDynamicsWorld
	
	btTransform btt;
	btt.setOrigin(btVector3(_ogrePos.x,_ogrePos.y,_ogrePos.z)); // set here the desired position of the body
	btt.setRotation(btQuaternion(_ogreQuat.x,_ogreQuat.y,_ogreQuat.z,_ogreQuat.w));// set here the desired orientation
	_body->getMotionState()->setWorldTransform(btt);
	_body->setCenterOfMassTransform(btt);
	
		
	BulletManager::sharedManager()->bulletWorld()->addRigidBody(_body);
this works quite well for my application
Zeal
Posts: 47
Joined: Thu Oct 18, 2007 6:49 am

Re: Manually repositioning dynamic bodies...

Post by Zeal »

Thanks for the help, everything is working fine now. Disregard my earlier post, turns out it was just a bug in my code. The problem was purely visual - you see, I was using 'setUserPointer()' to associate the body with my Ogre scene node. However, the area of memory I was pointing to was moving around (the scene node was stored in a vector), which caused only the second body to point to valid data. When I fixed that, a simple 'body->setWorldTransform(trans);' was all it took to move the body perfectly.

I did run into one other problem with repositioning a static body (0 mass). But again, it was purely visual (since repositioning a static body doesnt change its activation state, and I am not using a motionState, Ogre didnt know to update the scene node).

So long story short, body->setWorldTransform(trans); works PERFECTLY. I didnt even have to remove/add the body to the world, or call setCenterOfMassTransform. It just 'works' now, with that ONE method. Wohoo!