btDiscreteDynamicsWorld::addRigidBody not working?

PaloDeQueso
Posts: 17
Joined: Thu Nov 09, 2006 8:50 pm
Location: Middletown, PA

btDiscreteDynamicsWorld::addRigidBody not working?

Post by PaloDeQueso »

I'm working on integrating bullet into my game engine, and am running into some issues. I have objects loaded from xml with rigid body data. Then those objects get added into my physics system. But the problem is, those collision objects do not show up in the vector btDiscreteDynamicsWorld::getCollisionWorld()::getCollisionObjectArray(). However if I add in a test rigid body and sphere collision shape, they show up in the list just fine.

This is my physics initialization, note the commented out test data, it showed up and worked quite well!

Code: Select all

Physics::Physics(EG::Base::Math::Vector3f _gravity){
	broadphase = new btDbvtBroadphase();
	collision_configuration = new btDefaultCollisionConfiguration();
	dispatcher = new btCollisionDispatcher(collision_configuration);
	solver = new btSequentialImpulseConstraintSolver();
	dynamics_world = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collision_configuration);

	gravity = _gravity;
	dynamics_world->setGravity(btVector3(gravity.X(), gravity.Y(), gravity.Z()));

	// THESE WORK QUITE WELL, BUT ARE ONLY TEST DATA!!!
	// TEST This is a bottom line. muahahah
	//btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,-10,0),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);
	//dynamics_world->addRigidBody(groundRigidBody);

	// TEST Test shape
	//btCollisionShape* fallShape = new btSphereShape(1);
	//btDefaultMotionState* fallMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,50,0)));
	//btScalar mass = 1;
	//btVector3 fallInertia(0,0,0);
	//fallShape->calculateLocalInertia(mass,fallInertia);
	//btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass,fallMotionState,fallShape,fallInertia);
	//fallRigidBody = new btRigidBody(fallRigidBodyCI);
	//dynamics_world->addRigidBody(fallRigidBody);
	//std::cout << "Physics Initialized" << std::endl;
}
Here is my AddObject function for the physics class, it takes a game object and a rigid body, and puts them into the bullet simulation, or is supposed to. NOTE: the debug print you see gets called and prints:
"Adding "planet" Object to Physics System with mass: 1kg"

Code: Select all

void Physics::AddObject(EG::Engine::Game::Object *object, EG::Engine::Physics::RigidBody *rigid_body){
	EG::Engine::Physics::CollisionShape *collision_shape = rigid_body->GetCollisionShape();
	EG::Base::Math::Transformationf collision_shape_transformation = collision_shape->GetTransformation();
	btCollisionShape* bt_shape;
	bool all_set = false;
	if (collision_shape->GetType() == EG::Engine::Physics::CollisionShape::SPHERE){
		EG::Engine::Physics::SphereCollisionShape *sphere_collision_shape = static_cast<EG::Engine::Physics::SphereCollisionShape *>(collision_shape);
		btScalar radius = sphere_collision_shape->GetRadius();
		bt_shape = new btSphereShape(radius);
		bt_shape->setUserPointer(sphere_collision_shape);
		all_set = true;
	}

	if (all_set){
		btTransform bt_transform;
		bt_transform.setFromOpenGLMatrix(collision_shape->GetTransformation().Data());
		btDefaultMotionState *bt_motion_state = new btDefaultMotionState(bt_transform);
		btVector3 local_inertia(0, 0, 0);
		btScalar mass = collision_shape->GetMass();
		bt_shape->calculateLocalInertia(mass, local_inertia);
		btRigidBody::btRigidBodyConstructionInfo bt_rigid_body_construction_information(mass, bt_motion_state, bt_shape, local_inertia);
		btRigidBody *bt_rigid_body = new btRigidBody(bt_rigid_body_construction_information);
		bt_rigid_body->setUserPointer(rigid_body);
		rigid_body->SetBulletRigidBody(bt_rigid_body);
		bt_rigid_body->applyCentralForce(btVector3(0, -1, 0));
		dynamics_world->addRigidBody(bt_rigid_body); // can this fail? I've checked the btCollisionShape pointer!!!

		std::cout << "Adding \"" << object->Name() << "\" Object to Physics System with mass: " << collision_shape->GetMass() << "kg" << std::endl; // DEFINITELY GETTING CALLED
	}
}

