Setting rotation of a static rigid body

Buck1000
Posts: 5
Joined: Sun Dec 27, 2009 8:13 pm

Setting rotation of a static rigid body

Post by Buck1000 »

I'm trying to get a rigid body to rotate around the Y access by a set amount every frame. What I've achieved instead, is a rigid body that spins insanely. I've been messing around with the code for over an hour now, and thought I'd ask you guys for help.

Heres the code -

Code: Select all

			btVector3 EulerRotation;
			QuaternionToEuler(TurretPhysics->getOrientation(), EulerRotation);
			vector3df Rotation = vector3df(EulerRotation[0], EulerRotation[1], EulerRotation[2]);
			float rotValue=2;

			Rotation = vector3df(0,Rotation.Y+rotValue,0);
			
			btTransform transform;
			transform.setIdentity();
			transform.setOrigin(TurretPhysics->getWorldTransform().getOrigin());

			btQuaternion quat;
			quat.setEuler(Rotation.Y,Rotation.X,Rotation.Z);

			transform.setRotation(quat);

			TurretPhysics->setWorldTransform(transform);
And QuaternionToEuler() -

Code: Select all

void QuaternionToEuler(const btQuaternion &TQuat, btVector3 &TEuler)
{
	btScalar W = TQuat.getW();
	btScalar X = TQuat.getX();
	btScalar Y = TQuat.getY();
	btScalar Z = TQuat.getZ();
	float WSquared = W * W;
	float XSquared = X * X;
	float YSquared = Y * Y;
	float ZSquared = Z * Z;
	TEuler.setX(atan2f(2.0f * (Y * Z + X * W), -XSquared - YSquared + ZSquared + WSquared));
	TEuler.setY(asinf(-2.0f * (X * Z - Y * W)));
	TEuler.setZ(atan2f(2.0f * (X * Y + Z * W), XSquared - YSquared - ZSquared + WSquared));
	TEuler *= RADTODEG;
};
Whats supposed to happen, is the rigidbody (TurretPhysics) is supposed to rotate by 2 degrees every frame. The body is made from a btBvhTriangleMeshShape, so its static. When the simulation is run, the body spins wildly. I checked it's angle at runtime, and it jumps from somewhere around 1, to anywhere from 0-100, and sometimes goes negative. What am I doing wrong?
Mattg
Posts: 12
Joined: Thu Oct 22, 2009 12:50 am

Re: Setting rotation of a static rigid body

Post by Mattg »

btQuaternion::setEuler() expects radians as far as I know.

Also if your program run at a high framerate adding 2 degrees each time might still be very fast, I suggest you multiply with the deltatime to get the rotation to be frame rate independent.

Lastly is that a dynamic rigid body? It's not a good idea to set the position or orientation of a simulated body each frame since it override whatever state the physics calculated.