[FIXED] Objects do not rotate after collisions

Etek
Posts: 15
Joined: Mon Oct 04, 2010 5:55 pm

[FIXED] Objects do not rotate after collisions

Post by Etek »

Hello,

First of all, thank you very much for creating this physics SDK. :D

Secondly, I'm stuck and can't make out where and what I did wrong.
I have two objects, one box and one sphere, both start above the ground, the sphere above the box. The box is a bit rotated.
Both start falling, the box touching the ground first, bounces back and hits the ball which slides off to a side and also pushes the box a bit.
The problem is that the box never rests properly on the ground, but on one of it's corners. Also, the ball looks like it's sliding across the ground instead of rolling on it.

Here is the code responsible:

Initialization:

Code: Select all

    // Build the broadphase
    physicsBroadphase = new btDbvtBroadphase();
 
    // Set up the collision configuration and dispatcher
    physicsCollisionConfiguration = new btDefaultCollisionConfiguration();
    physicsDispatcher = new btCollisionDispatcher(physicsCollisionConfiguration);
 
    // The actual physics solver
    physicsSolver = new btSequentialImpulseConstraintSolver;
 
    // The world.
    physicsDynamicsWorld = new btDiscreteDynamicsWorld(physicsDispatcher,physicsBroadphase,physicsSolver,physicsCollisionConfiguration);
    physicsDynamicsWorld->setGravity(btVector3(0,-10,0)); // Y is the UP direction
Objects initialization:

Code: Select all

			groundShape			= new btStaticPlaneShape(btVector3(0, 1, 0), 1);
			groundMotionState	= new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, -1, 0)));
			groundRigidBody		= new btRigidBody(btRigidBody::btRigidBodyConstructionInfo(0, groundMotionState, groundShape, btVector3(0, 0, 0)));
			physicsDynamicsWorld->addRigidBody(groundRigidBody);

			//setup falling ball
			fallTransform.setIdentity();
			fallTransform.setOrigin(btVector3(0.0f, 60.0f, 0.0f));
			fallShape		= new btSphereShape(1);
			fallMotionState	= new btDefaultMotionState(fallTransform);
			fallShape->calculateLocalInertia(1, btVector3(0, 0, 0));
			fallRigidBody	= new btRigidBody(btRigidBody::btRigidBodyConstructionInfo(1, fallMotionState, fallShape, btVector3(0, 0, 0)));
			physicsDynamicsWorld->addRigidBody(fallRigidBody);

			//setup falling cube
			cubeTransform.setIdentity();
			cubeTransform.setOrigin(btVector3(0.0f, 50.0f, 0.7f));
			cubeTransform.setRotation(btQuaternion(0.0f, 3.0f, 1.0f, 1.0f));
			cubeShape		= new btBoxShape(btVector3(1, 1, 1));
			cubeMotionState = new btDefaultMotionState(cubeTransform);
			cubeShape->calculateLocalInertia(1, btVector3(0, 0, 0));
			cubeRigidBody	= new btRigidBody(btRigidBody::btRigidBodyConstructionInfo(1, cubeMotionState, cubeShape, btVector3(0, 0, 0)));
			physicsDynamicsWorld->addRigidBody(cubeRigidBody);
Update:

Code: Select all

physicsDynamicsWorld->stepSimulation(1/240.0f, 1);
I've put 240 to properly examine how things unfold

Draw:

Code: Select all

		gluLookAt(20.0f, 5.0f, 5.0f, 
			cubeRigidBody->getCenterOfMassPosition().getX(), 
			cubeRigidBody->getCenterOfMassPosition().getY(),
			cubeRigidBody->getCenterOfMassPosition().getZ(),
			0.0f, 1.0f, 0.0f);

		
		glPushMatrix();
		GLfloat mat[16];
		fallRigidBody->getCenterOfMassTransform().getOpenGLMatrix(mat);
		glMultMatrixf(mat);
		

		glEnableClientState(GL_VERTEX_ARRAY);
		glEnableClientState(GL_NORMAL_ARRAY);
		glEnableClientState(GL_TEXTURE_COORD_ARRAY);

		////glBindTexture(GL_TEXTURE_2D, ball
		glVertexPointer(3, GL_FLOAT, sizeof(TexturedVertexData3D), &ballModel->texturedVertexData3D[0].vertex);
		glNormalPointer(GL_FLOAT, sizeof(TexturedVertexData3D), &ballModel->texturedVertexData3D[0].normal);
		glTexCoordPointer(2, GL_FLOAT,  sizeof(TexturedVertexData3D), &ballModel->texturedVertexData3D[0].texCoord);
		glDrawArrays(GL_TRIANGLES, 0, ballModel->vertices);

		glPopMatrix();
		glPushMatrix();
		cubeRigidBody->getCenterOfMassTransform().getOpenGLMatrix(mat);
		glMultMatrixf(mat);

		////glBindTexture(GL_TEXTURE_2D, ball
		glVertexPointer(3, GL_FLOAT, sizeof(TexturedVertexData3D), &tankModel->texturedVertexData3D[0].vertex);
		glNormalPointer(GL_FLOAT, sizeof(TexturedVertexData3D), &tankModel->texturedVertexData3D[0].normal);
		glTexCoordPointer(2, GL_FLOAT,  sizeof(TexturedVertexData3D), &tankModel->texturedVertexData3D[0].texCoord);
		glDrawArrays(GL_TRIANGLES, 0, tankModel->vertices);

		glPopMatrix();
		glPushMatrix();
		groundRigidBody->getCenterOfMassTransform().getOpenGLMatrix(mat);
		glMultMatrixf(mat);

		glVertexPointer(3, GL_FLOAT, sizeof(TexturedVertexData3D), &basePlane->texturedVertexData3D[0].vertex);
		glNormalPointer(GL_FLOAT, sizeof(TexturedVertexData3D), &basePlane->texturedVertexData3D[0].normal);
		glTexCoordPointer(2, GL_FLOAT,  sizeof(TexturedVertexData3D), &basePlane->texturedVertexData3D[0].texCoord);
		glDrawArrays(GL_TRIANGLES, 0, basePlane->vertices);

		glPopMatrix();
Halp! :o)
Last edited by Etek on Sat Oct 09, 2010 5:10 pm, edited 1 time in total.
Etek
Posts: 15
Joined: Mon Oct 04, 2010 5:55 pm

Re: Objects do not rotate after collisions

Post by Etek »

Hmpf!!!

Fixed it

The problem is here

Code: Select all

         cubeShape->calculateLocalInertia(1, btVector3(0, 0, 0));
         cubeRigidBody   = new btRigidBody(btRigidBody::btRigidBodyConstructionInfo(1, cubeMotionState, cubeShape, btVector3(0, 0, 0)));
It should be

Code: Select all

         btVector3 localInertia(0, 0, 0);
cubeShape->calculateLocalInertia(1, localInertia);
         cubeRigidBody   = new btRigidBody(btRigidBody::btRigidBodyConstructionInfo(1, cubeMotionState, cubeShape, localInertia));
Because when inertia is calculated, it is passed back to localinertia and after that passed to the RigidBody constructor. Stupid me!!!