And here is my physics update function, it has two approaches, one from my engine, and one from bullet, which is what I've had success with in the past. But the problem is, there doesn't appear to be any values in the collisionObjectArray from bullet as the count prints out 0. However, addRigidBody is definitely getting called from the AddObject function above!

Code: Select all

void Physics::Update(EG::Engine::Game::ObjectManager *objects, float time_step){
	dynamics_world->stepSimulation(time_step, 10);

	// update all of the objects after the simulation is run!

	// From an Engine point of view
	//btTransform bt_trans;
	//EG::Base::Math::Transformationf my_trans;
	//float gl_trans[16];
	//std::vector<unsigned int> object_ids = objects->ObjectIDs();
	//std::vector<unsigned int>::iterator object_id_iterator = object_ids.begin();
	//while (object_id_iterator != object_ids.end()){
	//	EG::Engine::Game::Object *object = objects->GetObject(*object_id_iterator);
	//	if (object->BasicAttributeCountType(EG::Engine::Game::ObjectAttribute::BASIC_TRANSFORMATION) > 0 && object->ControlAttributesCountType(EG::Engine::Game::ObjectAttribute::CONTROL_RIGID_BODY) > 0){
	//		EG::Engine::Game::ObjectTransformationAttribute *transformation_attribute = static_cast<EG::Engine::Game::ObjectTransformationAttribute *>(object->BasicAttributesType(EG::Engine::Game::ObjectAttribute::BASIC_TRANSFORMATION)->at(0));
	//		EG::Engine::Game::ObjectRigidBodyControlAttribute *rigid_body_attribute = static_cast<EG::Engine::Game::ObjectRigidBodyControlAttribute *>(object->ControlAttributesType(EG::Engine::Game::ObjectAttribute::CONTROL_RIGID_BODY)->at(0));
	//		EG::Engine::Physics::RigidBody *rigid_body = rigid_body_attribute->GetRigidBody();
	//		btRigidBody *bt_rigid_body = static_cast<btRigidBody *>(rigid_body->GetBulletRigidbody());
	//		bt_rigid_body->getMotionState()->getWorldTransform(bt_trans);
	//		bt_trans.getOpenGLMatrix(gl_trans);
	//		btVector3 origin = bt_trans.getOrigin();
	//		std::cout << "Height: " << origin.getY() << std::endl;
	//		my_trans.Set(gl_trans);
	//		transformation_attribute->SetTransformation(my_trans);
	//		}
	// 
	//		++object_id_iterator;
	//}

	// From a Bullet point of view
	btTransform bt_trans;
	EG::Base::Math::Transformationf my_trans;
	float gl_trans[16];
	std::cout << "collision object array COUNT: " << dynamics_world->getCollisionWorld()->getCollisionObjectArray().size() << std::endl;
	btCollisionObjectArray collision_objects = dynamics_world->getCollisionWorld()->getCollisionObjectArray();
	unsigned int object_count = collision_objects.size();
	for (unsigned int object_index = 0; object_index < object_count; object_index++){
		// Gather Bullet Data
		btCollisionObject *collision_object = collision_objects.at(object_index);
		btRigidBody* bt_body = btRigidBody::upcast(collision_object);
		bt_body->getMotionState()->getWorldTransform(bt_trans);
		bt_trans.getOpenGLMatrix(gl_trans);
		my_trans.Set(gl_trans);
		btVector3 origin = bt_trans.getOrigin();
		std::cout << "Object Index (" << object_index << ") Height: " << origin.getY() << std::endl;

		// Gather Engine Data
		RigidBody *rigid_body = static_cast<RigidBody *>(bt_body->getUserPointer());
		EG::Engine::Game::Object *object = rigid_body->GetObject();
		EG::Engine::Game::ObjectTransformationAttribute *transformation_attribute = static_cast<EG::Engine::Game::ObjectTransformationAttribute *>(object->BasicAttributesType(EG::Engine::Game::ObjectAttribute::BASIC_TRANSFORMATION)->at(0));

		// Set world transformation
		transformation_attribute->SetTransformation(my_trans);
	}
}
Any insight into this would definitely be appreciated!!!
PaloDeQueso
Posts: 17
Joined: Thu Nov 09, 2006 8:50 pm
Location: Middletown, PA

Re: btDiscreteDynamicsWorld::addRigidBody not working?

Post by PaloDeQueso »

*SOLVED*