I'm having some math problems here,
I created a 3rd person camera for my game and i want my
node to move with the WASD keys, however
it works if for example the camera is behind the character
but if i then rotate the camera to for example it's side, then W is still (move on Z axis) while it should change into X axis... i found a temporarily solution but it's kinda messed up,
here's what i have now:
Code: Select all
void Class::relative_camera(ISceneManager *smgr, IrrlichtDevice *device)
{
if(cameraActive == true)
{
float cursorpositionX = 0.5;
float cursorpositionY = 0.5;
if(keys.IsKeyDown(KEY_KEY_I)){cursorpositionY += 0.01;}
if(keys.IsKeyDown(KEY_KEY_J)){cursorpositionX -= 0.01;}
if(keys.IsKeyDown(KEY_KEY_K)){cursorpositionY -= 0.01;}
if(keys.IsKeyDown(KEY_KEY_L)){cursorpositionX += 0.01;}
core::vector3df cameraPos = camera->getAbsolutePosition();
float change_x = ( cursorpositionX - 0.5 ) * 256.0f;
float change_y = ( cursorpositionY - 0.5 ) * 256.0f;
direction += change_x;
zdirection -= change_y;
if( zdirection <- 90 )
zdirection = -90;
else
if( zdirection > -1 )
zdirection = -1;
core::vector3df nodeposition = node->getPosition();
float xf = nodeposition.X - cos( direction * PI / 180.0f ) * 64.0f;
float yf = nodeposition.Y - sin( zdirection * PI / 180.0f ) * 64.0f;
float zf = nodeposition.Z + sin( direction * PI / 180.0f ) * 64.0f;
camera->setPosition( core::vector3df( xf, yf, zf ) );
camera->setTarget( core::vector3df( nodeposition.X, nodeposition.Y+25.0f, nodeposition.Z ) );
if(direction == 0 || (direction <= -350 && direction >= -360) || (direction >= 350 && direction <= 360) ) direction = 0;
if(keys.IsKeyDown(KEY_KEY_Q)) std::cout << "Direction: " << direction << "\n" << "Zf: " << zf << "\nYf: " << yf << "Xf: " << xf << "\n";
//RIGHT NOW I HAVE THE FOLLOWING:
if((direction >= -15 && direction <= 50) || (direction <= 50 && direction >= -15)) {dir = 1;}//LOOKING FROM LEFT
if((direction >= 165 && direction <= 195) || (direction >= -205 && direction <= -175)) {dir = 2;}//LOOKING FROM RIGHT
if((direction >= 235 && direction <= 275) || (direction >= -90 && direction <= -60)) {dir = 3;}//LOOKING FROM BACK
if((direction >= 55 && direction <= 120) || (direction >= -260 && direction <= -240)) {dir = 4;}//LOOKING FROM FRONT
if((direction >= 290 && direction <= 325) || (direction >= -60 && direction <= -35)) {dir = 5;}//LOOKING FROM LEFT BACK
if((direction >= 220 && direction <= 255) || (direction >= -135 && direction <= -95)) {dir = 6;}//LOOKING FROM RIGHT BACK
if((direction >= 104 && direction <= 119) || (direction >= -320 && direction <= -295)){dir = 7;}//LOOKING FROM RIGHT FRONT
if((direction >= 120 && direction <= 160) || (direction >= -235 && direction <= -205)) {dir = 8;}//LOOKING FROM LEFT FRONT
/*
dir 1 = left
dir 2 = right
dir 3 = back
dir 4 = front
dir 5 = left-back
dir 6 = right-back
dir 7 = right-front
dir 8 = left-front
*/
}
Code: Select all
if(dir == 1) //LEFT
{
if(keys.IsKeyDown(KEY_KEY_W)){linkposition.X += 2.f;}
if(keys.IsKeyDown(KEY_KEY_A)){linkposition.Z += 2.f;}
if(keys.IsKeyDown(KEY_KEY_S)){linkposition.X -= 2.f;}
if(keys.IsKeyDown(KEY_KEY_D)){linkposition.Z -= 2.f;}
}
Thanks for helping!