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
//CUSTOM FORCE CALLBACK
// KEY PRESSED
//KEY RELEASED
//MOVE CAMERA
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
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