Confused

AntiMatter
Posts: 6
Joined: Tue Mar 05, 2013 11:28 pm

Confused

Post by AntiMatter »

Hello,

I'm trying to learn the Bullet Physics system, but am having some problems. Mainly that when I create a rigid body in the world, it starts at the position indicated, but then swings out 800 or so units away from the area it started in the next step. Its really just a simple thing, but i don't know where i am goofing.

Essentially there is a base class which creates the basic objects of the system if they have not been created yet. Since the members a re static, they are shared by all the members of the deriving classes. I have a world object, and various solid objects that derive from this base class. The world object steps the simulation, and the box object updates its graphic position from the MotionState. It then draws itself.

Here is the initialization of the base class:

Code: Select all

class cPhysicsBase
{
public:
	cPhysicsBase()
	{
		if(!CollisionConfiguration)
			 CollisionConfiguration = new btDefaultCollisionConfiguration();

		if(!BroadPhase)
			BroadPhase = new btDbvtBroadphase();

		if(!Dispatcher)
			Dispatcher = new btCollisionDispatcher(CollisionConfiguration);

		if(!Solver)
			Solver = new btSequentialImpulseConstraintSolver;

		if(!World)
		{
			World = new btDiscreteDynamicsWorld(Dispatcher,BroadPhase, Solver,CollisionConfiguration);
			World->setGravity(btVector3(0,0,-10));
		};
	};
The gravity is set to the negative Z axis because the coordinate system i am using has the negative y pointing up. (i think this is a left hand orientation).

Here is the constructor for the cBox class:

Code: Select all

cBox(sf::Texture& Texture, sf::Vector3<float> Position, bool bDynamic = true, btScalar Mass = 0, btScalar Size = 50):
	Sprite(Texture)
	{
		Sprite.setPosition(sf::Vector2<float>(Position.x, Position.y));
		Shape = new btBoxShape(btVector3(Size, Size, Size));
		btVector3 localInertia(0,0,0);
		if(bDynamic)
		{			
			Shape->calculateLocalInertia(Mass, localInertia);
		}

		btTransform startTransform;
		startTransform.setIdentity();
		startTransform.setOrigin(btVector3(Position.x, Position.y, Position.z));

		btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
		btRigidBody::btRigidBodyConstructionInfo rbInfo(Mass,myMotionState,Shape,localInertia);
		Body = new btRigidBody(rbInfo);
		AddRigidBody(Body);
		AddCollisionObject(Shape);
		btTransform trans;
		Body->getMotionState()->getWorldTransform(trans);
		std::cout<<trans.getOrigin().getX()<<" "<<trans.getOrigin().getY()<<" "<<trans.getOrigin().getZ()<<std::endl;
	};
and here is the creation of the object in the main:

Code: Select all

cBox Box(BoxTex, sf::Vector3<float>(20, 20, 0), true, 100, 128);
cBox Ground(GroundTex, sf::Vector3<float>(0,0,0), false, 0, 800);

Essentially what i am doing here is creating two boxes, the first is dynamic, located at 20, 20, 0, and is a 128x128x128 cube. The second is not dynamic, is a cube of 800x800x800, and is located at the origin.

When i step the simulation the original position of the dynamic cube starts at the home location, but moves out 800 units down the x axis. I don't know why it is doing this. It then stops, but continues to fall through the -z direction.

Any help would be appreciated.
AntiMatter
Posts: 6
Joined: Tue Mar 05, 2013 11:28 pm

Re: Confused

Post by AntiMatter »

Never mind. I got it now.

Thanks for looking.