Motion State help

killsto
Posts: 10
Joined: Mon Mar 30, 2009 2:54 am

Motion State help

Post by killsto »

Can somebody please explain to me how I use the motion state listed in the wiki? I'm using Irrlicht and replaced the Ogre parts with Irrlicht parts. The problem is it doesn't seem to update. I call getWorldTrans and setWorldTrans and nothing happens. Using the console, I found that the Initial Position that I set via the constructor isn't the same as the first time I grab it.

I modeled the rest of the code after the Hello World. This program worked and I had a scene rendered from it. I'll post my code, but it is a bit messy since I've been playing with it all afternoon. Right now, I'm just trying to get it to work in the console so please ignore the Irrlichtparts.

Code: Select all

#include <iostream>
#include <irrlicht.h>
#include <btBulletDynamicsCommon.h>



class MyMotionState : public btMotionState {
public:
	MyMotionState(const btTransform &initialpos/*, ISceneNode* node*/) {
	//	mVisibleobj = node;
		mPos1 = initialpos;
	/*	if (mVisibleobj)
		{
			mVisibleobj->setMaterialFlag(irr::video::EMF_LIGHTING, false);
		}*/
	}

	virtual ~MyMotionState() {
	}

	void setNode(irr::scene::IMeshSceneNode *node) {
		mVisibleobj = node;
	}

	virtual void getWorldTransform(btTransform &worldTrans) const {
		worldTrans = mPos1;
		//mPos1 = world ???
	}

	virtual void setWorldTransform(const btTransform &worldTrans) {
		//	if(NULL == mVisibleobj) return; // silently return before we set a node
		btQuaternion rot = worldTrans.getRotation();
		//mVisibleobj->setRotation(irr::core::vector3df(rot.getX(), rot.getY(), rot.getZ()));
		btVector3 pos = worldTrans.getOrigin();
		mPos1 = worldTrans; // ???
		//mVisibleobj->setPosition(irr::core::vector3df((irr::f32)pos.x(), (irr::f32)pos.y(), (irr::f32)pos.z()));
	}
	//irr::scene::IMeshSceneNode *mVisibleobj;
	btTransform mPos1;
};

int main (void)
{
	using namespace irr;

	using namespace core;
	using namespace scene;
	using namespace video;
	using namespace io;
	using namespace gui;

	IrrlichtDevice *device =
		createDevice(EDT_SOFTWARE, dimension2d<s32>(512, 384), 16,
		false, false, false, 0);

	IVideoDriver* driver = device->getVideoDriver();
	ISceneManager* smgr = device->getSceneManager();
	IGUIEnvironment* guienv = device->getGUIEnvironment();
	const c8 name =(c8)"fall";


	IMesh* cube = smgr->addSphereMesh(&name);
	//IMesh* cube2 = smgr->addSphereMesh(&name);
	IMeshSceneNode* node = smgr->addMeshSceneNode(cube);
	IMeshSceneNode* node2 = smgr->addMeshSceneNode(cube);







	smgr->addCameraSceneNodeFPS();

	btVector3 worldAabbMin(-10000,-10000,-10000);
	btVector3 worldAabbMax(10000,10000,10000);
	int maxProxies = 1024;
	btAxisSweep3* broadphase = new btAxisSweep3(worldAabbMin,worldAabbMax,maxProxies);

	btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
	btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);

	btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;

	btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,collisionConfiguration);

	dynamicsWorld->setGravity(btVector3(0,-10,0));


	btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),1);

	btCollisionShape* fallShape = new btSphereShape(1);

	btDefaultMotionState* groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,-1,0)));
	btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI(0,groundMotionState,groundShape,btVector3(0,0,0));
	btRigidBody* groundRigidBody = new btRigidBody(groundRigidBodyCI);
	dynamicsWorld->addRigidBody(groundRigidBody);

	MyMotionState* fall = new MyMotionState(btTransform(btQuaternion(0,5000,0)));
	btRigidBody::btRigidBodyConstructionInfo fallRigidBodyConstructionInfo(1,fall,fallShape);
	btRigidBody* fallRigidBody = new btRigidBody(fallRigidBodyConstructionInfo);
	dynamicsWorld->addRigidBody(fallRigidBody);




	/*
	for (int i=0 ; i<300 ; i++) {
	dynamicsWorld->stepSimulation(1/60.f,10);

	btTransform trans;
	fallRigidBody->getMotionState()->getWorldTransform(trans);

	std::cout << "sphere height: " << trans.getOrigin().getY() << std::endl;
	}
	*/
		
		
	while (true)
	{
		

		btTransform trans;
		fallRigidBody->getMotionState()->getWorldTransform(trans);
		fallRigidBody->getMotionState()->setWorldTransform(trans);
		std::cout << trans.getOrigin().getY()<< std::endl;
		dynamicsWorld->stepSimulation(1/60.f,10);
	}

	dynamicsWorld->removeRigidBody(fallRigidBody);
	delete fallRigidBody->getMotionState();
	delete fallRigidBody;

	dynamicsWorld->removeRigidBody(groundRigidBody);
	delete groundRigidBody->getMotionState();
	delete groundRigidBody;


	delete fallShape;

	delete groundShape;


	delete dynamicsWorld;
	delete solver;
	delete collisionConfiguration;
	delete dispatcher;
	delete broadphase;


	device->drop();

	return 0;

}
gabba
Posts: 9
Joined: Thu Mar 19, 2009 3:20 am

Re: Motion State help

Post by gabba »

I don't have time to look it up, but I find weird that you do this continuously:

Code: Select all

      fallRigidBody->getMotionState()->getWorldTransform(trans);
      fallRigidBody->getMotionState()->setWorldTransform(trans);
From what I remember reading from the Bullet manual, setWorldTransform(.....) is called by Bullet when an object moves: it's "don't call us, we'll call you" like in Holywood.