1

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:

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);

Where 'body' is the rigidbody I'm trying to orient.

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
assert(d != btScalar(0.0));

Can anyone help?

Thanks!

1
  • I think you can construct a btQuaternion from two vectors and it will give a quaternion that rotates from the first to the second. If you use the base orientation of your character and the direction you want, you should get the quaternion you need. Jitter probably happens because you have a rotation where the quaternion is not unique (like 180° vs 0°).
    – LiMuBei
    May 14, 2012 at 8:34

1 Answer 1

2

I think you shouldn't use functions like 'acos' for such things, as it may cause some inconsistencies in border-cases as the 180 vs 0 rotation mentioned above. You can also get high numerical error for such data.

The second thing is that - in general - you should avoid setting explicit position and rotation in physics engines, but rather apply forces and torques to make your body moving as you want. Your current approach may work perfectly now, but when you add another object and force you character to occupy the same space, your simulation will explode. And at this stage it's very hard to fix it, so it's better to do it right from start :) .

I know that finding correct force/torque can be tricky but it's the best way to make your simulation consistent.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.