Strange behaviour when setting collision shapes inertia

Post Reply
Void
Posts: 2
Joined: Sun Sep 27, 2015 7:12 pm

Strange behaviour when setting collision shapes inertia

Post by Void »

Hello, in the last couple of days I have managed to implement a basic physics functionality in my OpenGL program,
but I have some strange problems with shapes that are not spheres.

Here you can see the problem: https://www.youtube.com/watch?v=vmJkckITt6w

The problem is with the cube, it acts kinda strange (stands on a edge / ruberbands etc.)

Here is the function that creates the rigid body:

Code: Select all

		int addConvexHullRigidDynamic(btCollisionShape* colShape)
		{
			int id = rigidBodies.size();
			btTransform transf;
			transf.setIdentity();

			btMotionState* motionState = new btDefaultMotionState(transf);
			btScalar mass = 10.0f;
			btVector3 inertia(0, 0, 0);

			colShape->calculateLocalInertia(mass, inertia);

			btRigidBody::btRigidBodyConstructionInfo convexHullInfo = btRigidBody::btRigidBodyConstructionInfo(mass, motionState, colShape, inertia);

			btRigidBody* rigidBody = new btRigidBody(convexHullInfo);

			bltDynamicsWorld->addRigidBody(rigidBody);
			rigidBodies.push_back(rigidBody);
			return id;
		}
Previously I didnt calculate the local inertia, then I had the problem that objects wouldnt rotate, but then the cube was acting fine (only it didnt rotate).

The collision shape is created from a simpler convex hull shape that represents the triangle mesh that is rendered by OpenGL (I use blender to create the mesh for OpenGL
and then generate simpler convex hull shape from the mesh using a plugin in blender).

Here is a snippet how I create the shape for bullet in my app:

Code: Select all

		btTriangleMesh* trimesh = new btTriangleMesh();
			btVector3 triangleV1 = btVector3();
			btVector3 triangleV2 = btVector3();
			btVector3 triangleV3 = btVector3();

			for (int i = 0; i < elements.size(); i++)
			{
				glmVec4TobtVec3(vertices[elements[i][0]], triangleV1);
				glmVec4TobtVec3(vertices[elements[i][3]], triangleV2);
				glmVec4TobtVec3(vertices[elements[i][6]], triangleV3);

				trimesh->addTriangle(triangleV1, triangleV2, triangleV3);
			}

			btCollisionShape* collisionShape = 0;
			collisionShape = new btConvexTriangleMeshShape(trimesh);
If anyone has a quick idea what could this be then I would really appreciate your help, thanks!
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Strange behaviour when setting collision shapes inertia

Post by drleviathan »

My guess would be that the "center of mass" of your shape is outside its bounding hull.

In Bullet the center of mass of the btRigidBody in the world-frame is its position, and in the shape's frame it is the origin. This means that if you use a mesh with an offset such that the origin is not contained inside the bounding hull of the collection of mesh points then the body's center of mass will also be outside its bounding hull and you'll get non-realistic tumbling.

To test this theory: (1) compute the average of the points that make up your mesh* then (2) subtract the average from each point and finally (3) create your shape using the shifted points.

BTW, the world-frame inertia tensor for a box should be a diagonal matrix which means that the inverse inertia tensor should also be diagonal and you can examine it using btRigidBody::getInvInertiaTensorWorld().

* In general, averaging the mesh points would only provide an approximate center of mas, but for a rectangular box mesh with exactly 8 points it would be exact.
Void
Posts: 2
Joined: Sun Sep 27, 2015 7:12 pm

Re: Strange behaviour when setting collision shapes inertia

Post by Void »

Thanks for the help, will try your suggestions later today.

You were right, the problems were caused because the shape was not at the origin, thanks again for your help!
Post Reply