weird character movement

aida

09-08-2007 13:59:32

I've already sent a message about this issue, but it was in a old thread on another subject. So I think i'll be luckier if I create a new message.

Maybe someone has faced the same problem as mine. Here is it :

I move my character using the 4 keys UP, DOWN, RIGHT and LEFT. I've defined a simple customForceAndTorque Callback to control the character.
When I hit the RIGHT or LEFT key once, the character rotates well. when I hit another key afterwards, the character comes back to the position he was before the first key hit.
Here is the code :

//CHARACTER BODY

//Collision body for character
Vector3 size(1,1,1);
Real mass = 10;
Vector3 inertia = OgreNewt::MomentOfInertia::CalcEllipsoidSolid( mass, size ); // calculate the inertia ov the body
Vector3 pos = viewNode->getPosition();
Quaternion orient = Ogre::Quaternion::IDENTITY;

OgreNewt::Collision* col = new OgreNewt::CollisionPrimitives::Ellipsoid( pWorld, size );
characterCollisionBody = new OgreNewt::Body( pWorld, col );
delete col;

characterCollisionBody->attachToNode( viewNode );
characterCollisionBody->setMassMatrix( mass, inertia );

characterCollisionBody->setPositionOrientation( pos, orient );
characterCollisionBody->setAutoFreeze(0);
characterCollisionBody->setContinuousCollisionMode(1);


//CUSTOM FORCE CALLBACK

void MyFrameListener::character_force_callback(OgreNewt::Body* me){
Real mass;
Vector3 inertia;
Real gravity = -9.8;

me->getMassMatrix(mass, inertia);
Ogre::Vector3 force(0,gravity,0);
Ogre::Vector3 force2(0,gravity,0);

Quaternion q,orient;
Vector3 qq,s,direction,pos;
me->getPositionOrientation(pos, orient);


Ogre::Vector3 V0 = me->getVelocity();
Ogre::Vector3 V1 = orient * mDirection;

Ogre::Real deltatime = me->getWorld()->getTimeStep();
Vector3 acel ((V1 - V0) / deltatime);
force2 = (Vector3(acel.x,gravity,acel.z))*mass;

me->addForce( force2 );
}


// KEY PRESSED


bool MyFrameListener::keyPressed( const OIS::KeyEvent &arg )
{
if( mKeyboard->isKeyDown(OIS::KC_ESCAPE) || mKeyboard->isKeyDown(OIS::KC_Q) )
mWindow->destroy();


if(arg.key == OIS::KC_UP){
mTranslateVector.z = mMoveScale; // Move forward
mDirection = mCamera->getDerivedOrientation() * Vector3::NEGATIVE_UNIT_Z;
}
if(arg.key == OIS::KC_DOWN){
mTranslateVector.z = -mMoveScale; // Move backward
mDirection = mCamera->getDerivedOrientation() * Vector3::UNIT_Z;
}
if(arg.key == OIS::KC_RIGHT){
mRotX = -mRotScale;
mDirection = mCamera->getDerivedOrientation() * Vector3::UNIT_X;
}

if(arg.key == OIS::KC_LEFT){
mRotX = mRotScale;
mDirection = mCamera->getDerivedOrientation() * Vector3::NEGATIVE_UNIT_X;
}


mTimeUntilNextToggle = 1;
return true;
}


//KEY RELEASED

bool MyFrameListener::keyReleased( const OIS::KeyEvent &arg )
{

if (arg.key == OIS::KC_UP) {
mTranslateVector.z = 0;
}
if(arg.key == OIS::KC_DOWN)
mTranslateVector.z = 0;

if(arg.key == OIS::KC_RIGHT){
mRotX = 0;
mTranslateVector.x = 0;
}

if(arg.key == OIS::KC_LEFT){
mRotX = 0;
mTranslateVector.x =0;
}
mDirection = Vector3::ZERO;
return true;
}


//MOVE CAMERA

void MyFrameListener::moveCamera(){

mCamera->getParentSceneNode()->getParentSceneNode()->yaw(mRotX); mCamera->getParentSceneNode()->getParentSceneNode()->pitch(mRotY);
//mCamera->getParentSceneNode()->getParentSceneNode()->translate(-mTranslateVector, Ogre::Node::TS_LOCAL);

if (mRotX !=(Radian)0 || mRotY!=(Radian)0) mDirection = mCamera->getDerivedDirection()*mDirection;
}


Does someone know this 'phenomenon'? Maybe it has to do with velocity, but I'm not sure. I don't set velocity anywhere, should I?

Please help

aida

10-08-2007 18:15:22

Actually to rotate the character I apply a force in the x Direction * CameraDirection. The character rotates indeed. But, when I apply afterwards another force to move the character, his position is reset!
Pleaase any idea is welcome!!!!!!

RedSkull

14-08-2007 10:48:52

Hi aida, in the following post there is a example wich can help you about moving camera (by the code above I suppose you want implement a FPS camera).

http://www.ogre3d.org/phpBB2addons/viewtopic.php?t=2110&postdays=0&postorder=asc&start=0

aida

14-08-2007 16:52:53

Thank you for your reply

I've already tried this solution, and many others in the forum, it's just that I couldn't get a nice movement. The character starts jumping, bouncing, or non stop rotating.
So I've decided to simply set Omega to rotate and when key is released, set Omega to zero. Not very proud of it, but the only way I found. Couldn't manage to use addTorque or addLocalForce properly. :(

RedSkull

14-08-2007 22:07:39

Hi again!!!

Ok, reading another time your code I can give you two points to consider (I don't know if they'll can be able to resolve your problem, but, just in case...):

- I supposed that type of 'mCamera' is Ogre::Camera. Instead of use directly this variable you must use an attached SceneNode to her. Besides, this SceneNode must be attached to a Body, too.

- Try to reset 'mRotX', 'mRotY' and 'mDirection' variables at the end of 'frameStarted' method.

Godd luck.