Movement on X,Y,Z axis based on camera [HELP!!]

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
DoritoX
Posts: 16
Joined: Fri Mar 11, 2011 2:16 pm
Location: 144

Movement on X,Y,Z axis based on camera [HELP!!]

Post by DoritoX »

Hi there,
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
     */

    }
then i have 8 different control ways set up like this:

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;}
    }
i'm really stuck and i can't find anything on the internet, there must be a better solution!

Thanks for helping!
ChaiRuiPeng
Posts: 363
Joined: Thu Dec 16, 2010 8:50 pm
Location: Somewhere in the clouds.. drinking pink lemonade and sunshine..

Post by ChaiRuiPeng »

well if your player is only moving along z and x and you have some different system that handles y, or climbing up slopes, then just transform the linear translation of your character by the cameras orienation, but set the resulting y to null, i think that should give you your desired results.
ent1ty wrote: success is a matter of concentration and desire
Butler Lampson wrote: all problems in Computer Science can be solved by another level of indirection
at a cost measure in computer resources ;)
DoritoX
Posts: 16
Joined: Fri Mar 11, 2011 2:16 pm
Location: 144

Post by DoritoX »

ufff how exactly would i do that? cause i also want the camera to be rotatable around the character
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Post by Radikalizm »

So, if I understand correctly the problem you're having right now is that the character is always moving in the direction of the target vector of the camera? (in other words, the direction the camera is facing)

If so, you should redesign your system so it's not your camera which is moving, but the character, eg. by creating a custom scene node for you character, including a vector which determines the direction the character is facing
Then you can set your camera as a child of this character so the camera position will stay relative to the character position, this would mean that if your character would move the camera would move along with it
When you've done this your camera only has to deal with the rotation around your character, since your movement will always be correct because it's determined by your character scene node

I hope this is what you meant, if not just disregard this post :D
DoritoX
Posts: 16
Joined: Fri Mar 11, 2011 2:16 pm
Location: 144

Post by DoritoX »

err that was not exactly what i meant,

it's like this, the camera is behind my character, then if i press W (up) my character moves up, then if i press A (left) my character moves left etc.
but if i rotate my camera around my character for let's say....180 degrees (so it faces the front of my character)
Then i want my character to move backwards with W and right with A

like....the controls must change depending on where the camera is positioned
ChaiRuiPeng
Posts: 363
Joined: Thu Dec 16, 2010 8:50 pm
Location: Somewhere in the clouds.. drinking pink lemonade and sunshine..

Post by ChaiRuiPeng »

then just do what i told you.

you can access the cameras transform with ISceneNode->getAbsoluteTransform()

simply set up a local vector

for key W it would be (0.0,0.0,1.0) or whatever. once you transform it by the absolute transform it will move relative to the cameras axis
ent1ty wrote: success is a matter of concentration and desire
Butler Lampson wrote: all problems in Computer Science can be solved by another level of indirection
at a cost measure in computer resources ;)
DoritoX
Posts: 16
Joined: Fri Mar 11, 2011 2:16 pm
Location: 144

Post by DoritoX »

but if i'd do that, then i wouldnt be able to rotate my cam around the player right? then my player would move together with the camera
ChaiRuiPeng
Posts: 363
Joined: Thu Dec 16, 2010 8:50 pm
Location: Somewhere in the clouds.. drinking pink lemonade and sunshine..

Post by ChaiRuiPeng »

no it that wont happen. that will only happen if you use the irrlicht ISceneNode parenting scheme. that is not what i am talking about.

i am talking about transforming a vector the the cameras local axis, coord system.
ent1ty wrote: success is a matter of concentration and desire
Butler Lampson wrote: all problems in Computer Science can be solved by another level of indirection
at a cost measure in computer resources ;)
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

DoritoX,
if you need to move your camera forward (independetly from where and how it is located in the scene), you need to do:
1) calculate normalized vew vector, something like "viewVector = (camera.Target - camera.Position).Normalize()";
2) move camera along that vector, something like "camera.Position += viewVector * N", where N is some unit value, depending on your camera speed and scene scale in general, try 1.0f for instance.
DoritoX
Posts: 16
Joined: Fri Mar 11, 2011 2:16 pm
Location: 144

Post by DoritoX »

Thanks for all the replies but nothing yet has really worked because well
my camera seems to be alright, it rotates around my character perfectly and it works well, it's just a problem in my character

It needs to move independently of the axis.

like if i press W when the camera is on the X axis, the character should move on the X axis, but if i press W when the camera is behind the character on the Z axis, my character should move up on the Z axis
Post Reply