How to move the camera along its local axis?

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
Kahenraz
Posts: 2
Joined: Sat May 21, 2016 2:23 pm

How to move the camera along its local axis?

Post by Kahenraz »

How to move the camera along its local axis?

I want to move the camera 1 unit along its local x axis as if it were strafing along to the direction that it's looking.

If I add 1 to the X value of camera->setPosition() it instead moves along what I think is the global axis; it also doesn't account for rotation. How can I take a vector(1, 0, 0) and transform it in such a way that I can simply add it to the camera's position so that it moves along its local axis?

If I understand Irrlicht's camera methods, I think I also need to add this to the target so that it doesn't just strafe and then turn to look back at the target.

Information I'm finding on the internet talks about multiplying vectors with matrices but there doesn't seem to be an overload for multiplying vector3df and matrix4.
Foaly
Posts: 142
Joined: Tue Apr 15, 2014 8:45 am
Location: Germany

Re: How to move the camera along its local axis?

Post by Foaly »

You can get that vector by computing the cross product of the up and forward vector.
Vector3df side = up.crossProduct(forward).normalize();
You can get the up vector with camera->getUpVector() and the forward vector by subtracting the camera position from the target position.

No need for matrices here :)
Kahenraz
Posts: 2
Joined: Sat May 21, 2016 2:23 pm

Re: How to move the camera along its local axis?

Post by Kahenraz »

Glorious, thank you!

One thing I'm uncertain of is when I should call camera->updateAbsolutePosition(). Is it something I should do before calling getPosition() or after setPosition()? When should it be used?
Foaly
Posts: 142
Joined: Tue Apr 15, 2014 8:45 am
Location: Germany

Re: How to move the camera along its local axis?

Post by Foaly »

As long as it works I wouldn't think to hard about it.
It's also automatically called with drawAll.
You usually need it, when you call setPosition and then getAbsolutePosition, otherwise that will return the position from the previous frame.

You also don't need it if your node does not have a parent. (In that case getPosition() can be used instead of getAbsolutePosition())
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Re: How to move the camera along its local axis?

Post by Seven »

here are a few useful code snippets for what you are doing.

Code: Select all

 
    vector3df CSObject::getIn(ISceneNode* node)
    {
        if (node)
        {
            matrix4 mat = node->getRelativeTransformation();
            vector3df in(mat[8], mat[9], mat[10]);
            in.normalize();
            return in;
        }
        else return vector3df(0, 0, 0);
    }
 
    vector3df CSObject::getLeft(ISceneNode* node)
    {
        if (node)
        {
            matrix4 mat = node->getRelativeTransformation();
            vector3df left(mat[0], mat[1], mat[2]);
            left.normalize();
            return left;
        }
        else return vector3df(0, 0, 0);
    }
 
    vector3df CSObject::getUp(ISceneNode* node)
    {
        if (node)
        {
            matrix4 mat = node->getRelativeTransformation();
            vector3df up(mat[4], mat[5], mat[6]);
            up.normalize();
            return up;
        }
        else return vector3df(0, 0, 0);
    }
Post Reply