3dr person character controller

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
tuket
Posts: 11
Joined: Thu Jan 23, 2014 2:05 am

3dr person character controller

Post by tuket »

Hello, I have made a very simple character controller. The controlled character is seen from a camera that is placed behind it. Now it works but there is still something which I don't understand.
I use left and right arrows to rotate the character arround the Y axis. The up and down arrows are used to move the character forwards and backwards respectivelly.
The handling of left and right arrows is pretty simple:

Code: Select all

if(receiver->IsKeyDown(KEY_LEFT)){
        
        vector3df r = box->getRotation();
        
        float speed = 2;
        vector3df inc = vector3df(0,-1,0) * speed;
        
        r = r+inc;
        
        box->setRotation(r);
        
    }
In the case of up and down arrows the code is a little more complex and is were I have my doubts:

Code: Select all

if(receiver->IsKeyDown(KEY_UP)){
        
        vector3df v = box->getPosition();
        vector3df r = -box->getRotation();
        r *= DEGTORAD;
        
        
        quaternion q(r);
        
        float speed = 0.1;
        vector3df inc = (vector3df(0,0,1));
        quaternion resq(inc.X, inc.Y, inc.Z, 0);
        resq = q * resq;
        q.makeInverse();    // q * resq * q^(-1)
        resq = resq*q;
        box->setPosition( v + vector3df(resq.X, resq.Y, resq.Z)*speed );
        
    }
What I do is taking the forward unit vector and rotate it by the quaternion of the character. Then I increment the postion by the rotated vector. But look this part:

Code: Select all

vector3df r = -box->getRotation();
Notice that I negated the rotation. I did so because otherwise it does not work. If I do not place the minus there the box rotates correctly but when the character moves forwards or backwards it does not move as spected. For example: if I turn 10 degrees to the left then it moves forwards as if it was rotated 10 degrees to the right. I do not get it. In the beggining I thought it might be because the camera was looking the character from the front of it(I was using a box as a character) but I if you think about it it does not really matter.
I will attach the full code(still do not know how).
http://tuket.webcindario.com/follow.zip
Thanks in advance.
Post Reply