C4 Integration

FlashJackson
Posts: 2
Joined: Wed Oct 11, 2006 7:09 pm

C4 Integration

Post by FlashJackson »

Erwin,

I decided to go ahead and post my questions in this topic. That way other people can benefit from the questions and answers.

I have played with the code a little more. I now have a version of C4 with Bullet integrated at a very basic level. I am trying to modify my function to build the static collision mesh to work with Bullet. It compiles and runs, but I am getting an exception.

Here is the code I now have for collision registrations:

Code: Select all

void PhysicsMgr::BuildCollisionMesh(World *theWorld)
{

	Node *root = theWorld->GetRootNode(); // start at the root 
	Node *node = root; 
	while (node) 
	{ 
		// Step 1: Check  to make sure that this is a static geometry object
		if(node->GetNodeType() == kNodeGeometry && !node->GetController()) 
		{ 
			btCollisionShape	*collision=new btConvexHullShape(NULL,0,0); 
		
			Geometry *geometry = static_cast<Geometry *>(node); 
			const GeometryLevel *level = geometry->GetObject()->GetGeometryLevel(0); 
			long vertexCount = level->GetVertexCount(); 

			Point3D *vertex = level->GetArrayPoint3D(kArrayVertex); 
			
			const GeometryObject *object = geometry->GetObject(); 
			GeometrySpace space = object->GetGeometrySpace(); 

			
			// Step 2: Convert vertex coordinates if necassary and 
			// put each vertex into the btCollisionShape

			if (space == kGeometrySpaceWorld) 
			{ 
				const Transform4D& transform = geometry->GetInverseWorldTransform(); 
				const Transform4D& invTransform = geometry->GetWorldTransform();
				for (long i = 0; i < vertexCount; i++)
				{ 
					// transform to object space
					vertex[i] = transform * vertex[i]; 
					// add to collision shape
					static_cast<btConvexHullShape*>(collision)->addPoint(btVector3(vertex[i].x,vertex[i].y,vertex[i].z));
					// return to world space
					vertex[i] = invTransform * vertex[i];
				}
			}
			else
			{
				for (long i = 0; i < vertexCount; i++)
				{ 
					// no conversion necassary, just create the point
					static_cast<btConvexHullShape*>(collision)->addPoint(btVector3(vertex[i].x,vertex[i].y,vertex[i].z));
				}
			}
			
			// Step 3: Calcualte the world transform and convert to btTransform
						
			const Transform4D& transformC4World = node->GetWorldTransform();
			const Matrix3D& mat3= transformC4World.GetMatrix3D();
			btTransform shapeTransform;
			shapeTransform.setOrigin(btVector3(transformC4World.GetTranslation().x,transformC4World.GetTranslation().y,transformC4World.GetTranslation().z));
			shapeTransform.setBasis(btMatrix3x3(mat3.GetRow(0).x,mat3.GetRow(1).x,mat3.GetRow(2).x,mat3.GetRow(0).y,mat3.GetRow(1).y,mat3.GetRow(2).y,mat3.GetRow(0).z,mat3.GetRow(1).z,mat3.GetRow(2).z));
			
			// Step 4: use all of this to create rigid body and add to the world.

			btRigidBody* body = new btRigidBody(0.0f,shapeTransform,collision);
			m_dynamicsWorld->addRigidBody(body);

			delete collision;
			delete body;

			
		} 
		node = root->GetNextNode(node); 
	}
	 
}
I have traced the code and the conversions I am making from C4 types to Bullet types appear to be working. However, on about the 2nd body I try to register, I get an exception. I tried different data sets and got the error in different places. I tracked the assertion to here:

Code: Select all

void	btCollisionWorld::addCollisionObject(btCollisionObject* collisionObject,short int collisionFilterGroup,short int collisionFilterMask)
{

	//check that the object isn't already added
	std::vector<btCollisionObject*>::iterator i =	std::find(m_collisionObjects.begin(), m_collisionObjects.end(), collisionObject);
	assert(i == m_collisionObjects.end());
That assert is throwing the exception. I am not smart enough to follow what is going on here, so can you tell me what could cause this exception to be thrown? Do you see something in the above code that would cause the problem?

The dataset I am using for this test is very simple. It is four walls, a ceiling, and a floor. Each are planes and have 4 vertexs.

Thanks
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Post by Erwin Coumans »

You should not delete the rigidbody and collisionshape, immediately after you add them to the dynamics world.

They can only delete them after the simulation is terminated/when they are removed from the world.
FlashJackson
Posts: 2
Joined: Wed Oct 11, 2006 7:09 pm

Post by FlashJackson »

I just asked about this in a PM. Thanks for the help. I have to think of an elegant way to handle that.