I have been messing around with this code trying to get it to work right, and it seems that when I rotate the player and move him, he always moves in the same direction. So it seems like the camera's target vector doesn't change. Unless I am doing something awfully wrong, it should work correctly ( however it's not).
Code: Select all
void Player::strafeLeft(){
cam->GetIrrCamNode()->updateAbsolutePosition();
core::vector3df tempVector = cam->GetIrrCamNode()->getAbsoluteTransformation().getRotationDegrees();
tempVector.rotateXZBy(-90 , core::vector3df(X,Y,Z));
tempVector = tempVector.normalize();
playerMesh->setPosition(core::vector3df(X + (tempVector.X * 2) , Y, Z + (tempVector.Z * 2)));
X += tempVector.X * 2;
Z += tempVector.Z * 2;
}
void Player::strafeRight(){
cam->GetIrrCamNode()->updateAbsolutePosition();
core::vector3df tempVector = cam->GetIrrCamNode()->getAbsoluteTransformation().getRotationDegrees();
tempVector.rotateXZBy(90 , core::vector3df(X,Y,Z));
tempVector = tempVector.normalize();
playerMesh->setPosition(core::vector3df(X + (tempVector.X * 2) , Y, Z + (tempVector.Z * 2)));
X += tempVector.X * 2;
Z += tempVector.Z * 2;
}
void Player::moveForward(){
cam->GetIrrCamNode()->updateAbsolutePosition();
core::vector3df tempVector = cam->GetIrrCamNode()->getAbsoluteTransformation().getRotationDegrees();
tempVector = tempVector.normalize();
playerMesh->setPosition(core::vector3df(X + (tempVector.X * 2) , Y, Z + (tempVector.Z * 2)));
X += tempVector.X / 10;
Z += tempVector.Z / 10;
}
void Player::moveBack(){
cam->GetIrrCamNode()->updateAbsolutePosition();
core::vector3df tempVector = cam->GetIrrCamNode()->getAbsoluteTransformation().getRotationDegrees();
tempVector = tempVector.normalize();
tempVector = tempVector.invert();
playerMesh->setPosition(core::vector3df(X + (tempVector.X * 2) , Y, Z + (tempVector.Z * 2)));
X += tempVector.X * 2;
Z += tempVector.Z * 2;
}
And here is the code that handles input
Code: Select all
if( receiver->IsKeyDown(KEY_KEY_W) )
currentPlayer.moveForward();
if( receiver->IsKeyDown(KEY_KEY_S) )
currentPlayer.moveBack();
if( receiver->IsKeyDown(KEY_KEY_A) )
currentPlayer.strafeLeft();
if( receiver->IsKeyDown(KEY_KEY_D) )
currentPlayer.strafeRight();
if ( receiver->IsKeyDown(KEY_LEFT) ){
currentPlayer.rotate(-1);
}
else if ( receiver->IsKeyDown(KEY_RIGHT) ){
currentPlayer.rotate(1);
}
else
myCam.RotateHorizontal(.0);
if ( receiver->IsKeyDown(KEY_UP) )
myCam.RotateVertical(-.1);
else if ( receiver->IsKeyDown(KEY_DOWN) )
myCam.RotateVertical(.1);
else
myCam.RotateVertical(.0);
if ( receiver->IsKeyDown(KEY_KEY_X) )
myCam.ZoomIn();
else if ( receiver->IsKeyDown(KEY_KEY_Z) )
myCam.ZoomOut();
myCam.Update();
Also, when moving forward the player will move not in the right direction, but will move until it gets to a certain spot then won't move any further. So I can this means that the camera vector never updates? By the way I ditched the original idea and just made it so that when you rotate the camera, it rotates the player as well. This post is trying to fix movement based on the cameras position vector.