Changing camera position also changes rotation

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
Eventide
Posts: 23
Joined: Fri Jul 19, 2013 4:46 am

Changing camera position also changes rotation

Post by Eventide »

Hi, i'm trying to create a 3rd person camera, but when i change the position the camera look like to rotate instead of moving, after some search in the forums i saw someone saying to create a variable with the old camera rotation, and set the rotation to the new rotation after the position is changed, now i can see the camera moving, but it changes the rotation everytime i move the character to a new direction.

I will show the pieces of my code related to it.


This is called by the IEventReceiver inherited class everytime in the main loop:

Code: Select all

 
            if(IsKeyDown(KEY_KEY_W))
            {
                vector3df oldrot = mCamera->getRotation();
                mPlayer->walk(1);
                mCamera->updateAbsolutePosition();
                mCamera->setRotation(oldrot);
            }
            else if(IsKeyDown(KEY_KEY_D))
            {
                vector3df oldrot = mCamera->getRotation();
                mPlayer->walk(2);
                mCamera->updateAbsolutePosition();
                mCamera->setRotation(oldrot);
            }
            else if(IsKeyDown(KEY_KEY_A))
            {
                vector3df oldrot = mCamera->getRotation();
                mPlayer->walk(3);
                mCamera->updateAbsolutePosition();
                mCamera->setRotation(oldrot);
            }
 
The camera constructor, the parent is the character, so it moves when the character moves, i already tried to move the camera manually and happens exactly the same thing(but i tryied it before search the forums and fixing the rotation, so, if you think this is wrong, tell me please):

Code: Select all

 
    mCamera = mSceneManager->addCameraSceneNode(mPlayer->getNode());
    mCamera->bindTargetAndRotation(true);
 
The walk function called by the IEventReceiver:

Code: Select all

 
void Creature::walk(int dir)
{
    vector3df oldpos = mNode->getPosition();
    if(dir == 1)
        mNode->setPosition(vector3df(oldpos.X, oldpos.Y, oldpos.Z + 1.0f));
    else if(dir == 2)
        mNode->setPosition(vector3df(oldpos.X + 1.0f, oldpos.Y, oldpos.Z));
    else if(dir == 3)
        mNode->setPosition(vector3df(oldpos.X - 1.0f, oldpos.Y, oldpos.Z));
}
 
Thanks in advance.
Eventide
Posts: 23
Joined: Fri Jul 19, 2013 4:46 am

Re: Changing camera position also changes rotation

Post by Eventide »

Changed the IEventReceiver part of the code, now the camera just change the rotation one time and don't change anymore, here is what i changed:

Code: Select all

 
            if(IsKeyDown(KEY_KEY_W))
            {
                mPlayer->walk(1);
                mCamera->setTarget(mPlayer->getNode()->getPosition());
            }
            else if(IsKeyDown(KEY_KEY_D))
            {
                mPlayer->walk(2);
                mCamera->setTarget(mPlayer->getNode()->getPosition());
            }
            else if(IsKeyDown(KEY_KEY_A))
            {
                mPlayer->walk(3);
                mCamera->setTarget(mPlayer->getNode()->getPosition());
            }
            else if(IsKeyDown(KEY_KEY_S))
            {
                mPlayer->walk(4);
                mCamera->setTarget(mPlayer->getNode()->getPosition());
            }
 
Before the movement:
Image

After the movement:
Image

Also, the camera was already targeting the player before that.

edit: no, it wasn't, the if condition before the targeting was wrong, now it's working fine, im sorry for bothering you with this thread. :oops:
Post Reply