movement and direction

Empaler

25-02-2010 14:20:35

Hi

I have a small application where i have a player and a map with physics enabled. What im trying to do is when in moving the player i can look where i want without change its moving direction. when i stop moving my movement direction updates from my looking direction.
My problem is that im not beeing able to stop my movement direction from updating.

// set player velocity from the user
if(mKeyboard->isKeyDown(OIS::KC_W))
{
forwardSpeed += 8.0*MovGain;
DesaUp = true;
}
else DesaUp = false;
if(mKeyboard->isKeyDown(OIS::KC_S))
{
forwardSpeed -= 5.0*MovGain;
DesaDown = true;
}
else DesaDown = false;
if(mKeyboard->isKeyDown(OIS::KC_A))
{
sideSpeed -= 5.0*MovGain;
DesaLeft = true;
}
else DesaLeft = false;
if(mKeyboard->isKeyDown(OIS::KC_D))
{
sideSpeed += 5.0*MovGain;
DesaRight = true;
}
else DesaRight = false;

if (Desacoplado == true)
{
if ((DesaUp == false)||(DesaDown == false)||(DesaLeft == false)||(DesaRight == false))
{

/* normal */
Real timestep = evt.timeSinceLastFrame;
Real rate = (60.0f * 3.1416f/ 180.0f) * timestep;
// calculate the camera Pitch Angle
int pitchDir = -mMouse->getMouseState().Y.rel/RotaGain;
cameraPitchAngle += pitchDir * rate;
if (cameraPitchAngle > 80.0f * 3.1416f/ 180.0f) cameraPitchAngle = 80.0f * 3.1416f/ 180.0f;
if (cameraPitchAngle < -80.0f * 3.1416f/ 180.0f) cameraPitchAngle = -80.0f * 3.1416f/ 180.0f;
Ogre::Quaternion pitch (Ogre::Radian(cameraPitchAngle), Ogre::Vector3 (1, 0, 0));

int yawDir = -mMouse->getMouseState().X.rel/RotaGain;
heading += Ogre::Radian(yawDir * rate);
mPlayer->setVelocity(forwardSpeed, sideSpeed, heading);

Ogre::Vector3 posit;
Ogre::Quaternion orient;
OgreNewt::Body* playerBody = mPlayer->getBody0();
playerBody->getVisualPositionOrientation (posit, orient);

orient = orient * pitch;
posit += orient * Ogre::Vector3 (0, 0, m_extenalViewDist);

// move eye point to be about the players head
posit.y += mPlayer->getPlayerHeight() * 0.75f;

mCamera->setPosition(posit);
mCamera->setOrientation(orient);

if ((DesaUp == true)||(DesaDown == true)||(DesaLeft == true)||(DesaRight == true))
{
Real timestep = evt.timeSinceLastFrame;
Real rate = (60.0f * 3.1416f/ 180.0f) * timestep;
// calculate the camera Pitch Angle
int pitchDir = -mMouse->getMouseState().Y.rel/RotaGain;
cameraPitchAngle += pitchDir * rate;
if (cameraPitchAngle > 80.0f * 3.1416f/ 180.0f) cameraPitchAngle = 80.0f * 3.1416f/ 180.0f;
if (cameraPitchAngle < -80.0f * 3.1416f/ 180.0f) cameraPitchAngle = -80.0f * 3.1416f/ 180.0f;
Ogre::Quaternion pitch (Ogre::Radian(cameraPitchAngle), Ogre::Vector3 (1, 0, 0));
int yawDir = -mMouse->getMouseState().X.rel/RotaGain;
heading += Ogre::Radian(yawDir * rate);

/* set old direction DesaRad */
mPlayer->setVelocity(forwardSpeed, sideSpeed, DesaRad);

Ogre::Vector3 posit;
Ogre::Quaternion orient;
OgreNewt::Body* playerBody = mPlayer->getBody0();
playerBody->getVisualPositionOrientation (posit, orient);

orient = orient * pitch;
posit += orient * Ogre::Vector3 (0, 0, m_extenalViewDist);

// move eye point to be about the players head
posit.y += mPlayer->getPlayerHeight() * 0.75f;

/* camera normal */
mCamera->setPosition(posit);
mCamera->setOrientation(orient);
}
}

Real timestep = evt.timeSinceLastFrame;
Real rate = (60.0f * 3.1416f/ 180.0f) * timestep;

// calculate the camera Pitch Angle
int pitchDir = -mMouse->getMouseState().Y.rel/RotaGain;
cameraPitchAngle += pitchDir * rate;
if (cameraPitchAngle > 80.0f * 3.1416f/ 180.0f) cameraPitchAngle = 80.0f * 3.1416f/ 180.0f;
if (cameraPitchAngle < -80.0f * 3.1416f/ 180.0f) cameraPitchAngle = -80.0f * 3.1416f/ 180.0f;
Ogre::Quaternion pitch (Ogre::Radian(cameraPitchAngle), Ogre::Vector3 (1, 0, 0));
DesaQuat = pitch;

int yawDir = -mMouse->getMouseState().X.rel/RotaGain;
heading += Ogre::Radian(yawDir * rate);
DesaRad = heading;
mPlayer->setVelocity(forwardSpeed, sideSpeed, heading);

Ogre::Vector3 posit;
Ogre::Quaternion orient;
OgreNewt::Body* playerBody = mPlayer->getBody0();
playerBody->getVisualPositionOrientation (posit, orient);

orient = orient * pitch;
posit += orient * Ogre::Vector3 (0, 0, m_extenalViewDist);
DesaQuat = orient;
// move eye point to be about the players head
posit.y += mPlayer->getPlayerHeight() * 0.75f;

mCamera->setPosition(posit);
mCamera->setOrientation(orient);


mPlayer->setVelocity(forwardSpeed, sideSpeed, DesaRad); set the orientation and destiny to the player. Im saving DesaRad from when the keys where not pressed.
Maybe im misunderstanding smt?

Thanks

tod

26-02-2010 07:56:48

int yawDir = -mMouse->getMouseState().X.rel/RotaGain;
heading += Ogre::Radian(yawDir * rate);
DesaRad = heading;
mPlayer->setVelocity(forwardSpeed, sideSpeed, heading);


It seems to me that you use the mouse to orient your player, I'm not really sure how are you trying to set the direction of movement and how to set the camera direction.

Empaler

26-02-2010 11:02:54

yes but when im pressing the keys WSAD
mPlayer->setVelocity(forwardSpeed, sideSpeed, DesaRad);
wich DesaRad (orientation) was stored when i wasnt pressing any key..
when im pressin a key i want the camera to move and movement direction to stay the same as b4

Empaler

26-02-2010 12:01:10

Now it works but not 100% but its smt

if(mKeyboard->isKeyDown(OIS::KC_W)||mKeyboard->isKeyDown(OIS::KC_A)||mKeyboard->isKeyDown(OIS::KC_S)||mKeyboard->isKeyDown(OIS::KC_D))
{
Real timestep = evt.timeSinceLastFrame;
Real rate = (60.0f * 3.1416f/ 180.0f) * timestep;
// calculate the camera Pitch Angle
int pitchDir = -mMouse->getMouseState().Y.rel/RotaGain;
cameraPitchAngle += pitchDir * rate;
if (cameraPitchAngle > 80.0f * 3.1416f/ 180.0f) cameraPitchAngle = 80.0f * 3.1416f/ 180.0f;
if (cameraPitchAngle < -80.0f * 3.1416f/ 180.0f) cameraPitchAngle = -80.0f * 3.1416f/ 180.0f;
Ogre::Quaternion pitch (Ogre::Radian(cameraPitchAngle), Ogre::Vector3 (1, 0, 0));
int yawDir = -mMouse->getMouseState().X.rel/RotaGain;
heading += Ogre::Radian(yawDir * rate);

DesaRadt = heading;
/* set old direction DesaRad */
mPlayer->setVelocity(forwardSpeed, sideSpeed, DesaRad);

Ogre::Vector3 posit;
Ogre::Quaternion orient;
OgreNewt::Body* playerBody = mPlayer->getBody0();
playerBody->getVisualPositionOrientation (posit, orient);

if (des == true)
{
orient = DesaQuat;
des=false;
}

orient = orient * pitch;
posit += orient * Ogre::Vector3 (0, 0, m_extenalViewDist);

// move eye point to be about the players head
posit.y += mPlayer->getPlayerHeight() * 0.75f;

/* camera normal */
mCamera->setPosition(posit);
//mCamera->setOrientation(orient);
mCamera->yaw(Degree(yawDir));
mCamera->pitch(Degree(pitchDir));
}

Empaler

26-02-2010 21:03:54

Problem now is that the player when releasing WSAD points at the movement direction instead of the camera direction
if(mKeyboard->isKeyDown(OIS::KC_W)||mKeyboard->isKeyDown(OIS::KC_A)||mKeyboard->isKeyDown(OIS::KC_S)||mKeyboard->isKeyDown(OIS::KC_D))
{
Real timestep = evt.timeSinceLastFrame;
Real rate = (60.0f * 3.1416f/ 180.0f) * timestep;
// calculate the camera Pitch Angle
pitchDir = -mMouse->getMouseState().Y.rel/RotaGain;
cameraPitchAngle += pitchDir * rate;
if (cameraPitchAngle > 80.0f * 3.1416f/ 180.0f) cameraPitchAngle = 80.0f * 3.1416f/ 180.0f;
if (cameraPitchAngle < -80.0f * 3.1416f/ 180.0f) cameraPitchAngle = -80.0f * 3.1416f/ 180.0f;
Ogre::Quaternion pitch (Ogre::Radian(cameraPitchAngle), Ogre::Vector3 (1, 0, 0));
yawDir = -mMouse->getMouseState().X.rel/RotaGain;
heading += Ogre::Radian(yawDir * rate);

DesaRadt = heading;
/* set old direction DesaRad */
mPlayer->setVelocity(forwardSpeed, sideSpeed, DesaRad);

//Ogre::Vector3 posit;
//Ogre::Quaternion orient;
OgreNewt::Body* playerBody = mPlayer->getBody0();
playerBody->getVisualPositionOrientation (posit, orient);

if (des == true)
{
orient = DesaQuat;
des=false;
}

orient = orient * pitch;
posit += orient * Ogre::Vector3 (0, 0, m_extenalViewDist);

// move eye point to be about the players head
posit.y += mPlayer->getPlayerHeight() * 0.75f;

/* camera normal */
mCamera->setPosition(posit);
//mCamera->setOrientation(orient);
mCamera->yaw(Degree(yawDir));
mCamera->pitch(Degree(pitchDir));
}
//if ((DesaUp == true)||(DesaDown == true)||(DesaLeft == true)||(DesaRight == true))
else
{
//heading = DesaRadt;
/* normal */
Real timestep = evt.timeSinceLastFrame;
Real rate = (60.0f * 3.1416f/ 180.0f) * timestep;
// calculate the camera Pitch Angle
pitchDir = -mMouse->getMouseState().Y.rel/RotaGain;
cameraPitchAngle += pitchDir * rate;
if (cameraPitchAngle > 80.0f * 3.1416f/ 180.0f) cameraPitchAngle = 80.0f * 3.1416f/ 180.0f;
if (cameraPitchAngle < -80.0f * 3.1416f/ 180.0f) cameraPitchAngle = -80.0f * 3.1416f/ 180.0f;
Ogre::Quaternion pitch (Ogre::Radian(cameraPitchAngle), Ogre::Vector3 (1, 0, 0));

yawDir = -mMouse->getMouseState().X.rel/RotaGain;

heading += Ogre::Radian(yawDir * rate);

DesaRad = heading;
mPlayer->setVelocity(forwardSpeed, sideSpeed, heading);

//Ogre::Vector3 posit;
//Ogre::Quaternion orientt;
OgreNewt::Body* playerBody = mPlayer->getBody0();
playerBody->getVisualPositionOrientation (posit, orient);

orient = orient * pitch;
posit += orient * Ogre::Vector3 (0, 0, m_extenalViewDist);

DesaQuat = orient;
des = true;

// move eye point to be about the players head
posit.y += mPlayer->getPlayerHeight() * 0.75f;

mCamera->setPosition(posit);
mCamera->setOrientation(orient);
}


shouldnt it by this code update direction correctly from mouse direction ?