Dynamic Object Slides on a Static Object

Post Reply
moeizle
Posts: 8
Joined: Mon Jan 05, 2015 1:47 pm

Dynamic Object Slides on a Static Object

Post by moeizle »

Hello,

I have a very simple world, where i have a ground object (static) with the collision shape of a cube mesh that the user provides, and another cube (dynamic), smaller than the ground cube, with the collision shape provided also by the user.

I place the dynamic cube on top of the static one and run the simulation with no forces, nothing happen, as expected, i just run this step to make sure that no collisions occur.

However, when i set the gravity to (0,-9.8,0), the dynamic cube seems to rollover and slide on the ground cube for no obvious reason!!

both cubes are in contact at the x/z plane, so the expected behavior should be, than the dynamic cube remains still, however, it doesn't, and i don't know why, can anyone help me out with this?

This is the constructor of my class, and the initphysics() function

Code: Select all

mechanism::mechanism(bool enable_gravity)
{
	if(enable_gravity)
		gravity=btVector3(0,-9.8,0);
	else
		gravity=btVector3(0,0,0);
	initPhysics();
	joints_size=0;
}

Code: Select all

void mechanism::initPhysics()
{
	//1
    broadphase = new btDbvtBroadphase();
 
    //2
    collisionConfiguration = new btDefaultCollisionConfiguration();
    dispatcher = new btCollisionDispatcher(collisionConfiguration);
 
    //3
    solver = new btSequentialImpulseConstraintSolver();
 
    //4
    dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
 
    //5
    dynamicsWorld->setGravity(gravity);
}
and here is the function i use to set up the models

Code: Select all

void mechanism::set_up_models(vector<Model> m,graph g,vector<descriptor> state,vector<int> movable)
{
	btRigidBody* trb;
	btDefaultMotionState* tms;
	btCollisionShape* tcs;
	btTriangleMesh* ttm;
	btConvexTriangleMeshShape* tctms;
	btTypedConstraint* ttc;
	//btHingeConstraint* ttc;
	double pi=3.14159265359;
	vector<int>::iterator it;

	//create fall shape(s) and set them up
	for(int i=0;i<m.size();i++)
	{
		fallRigidBodies.push_back(trb);
		fallMotionStates.push_back(tms);
		fallShapes.push_back(tcs);
		T.push_back(ttm);

		////new
		T[i]=new btTriangleMesh();
		
		for(int j=0;j<m[i].f.size();j++)
		{
			int a=m[i].f[j].a-1;
			int b=m[i].f[j].b-1;
			int c=m[i].f[j].c-1;

			btVector3 v0(m[i].v[a].x,m[i].v[a].y,m[i].v[a].z);
			btVector3 v1(m[i].v[b].x,m[i].v[b].y,m[i].v[b].z);
			btVector3 v2(m[i].v[c].x,m[i].v[c].y,m[i].v[c].z);

			T[i]->addTriangle(v0,v1,v2);
		}
		fallShapes[i]=new btConvexTriangleMeshShape(T[i]);
		fallShapes[i]->setMargin(0.f);
		fallMotionStates[i] =new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 0, 0)));
		////end new
		
		btScalar mass;
		btVector3 fallInertia(0, 0, 0);
		it=std::find(movable.begin(),movable.end(),i);
		if(it==movable.end())
		{
			mass=0;
		}
		else
		{
			mass=m[i].get_bb_volume();
		}

		fallShapes[i]->calculateLocalInertia(mass, fallInertia);
		btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass, fallMotionStates[i], fallShapes[i], fallInertia);
		
		fallRigidBodyCI.m_restitution=0.0f;
		//fallRigidBodyCI.m_friction=0.0f;

	
		fallRigidBodies[i] = new btRigidBody(fallRigidBodyCI);

		fallRigidBodies[i]->setRestitution(0.0f);
		//fallRigidBodies[i]->setFriction(0.0f);

		dynamicsWorld->addRigidBody(fallRigidBodies[i]);
	}
	fallRigidBodies_size=m.size();
}
thank you
moeizle
Posts: 8
Joined: Mon Jan 05, 2015 1:47 pm

Re: Dynamic Object Slides on a Static Object

Post by moeizle »

I tried setting the 'restitution' to a non-zero value, but still that didn't work!.
moeizle
Posts: 8
Joined: Mon Jan 05, 2015 1:47 pm

Re: Dynamic Object Slides on a Static Object

Post by moeizle »

I also tried using bulletshapes, like btshpereshape, it worked fine, the slidding stopped, i don't know why!!

but i need to draw my own mesh, so i'm stuck with using btconvexhullshape or bttrianglemesh, and both do this wired slidding in the direction 'out of the screen'

any help here will be really appreciated.
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Dynamic Object Slides on a Static Object

Post by drleviathan »

I wonder if your cube has a center of mass that it at the surface, or outside of, the shape itself. That is, are the mesh points in the local frame such that the origin is at their center (or at least inside the convex hull of points)? If not then the center of mass of the object will reside outside the shape itself and would cause the object to roll over onto its side.

BTW, I think (not 100% sure) setting the margin to 0.0 is not a good idea -- it causes the objects to interpenetrate constantly and, besides making collision calculations a little bit more expensive than they would otherwise be, their collision responses will be dominated by the penetration resolution logic rather than contact points.

Finally (and this is just a guess in case the other ideas are not the problem) does the btTriangleMesh need the triangles to be wound properly by the right-hand-rule? And if so, are you sure your triangles are properly wound? If triangle winding matters then I would expect bad winding to make for very strange collision responses (snagging and glitchy penetrations).
Post Reply