Collision shape does not rotate

whiplash
Posts: 16
Joined: Sun Dec 13, 2009 3:21 pm

Collision shape does not rotate

Post by whiplash »

Hello!

I use Ogre and Bullet. I create a hull approximation (btConvexTriangleMeshShape -> btShapeHull) from Ogre::Entity. I try world transformation to my btShapeHull, but my physic objects dont work correctly. My code:

Code: Select all

		...
		/* create btConvexHullShape... */
		...	

		btScalar mass = 60.0;
		btVector3 inertia(0.0, 0.0, 0.0);

		Ogre::Vector3 ent_pos = entHouse->getParentSceneNode()->_getDerivedPosition();
		Ogre::Quaternion ent_rot = entHouse->getParentSceneNode()->_getDerivedOrientation();

		btVector3 pos(ent_pos.x, ent_pos.y, ent_pos.z);
		btQuaternion rot(ent_rot.x, ent_rot.y, ent_rot.z, ent_rot.w);
		btTransform trans(rot, pos);
		btTransform center_of_mass;
		center_of_mass.setIdentity();
		btDefaultMotionState* mt_state = new btDefaultMotionState(trans, center_of_mass);

		btRigidBody::btRigidBodyConstructionInfo rb_info(mass, mt_state, conv_hull, inertia);		
		btRigidBody *body = new btRigidBody(rb_info);
		m_dynamics_world->addRigidBody(body);

		m_bodies.push_back(body);
		m_entities.push_back(entHouse);
Update states of objects:

Code: Select all

		m_dynamics_world->stepSimulation(evt.timeSinceLastFrame, 20);

		btTransform trans;
		Ogre::Vector3 entPos;
		Ogre::Quaternion entRot;
		for (int i = 0; i < m_bodies.size(); i++)
		{
			m_bodies[i]->getMotionState()->getWorldTransform(trans);
	
			entPos.x = trans.getOrigin().x();
			entPos.y = trans.getOrigin().y();
			entPos.z = trans.getOrigin().z();
			m_entities[i]->getParentSceneNode()->setPosition(entPos);
	
			entRot.x = trans.getRotation().x();
			entRot.y = trans.getRotation().y();
			entRot.z = trans.getRotation().z();
			entRot.w = trans.getRotation().w();
			m_entities[i]->getParentSceneNode()->setOrientation(entRot);
		}
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Collision shape does not rotate

Post by Erwin Coumans »

You forgot to compute the local inertia. This happened to a few people recently, so we probably need to add an assert or warning in the rigid body construction.
robagar
Posts: 48
Joined: Fri May 21, 2010 1:49 am

Re: Collision shape does not rotate

Post by robagar »

Also, that's not how motion states work. The documentation doesn't explain it terribly well, but basically the get and set functions are called by Bullet to update your objects.
  • getWorldTransform is called by Bullet to query the position & orientation of an object (for normal non-kinematic bodies, this only happens once. Kinematic objects are queried every step.)
  • setWorldTransform is called during the step to move your object
Basically with a motion state the position/orientation update happens automatically. You should not call either function in your code.

This is my motion state for a generic "Thing":

Code: Select all

ThingBulletMotionState::ThingBulletMotionState(Thing* pThing)
  :m_pThing(pThing)
{
}

void ThingBulletMotionState::getWorldTransform(btTransform &worldTrans) const
{
  Ogre::Quaternion orientation = m_pThing->WorldOrientation;
  worldTrans.setRotation(ogre2bullet(orientation));

  Ogre::Vector3 position = m_pThing->WorldPosition;
  worldTrans.setOrigin(ogre2bullet(position));
}

void ThingBulletMotionState::setWorldTransform(const btTransform &worldTrans)
{
  btQuaternion orientation = worldTrans.getRotation();
  m_pThing->WorldOrientation = bullet2ogre(orientation);

  btVector3 position = worldTrans.getOrigin();
  m_pThing->WorldPosition = bullet2ogre(position);
}
whiplash
Posts: 16
Joined: Sun Dec 13, 2009 3:21 pm

Re: Collision shape does not rotate

Post by whiplash »

Erwin Coumans, thanks, it is working!
robagar, ok, i try translate my code to your variant, because it is easy and stronge way :)