Cube droppin' weirdness

LaceySnr
Posts: 11
Joined: Fri Mar 20, 2009 11:11 am

Cube droppin' weirdness

Post by LaceySnr »

Hey guys,

I'm sure this is me being and idiot but was hoping somebody could help out... I'm dropping a couple of cubes, offset from each other, and they're landing in an impossible position. It could be rendering code but as you can see below it's pretty simple so I'm a bit confused as to what's going on. Any ideas?

Setup code:

Code: Select all

	pWorld->setGravity(btVector3(0, - GRABBITY, 0));

	// shapes needed (floor + cubes)
	tShapes[SHAPE_GROUND_PLANE].pShape = new btStaticPlaneShape(btVector3(0, 1, 0), 0);
	tShapes[SHAPE_CUBE_10].pShape = new btBoxShape(btVector3(BOX_SIZE_BY_2, BOX_SIZE_BY_2, BOX_SIZE_BY_2));

	// world static body - quaternion + origin
	tPhysicsBodies[0].pMS = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 0, 0)));
	tPhysicsBodies[0].pBody = new btRigidBody(btRigidBody::btRigidBodyConstructionInfo(0, tPhysicsBodies[0].pMS, tShapes[SHAPE_GROUND_PLANE].pShape, btVector3(0, 0, 0)));
	tPhysicsBodies[0].pBody->setRestitution(0.2);
	tPhysicsBodies[0].pBody->setFriction(0.25);

	pWorld->addRigidBody(tPhysicsBodies[0].pBody);

	// add our cube to the world...
	btVector3 vInertia;
	tShapes[SHAPE_CUBE_10].pShape->calculateLocalInertia(BOX_MASS, vInertia);
	tPhysicsBodies[1].pMS = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 30, 0)));
	tPhysicsBodies[1].pBody = new btRigidBody(btRigidBody::btRigidBodyConstructionInfo(1, tPhysicsBodies[1].pMS, tShapes[SHAPE_CUBE_10].pShape, vInertia));
	tPhysicsBodies[1].pBody->setAngularVelocity(btVector3(0, 0, 0));
	tPhysicsBodies[1].pBody->setDamping(0, 0);
	tPhysicsBodies[1].pBody->setRestitution(0.5f);
	tPhysicsBodies[1].pBody->setFriction(0.25f);
	tPhysicsBodies[1].pBody->setDeactivationTime(10.0f);
	pWorld->addRigidBody(tPhysicsBodies[1].pBody);

The second cube is identical, uses the same shape but has it's own motion state + body of course. I'm stepping at 60 fps and rendering as follows:

Code: Select all

	btTransform transform;
	float fMat[16];
	pCam->UseView(false);
	
	glEnable(GL_DEPTH_TEST);

	tPhysicsBodies[1].pBody->getMotionState()->getWorldTransform(transform);
	transform.getOpenGLMatrix(fMat);

	GFX_DrawBox(Vec3(-100, -10, -100), Vec3(100, 0, 100));

	glPushMatrix();
	// glLoadMatrixf(fMat);
	glMultMatrixf(fMat);
	GFX_DrawBox(Vec3(- BOX_SIZE_BY_2, - BOX_SIZE_BY_2, - BOX_SIZE_BY_2), Vec3(BOX_SIZE_BY_2, BOX_SIZE_BY_2, BOX_SIZE_BY_2), false);
	glPopMatrix();


	tPhysicsBodies[2].pBody->getMotionState()->getWorldTransform(transform);
	transform.getOpenGLMatrix(fMat);

	glPushMatrix();
	glMultMatrixf(fMat);
	GFX_DrawBox(Vec3(- BOX_SIZE_BY_2, - BOX_SIZE_BY_2, - BOX_SIZE_BY_2), Vec3(BOX_SIZE_BY_2, BOX_SIZE_BY_2, BOX_SIZE_BY_2), false);
	glPopMatrix();
	
	glDisable(GL_DEPTH_TEST);
What's odd is it looks like they stop moving quite suddenly, despite me setting the deactivation time pretty high. They end up stacked like this:

Image

So I ask you, wtf am I doing wrong? :)
LaceySnr
Posts: 11
Joined: Fri Mar 20, 2009 11:11 am

Re: Cube droppin' weirdness

Post by LaceySnr »

I forgot to mention, occasionally the top cube will slide off to the right as you'd expect, but then it ends up angled and stops on an edge.... I really don't get this :) Am I grabbing the world transform wrong or something? It's such a simple setup that I get the feeling that I've overlooked something obvious...

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

Re: Cube droppin' weirdness

Post by Erwin Coumans »

What are the sizes of the boxes? It is best to keep units in meters, so box sizes around 1 meter or so.

Hope this helps,
Erwin
LaceySnr
Posts: 11
Joined: Fri Mar 20, 2009 11:11 am

Re: Cube droppin' weirdness

Post by LaceySnr »

They're #defined as below:

Code: Select all

#define BOX_SIZE_BY_2	(2.4)
#define BOX_MASS		(25000)

// this should be scaled by 100 but 10 seems more realistic to look at..
#define GRABBITY		(9.8 * 10)
Is there anything wrong with my rendering code? I don't think so because they sit next to each other properly etc. which would suggest the sizes + orientations are correct. It's just odd that top box bounces straight up when it lands as shown on the box below... That ain't right :)
LaceySnr
Posts: 11
Joined: Fri Mar 20, 2009 11:11 am

Re: Cube droppin' weirdness

Post by LaceySnr »

Ok, so it turns out dropping down the mass to 1 or 2 gives much better results. So gravity size and mass are all on completely the wrong scales and yet it looks nice :)