Smooth Rotation of a Rigid Body

clamped
Posts: 1
Joined: Sat Apr 28, 2012 9:25 am

Smooth Rotation of a Rigid Body

Post by clamped »

I've been suffering from an issue regarding the implementation of orienting characters in a game I'm implementing using Ogre3D and Bullet physics.
What I have: A Direction Vector that the character is moving in, along with its current orientation.
What I need: To set the orientation of the character to face the way it is moving.

I have a snippet of code that sort of does what I want:

Code: Select all

btTransform src = body->getCenterOfMassTransform();
btVector3 up = BtOgre::Convert::toBullet(Ogre::Vector3::UNIT_X);
btVector3 normDirection = mDirection.normalized();
btScalar angle = acos(up.dot(normDirection));
btVector3 axis = up.cross(normDirection);
src.setRotation(btQuaternion(axis, angle));
body->setCenterOfMassTransform(src);
This snippet has a couple of problems however:

1) When changing direction, it tends to 'jitter' i.e. it rapidly faces one way, then the opposite for a second or so before correcting itself to the orientation it is supposed to be at.
2) Most times that the code is run I get an assertion error from Bullet's btQuaternion on

Code: Select all

assert(d != btScalar(0.0));
Can anyone help?

Thanks!
CookieMonster
Posts: 49
Joined: Sun Jan 29, 2012 10:01 pm

Re: Smooth Rotation of a Rigid Body

Post by CookieMonster »

If your character is a round shape then you only need to rotate the visual representation.
By running backwards and sidestepping, your character can change direction using a fast acceleration while the direction is slowly turning to the walking direction.
The head can turn to the walking direction faster than the body because it don't have so much weight.

This is what it may look like:
http://www.youtube.com/watch?v=evol5XTgBnM