rotating TPP box question

Siegfried
Posts: 5
Joined: Fri Apr 10, 2009 3:15 pm

rotating TPP box question

Post by Siegfried »

Hello everyone!
I have a problem - I am trying to do a very basic thing: something similar to TPP view, with BOX as a character. So basically I've created a box as rigid body, then I update it (rotate) and display the scene with camera set behind it's back (looking at it).
What would I expect is that I will always see the box in one view (ie. always see it's back side or two of it's corners) and the world would rotate around it. What I see is that everything rotates - including the box (which shouldnt) - it's still centered in the view, but slightly rotates as the world rotates (which is wrong).

The code is very simple -it's just some example code stitched together, but I spent two days trying to solve that mystery and I am going mental. I would be very happy if someone could help. thanks!

I create box rigid body like this:

Code: Select all

	btTransform startTransform;
	startTransform.setIdentity ();
	startTransform.setOrigin ( btVector3( 0.0, 3.0, 0.0 ) );
	fallShapeBox = new btBoxShape( btVector3( 3.0, 3.0, 3.0 ) );
	fallMotionStateBox = new btDefaultMotionState( startTransform );

	fallShapeBox->calculateLocalInertia( 1.0, fallInertia2 );
	btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCIBox( 1.0, fallMotionStateBox, fallShapeBox );
	fallRigidBodyBox = new btRigidBody( fallRigidBodyCIBox );

	fallRigidBodyBox->setSleepingThresholds ( 0.0, 0.0 );
	fallRigidBodyBox->setAngularFactor ( 0.0 );
	fallRigidBodyBox->setActivationState( DISABLE_DEACTIVATION );
	dynamicsWorld->addRigidBody( fallRigidBodyBox );
code for setting the camera is:

Code: Select all

	btTransform xform;
	xform = fallRigidBodyBox->getWorldTransform();

	btVector3 up = xform.getBasis()[1];
	btVector3 backward = xform.getBasis()[2];
	up.normalize ();
	backward.normalize ();
	m_cameraTargetPosition = xform.getOrigin();
	m_cameraPosition = m_cameraTargetPosition + up * ( 6.0 ) - backward * 24.0;

	btScalar	m[16];
	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity();

	gluLookAt( m_cameraPosition[0], m_cameraPosition[1], m_cameraPosition[2],
			   m_cameraTargetPosition[0], m_cameraTargetPosition[1], m_cameraTargetPosition[2],
			   m_cameraUp.getX(), m_cameraUp.getY(), m_cameraUp.getZ() );

code for displaying objects is:

Code: Select all

	mBulletSetCamera();
	const int	numObjects = dynamicsWorld->getNumCollisionObjects();
	btVector3 wireColor( 1, 0, 0 );
	for ( int i = 0; i < numObjects; i++ )
	{
		btCollisionObject*	colObj = dynamicsWorld->getCollisionObjectArray()[i];
		btRigidBody*		body = btRigidBody::upcast( colObj );

		if ( body && body->getMotionState() )
		{

			btDefaultMotionState* myMotionState = ( btDefaultMotionState* )body->getMotionState();
			myMotionState->m_graphicsWorldTrans.getOpenGLMatrix( m );
			rot = myMotionState->m_graphicsWorldTrans.getBasis();
		}
		else
		{

			colObj->getWorldTransform().getOpenGLMatrix( m );
			rot = colObj->getWorldTransform().getBasis();
		}

		btVector3 wireColor( 1.f, 0.0f, 0.5f ); //wants deactivation

		if ( i&1 ) wireColor = btVector3( 0.f, 1.0f, 1.f );

		btVector3 aabbMin, aabbMax;
		dynamicsWorld->getBroadphase()->getBroadphaseAabb( aabbMin, aabbMax );

		aabbMin -= btVector3( 1e30, 1e30, 1e30 );
		aabbMax += btVector3( 1e30, 1e30, 1e30 );

		glPushMatrix();
		glMultMatrixf( m );

		switch ( colObj->getCollisionShape()->getShapeType() )
		{
			case BOX_SHAPE_PROXYTYPE:
			{

				const btBoxShape* boxShape = static_cast<const btBoxShape*>( colObj->getCollisionShape() );
				btVector3 halfExtent = boxShape->getHalfExtentsWithMargin();
				glScaled( 2*halfExtent[0], 2*halfExtent[1], 2*halfExtent[2] );
				glutSolidCube( 1.0 );
				//useWireframeFallback = false;
				break;
			}

			case SPHERE_SHAPE_PROXYTYPE:
				//glMultMatrixf( m );
				const btSphereShape* sphereShape = static_cast<const btSphereShape*>( colObj->getCollisionShape() );
				float radius = sphereShape->getMargin();//radius doesn't include the margin, so draw with margin
				glutSolidSphere( radius, BALLQUALITY, BALLQUALITY );
				break;

			default:
				break;
		}

		glPopMatrix();
	}
and finally the code for "updating" the box (rotating it) is:

Code: Select all

	btScalar	m[16];

	if ( dir == ROTATER )
		m_angle += 3.1415f / 360;

	if ( dir == ROTATEL )
		m_angle -= 3.1415f / 360;

	btTransform xform;
	xform = fallRigidBodyBox->getWorldTransform();

	xform.setRotation ( btQuaternion ( btVector3( 0.0, 1.0, 0.0 ), m_angle ) );

	btVector3 linearVelocity = fallRigidBodyBox->getLinearVelocity();
	btScalar speed = fallRigidBodyBox->getLinearVelocity().length();

	btVector3 forwardDir = xform.getBasis()[2];
	forwardDir.normalize ();

	btVector3 walkDirection = btVector3( 0.0, 0.0, 0.0 );

	if ( dir == NORTH )
		walkDirection += forwardDir;

	if ( dir == SOUTH )
		walkDirection -= forwardDir;

	if ( ( dir == NORTH ) || ( dir == SOUTH ) )
		if ( speed < 15.0f )
		{
			linearVelocity *= btScalar( 2.2 );
			btVector3 velocity = linearVelocity + walkDirection * 3;
			fallRigidBodyBox->setLinearVelocity ( velocity );
		}
	fallRigidBodyBox->getMotionState()->setWorldTransform ( xform );
	fallRigidBodyBox->setCenterOfMassTransform ( xform );
	fallRigidBodyBox->activate( true );
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: rotating TPP box question

Post by Erwin Coumans »

What is TPP?

It seems a graphics issue, but this is just a physics forum.
Unless you post the entire project, I doubt anyone can digg into the code snippets.
Thanks,
Erwin
Siegfried
Posts: 5
Joined: Fri Apr 10, 2009 3:15 pm

Re: rotating TPP box question

Post by Siegfried »

Hello Erwin!
Thanks for answer.
the project is here: http://null-zero.com/downloads/opengl_bullet.rar (1.7mb, except static bullet lib & sources, but there is exec which you can try)
q,e - rotates the world
w - moves box "forward"

TPP stands for third person perspective ;)