[SOLVED] Problems in moving character with gravity

kintaro

16-03-2006 17:58:06

Hi, below there is a piece of code to move a character that has an box as primitive object phisics, and it has setStandardForceCallback().

mAnimationState->addTime( evt.timeSinceLastFrame );

Vector3 trans, strafe, vec;
Quaternion quat;

quat = msnEnt->getOrientation();

vec = Vector3(0.0,0.0,-0.5);
trans = quat * vec;

vec = Vector3(0.5,0.0,0.0);
strafe = quat * vec;

mInputDevice->capture();

msnCam->pitch( Degree(mInputDevice->getMouseRelativeY() * -0.5) );
msnCam->yaw( Degree(mInputDevice->getMouseRelativeX() * -0.5), SceneNode::TS_WORLD );

ent_body->unFreeze();

if (mInputDevice->isKeyDown(Ogre::KC_UP) || mInputDevice->isKeyDown(Ogre::KC_W))
{
ent_body->setVelocity( (trans * 10.0) );

// Set walking animation
if( mAnimationState->getAnimationName() == "Idle1" )
{
mAnimationState->setTimePosition( 0.0f );
mAnimationState->setEnabled( false );
}
mAnimationState = mEntity->getAnimationState( "Walk" );
mAnimationState->setLoop( true );
mAnimationState->setEnabled( true );
}

if (mInputDevice->isKeyDown(Ogre::KC_DOWN) || mInputDevice->isKeyDown(Ogre::KC_S))
{
ent_body->setVelocity( (trans * -1 * 10.0) );
}

if (mInputDevice->isKeyDown(Ogre::KC_LEFT) || mInputDevice->isKeyDown(Ogre::KC_A))
{
ent_body->setVelocity( (strafe * -1 * 10.0) );
}

if (mInputDevice->isKeyDown(Ogre::KC_RIGHT) || mInputDevice->isKeyDown(Ogre::KC_D))
{
ent_body->setVelocity( (strafe * 10.0) );
}

if (mInputDevice->isKeyDown(Ogre::KC_PGDOWN))
{
Ogre::Vector3 dir(0,-1,0);
ent_body->setVelocity( (dir * 10.0) );
}

if (mInputDevice->isKeyDown(Ogre::KC_PGUP))
{
Ogre::Vector3 dir(0,1,0);
ent_body->setVelocity( (dir * 10.0) );
}


When the character is not moving, the gravity act over it normally, but when I moving it in any direction, the gravity does not act, till I stop moving the character.

How can fix it?

Note: Till now only foward moving has walking animation, I decided to put animation in other directions after fix the gravity problem.

Thanks for help

walaber

16-03-2006 19:05:50

that's because you are setting the velocity manually. try this:


Ogre::Vector3 cur_vel = ent_body->getVelocity();

// in your movement code
Ogre::vector3 vel = trans * -1 * 10.0;
vel.y = cur_vel.y;

ent_body->setVelocity( vel );


when you call setVelocity() with a Y component of 0.0, you are basically cancelling out the effects of gravity. the code above keeps the Y velocity (gravity), while using your desired velocity for movement.

kintaro

16-03-2006 19:29:08

thanks very much wlaber, it worked perfectly.