Ogre + Bullet simple collision detection

Dealus
Posts: 2
Joined: Mon Feb 28, 2011 3:25 pm

Ogre + Bullet simple collision detection

Post by Dealus »

Hello, i'm trying to use Bullet as a simple collision engine.
So far, i could'nt manage to perform a single correct collision.

Hehe is my code to initialize Bullet, when creating a new scene

Code: Select all

bt_configuration = new btDefaultCollisionConfiguration();
bt_dispatcher= new btCollisionDispatcher(bt_configuration);
Bullet::bulletWorld = new btCollisionWorld(bt_dispatcher, broadphase, bt_configuration);

Bullet::bulletWorld is a btCollisionWorld*
Here is my code for initializing a collision object. I init the "collision" part when building my sceneNode for the previously created linked entity

Code: Select all

void MachineEntity::createNode()
{
ogreNode = sceneMgr->getRootSceneNode()->createChildSceneNode();
collisionShape = new btBoxShape((btVector3(btScalar(10),btScalar(10),btScalar(10))));
collisionShape->setMargin(0.0f);
setCollisionShape(collisionShape);
setCollisionFlags(btCollisionObject::CF_STATIC_OBJECT);

btTransform transform;
transform.setOrigin(btVector3(btScalar(ogreNode->getPosition().x), btScalar(ogreNode->getPosition().x), btScalar(ogreNode->getPosition().z )));
transform.setRotation(btQuaternion::getIdentity());
setWorldTransform(transform);
Bullet::bulletWorld->addCollisionObject(this);
}
The code to update a collision object's position

Code: Select all

void MachineEntity::update(float timeSinceLastFrame)
{
	Ogre::Vector3 currentPosition = machine->getPosition();
	ogreNode->translate(machine->getDirection() * timeSinceLastFrame);
	machine->setPosition(ogreNode->getPosition());
	btTransform transform;
	transform.setOrigin(btVector3(btScalar(ogreNode->getPosition().x), btScalar(ogreNode->getPosition().x), btScalar(ogreNode->getPosition().z )));
	transform.setRotation(btQuaternion::getIdentity());
	setWorldTransform(transform);
}
And finally, the code to check if any collision happened, run once per frame update

Code: Select all

        Bullet::bulletWorld->performDiscreteCollisionDetection();
	btCollisionObjectArray arrayBullet = Bullet::bulletWorld->getCollisionObjectArray();
	int numManifolds = Bullet::bulletWorld->getDispatcher()->getNumManifolds();
	for(int i=0; i<numManifolds; i++) {
		btPersistentManifold* pm = Bullet::bulletWorld->getDispatcher()->getManifoldByIndexInternal(i);
		if(pm->getNumContacts() > 0) {
			btCollisionObject* test1 = static_cast<btCollisionObject*>(pm->getBody0());
			btCollisionObject* test2 = static_cast<btCollisionObject*>(pm->getBody1());
			btVector3 pos = test2->getWorldTransform().getOrigin();
			m_pOgreHeadNode->setPosition(pos.x(),pos.y(), pos.z());
		}
		Bullet::bulletWorld->getDispatcher()->clearManifold(pm);
	}
This code should move the ogreHead to the collision point, but no collision is detected.
robagar
Posts: 48
Joined: Fri May 21, 2010 1:49 am

Re: Ogre + Bullet simple collision detection

Post by robagar »

Um, in the transform.setOrigin calls you have two .x's
Dealus
Posts: 2
Joined: Mon Feb 28, 2011 3:25 pm

Re: Ogre + Bullet simple collision detection

Post by Dealus »

Lol, well seen ^^
I'm thinking about the time lost for such a stupid error :(

thx anyway, works fine now :)