Issue with time to load many rigid bodies

Post Reply
Pottus
Posts: 6
Joined: Mon Nov 17, 2014 11:15 pm

Issue with time to load many rigid bodies

Post by Pottus »

I am having a problem with trying to add many rigid bodies for some reason the more I add the slower it gets to keep adding more (and I got a lot to add) is this a normal issue?

I am new to Bullet so I am hoping someone can take a look at this issue and figure out what I am doing wrong here or if there is a better way to this.

A bit more of an explanation of what this is for is for the GTA mod San Andreas Multiplayer and issue we have is there is currently no way to detect world collisions so this is being developed as a plugin to address that issue by using raytest. All the testing is working fine and indeed in game using the raytest is working perfectly against the few added objects. It's really only when I start adding thousands of rigid bodies the problem comes up any ideas would be appreciated thanks!

Code: Select all

	
	btRigidBody* addCollsionSphere(const btQuaternion& sphereRot, const btVector3& sphereOffset, const btVector3& spherePos, const btScalar sphereRadius)
	{
		// Translate rotation
		btVector3 NewPoint;

		NewPoint = transformPoint(sphereRot, sphereOffset);

		btSphereShape* sphere = new btSphereShape(sphereRadius);

		// Add offsets
		NewPoint += spherePos;

		// Set the default motion
		btDefaultMotionState* sphereposition = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), NewPoint));

		// Contruction body for the box
		btRigidBody::btRigidBodyConstructionInfo sphereRigidBodyCI(0, sphereposition, sphere, btVector3(0, 0, 0));
		
		// Create the rigid body
		btRigidBody* rigidBody = new btRigidBody(sphereRigidBodyCI);

		dynamicsWorld->addRigidBody(rigidBody);
		
		return rigidBody;
	}

	btRigidBody* addCollisionBox(const btQuaternion& boxRotation, const btVector3& boxMinXYZ, const btVector3& boxMaxXYZ, const btVector3& boxPos)
	{
		btVector3 boxCenter;
		btVector3 boxSize;

		// Calculate box center
		boxCenter = (boxMinXYZ + boxMaxXYZ) / 2;

		// Calculate box size
		boxSize = (boxMaxXYZ - boxMinXYZ) / 2;

		// Translate rotation
		btVector3 NewPoint;
		NewPoint = transformPoint(boxRotation, boxCenter);

		// Add offsets
		NewPoint += boxPos;

		// Create a box shape
		btBoxShape* box = new btBoxShape(boxSize);

		// Set the default motion
		btDefaultMotionState* boxposition = new btDefaultMotionState(btTransform(boxRotation, NewPoint));

		// Contruction body for the box
		btRigidBody::btRigidBodyConstructionInfo boxRigidBodyCI(0, boxposition, box, btVector3(0, 0, 0));

		// Create the rigid body
		btRigidBody* rigidBody = new btRigidBody(boxRigidBodyCI);
		dynamicsWorld->addRigidBody(rigidBody);
	
		return rigidBody;
	}

btRigidBody* addCollisionMesh(const btQuaternion& meshRotation, const btVector3& meshPos, const struct trianglebtVector triangles[], const int size)
	{
		// Create a triangular mesh
		btTriangleMesh* trimesh = new btTriangleMesh();

		for (int i = 0; i < size; i++)
		{
			// Add triangle faces
			trimesh->addTriangle(triangles[i].p1, triangles[i].p2, triangles[i].p3);
		}

		// Instantiate a triangular mesh shape
		btBvhTriangleMeshShape* shape = new btBvhTriangleMeshShape(trimesh, true);

		// Set the default motion
		btDefaultMotionState* shapeposition = new btDefaultMotionState(btTransform(meshRotation, meshPos));

		// Contruction body for the shape
		btRigidBody::btRigidBodyConstructionInfo shapeRigidBodyCI(0, shapeposition, shape, btVector3(0, 0, 0));

		// Create the rigid body
		btRigidBody* rigidBody = new btRigidBody(shapeRigidBodyCI);
		dynamicsWorld->addRigidBody(rigidBody);

		return rigidBody;
	}
Thanks a lot for looking at this problem.
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Issue with time to load many rigid bodies

Post by drleviathan »

The time is probably being spent in dynamicsWorld->addRigidBody(rigidBody) since that is the call we would expect to take more time as the object count in the world increases. I think you should hack the Bullet libs and add some debug timers inside addRigidBody(), and subsequent calls, to zero in on the source of the problem.
Pottus
Posts: 6
Joined: Mon Nov 17, 2014 11:15 pm

Re: Issue with time to load many rigid bodies

Post by Pottus »

Sounds like it could be something to try, another idea I had was to use compound objects and combine all elements of each object the GTA engine breaks them down into Spheres, Boxes, Meshes (obviously) but currently the way I have it set up is to instantiate a rigid body for each instance this is probably not the way to go. The next step for us right now is to actually get the whole world created and tested. Again if anyone has any suggestions I am all ears thanks for your reply.
Pottus
Posts: 6
Joined: Mon Nov 17, 2014 11:15 pm

Re: Issue with time to load many rigid bodies

Post by Pottus »

Okay well indeed everything worked out fine once I created all the rigid bodies with the loaded data there was no issues with load time consider this problem solved.
Post Reply