Problem with Character movement controller

Veggieto

11-09-2012 00:23:09

Hello everyone, I'm using Ogre 1.74 with the ogreBullet libraries and I'm trying to implement a Character Controller.
Everything works more or less fine, with the exception that applying a force to the main character to make him move not only will actually make him move to the correct direction, but will also (and I really have no idea why atm) make him rotate a little as if the force wasn't actually applied on the center of mass.

I'll post the relevant part of the code here :

Inizialization of the main character :

Ogre::Vector3 size = Ogre::Vector3::ZERO;
Ogre::AxisAlignedBox boundingB = ent->getBoundingBox();
size = boundingB.getSize(); size /= 2.0f; // only the half needed
size *= 0.96f; // Bullet margin is a bit bigger so we need a smaller size
// (Bullet 2.76 Physics SDK Manual page 18)

// after that create the Bullet shape with the calculated size
sceneBoxShape = new OgreBulletCollisions::BoxCollisionShape(size);
// and the Bullet rigid body
defaultBody = new OgreBulletDynamics::RigidBody("MainCharaRigid", mWorld);
defaultBody->setShape( _charaNode,
sceneBoxShape,
0.6f, // dynamic body restitution
0.2f, // dynamic body friction
1.0f, // dynamic bodymass
_charaNode->getPosition(), // starting position of the box
Ogre::Quaternion(0,0,0,1));// orientation of the box

/** Disabling model rotation, having your character tumbling around is no good! **/
defaultBody->getBulletRigidBody()->setAngularFactor(btVector3(0,1,0));
defaultBody->disableDeactivation();


WASD Implementation :

switch(e.key)
{
case OIS::KC_W:
_direction += Ogre::Vector3(0,0,0.10);
break;

case OIS::KC_A:
_direction += Ogre::Vector3(0.10,0,0);
break;

case OIS::KC_S:
_direction += Ogre::Vector3(0,0,-0.10);
break;

case OIS::KC_D:
_direction += Ogre::Vector3(-0.10,0,0);
break;


FrameStarted code function


...
defaultBody->applyForce(_direction, Ogre::Vector3(0,0,0));
mWorld->stepSimulation(evt.timeSinceLastFrame);


Am I doing something terribly wrong?
Thanks in advance for any help! :)

dermont

11-09-2012 11:22:07

Hello everyone, I'm using Ogre 1.74 with the ogreBullet libraries and I'm trying to implement a Character Controller.
Everything works more or less fine, with the exception that applying a force to the main character to make him move not only will actually make him move to the correct direction, but will also (and I really have no idea why atm) make him rotate a little as if the force wasn't actually applied on the center of mass.


I'm not sure what is the set-up of your character. Is the origin at (0,0,0) or if not have you tried a CompoundCollisionShape to supply an offset for your BoxShape? Maybe try a Kinematic Body:
viewtopic.php?f=12&t=14536

For your code does disabling the following code resolve the problem?

defaultBody->getBulletRigidBody()->setAngularFactor(btVector3(0,1,0));


otherwise restricting linear movement to X-Z plane:

case OIS::KC_W:
defaultBody->getBulletRigidBody()->setLinearFactor(btVector3(1,0,1));
_direction += Ogre::Vector3(0,0,0.10);
break;


or maybe supply non zero offset (world co-ords I think) to applyForce, you'll probably need to do a fair bit debugging to find if this is required/value needed.

defaultBody->applyForce(_direction, offset);


You could also check out btOgre:
http://www.ogre3d.org/forums/viewtopic.php?f=5&t=46856

There is also a bullet/ogre character tutorial here that may help.
http://www.ogre3d.org/forums/viewtopic.php?f=5&t=65482

Veggieto

12-09-2012 18:42:59

Changing the angular or the linear factor would solve the problem, but it would also stop me from implementing other behaviours such as a simple jump unfortunately..
It might be the third option, supplying a world-coord offset, but I have tried using the "applyCentralForce" function from bt and I get the same result.
Truth is I'm a little bit confused. :D
I will check the bullet tutorial you linked and see if I can get somewhere with it, thanks!
(if anyone knows a more direct solution though, I would appreciate it! :D)

UPDATE :

In the end, I just went using Bullet without any wrapper, thanks for the help and the tutorial link!