Custom camera problem with moving forward [SOLVED]

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
attila3453
Posts: 4
Joined: Sun Jan 27, 2013 5:05 pm

Custom camera problem with moving forward [SOLVED]

Post by attila3453 »

I want to use a camera which works like the FPS camera included in the lib, but I don't want the mouse to be locked. To avoid this, I rotate the camera when I reach one of the borders. The rotation works alright, but I have problems when I move forward. Strangely, walking backwards works.

Code: Select all

 
        const int W = 800, H = 600;
        const float minwidth = .1, maxwidth = .9, minheight = .1, maxheight = .9;
 
 
        vector3df rot = vector3df(0, 0, 0), mov = vector3df(0, 0, 0);
        float rotspeed = 1, movspeed = .5;
 
        if(receiver.mx < W * minwidth) rot.Y -= rotspeed;
        if(receiver.mx > W * maxwidth) rot.Y += rotspeed;
        if(receiver.my < H * minheight) rot.X -= rotspeed;
        if(receiver.my > H * maxheight) rot.X += rotspeed;
 
        vector3df camdir = camera->getTarget() - camera->getPosition();
        camdir.normalize();
        vector3df camright = camdir.crossProduct(vector3df(0, 1, 0));
 
        if(receiver.IsKeyDown(KEY_KEY_W)) mov += camdir;
        if(receiver.IsKeyDown(KEY_KEY_A)) mov -= camright;
        if(receiver.IsKeyDown(KEY_KEY_S)) mov -= camdir;
        if(receiver.IsKeyDown(KEY_KEY_D)) mov += camright;
 
        camera->setRotation(camera->getRotation() + rot);
        camera->setPosition(camera->getPosition() + mov);
 
Last edited by attila3453 on Tue Jan 29, 2013 4:03 pm, edited 3 times in total.
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: Custom camera problem with moving forward

Post by Mel »

Do you update also the camera target? keep in mind that you are moving with regard to the camera direction, and your camera direction -NEEDS- the camera target, so, the target must be also updated. That is posible binding the camera target to the camera using this

http://irrlicht.sourceforge.net/docu/cl ... 5ac54e7205

This way, the target will be rotated accordingly, and your movement should be consistent then
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
attila3453
Posts: 4
Joined: Sun Jan 27, 2013 5:05 pm

Re: Custom camera problem with moving forward

Post by attila3453 »

Well, I call this before the main loop:

Code: Select all

camera->bindTargetAndRotation(true);
The problem is that, when moving forward, the model (I have a terrain model displayed) flickers in a green color and the movement is slower.

EDIT: Well, my mistake was that I updated the camera rotation vector after others, and the target wasn't updated. So the camera rotation has to be applied between the first and the second series of if statements, before getting the target.
So moving forward, backwards and rotating works.

Still, I have a problem with the right vector, because the camera rotates some 45 degrees.
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: Custom camera problem with moving forward

Post by Mel »

In fact, you don't need the right vector at all for the rotations, only for the translation, so you can delay the calculation of the right vector until you are done rotating, and you can calculate all the vectors later. Rotate, update the absolute position of the camera and translate then

And, as a personal note, i'd add a time dependant variable so your rotations and translations were frame-rate independent. the ITimer object gives you the time in miliseconds, so, you can call it, calculate the time from a frame to the next, divide this time by 1000 and multiply this value by the corresponding increments to get efectively a per second speed, not per frame. (:
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
attila3453
Posts: 4
Joined: Sun Jan 27, 2013 5:05 pm

Re: Custom camera problem with moving forward

Post by attila3453 »

It works at last! I checked the SDK source code. Thanks for making it open source! :D
Maybe the problem was with the target updating (binding doesn't need to be made true). And regarding the movement speed, I set vsync to be true at createDevice.

If anyone is interested in the updatecamera code, here:

Code: Select all

 
    vector3df pos = camera->getAbsolutePosition();
    vector3df target = camera->getTarget() - pos;
    vector3df rot = target.getHorizontalAngle();
 
    if(receiver.mx < W * minwidth) rot.Y -= rotspeed;
    if(receiver.mx > W * maxwidth) rot.Y += rotspeed;
    if(receiver.my < H * minheight) rot.X -= rotspeed;
    if(receiver.my > H * maxheight) rot.X += rotspeed;
 
    target.set(0, 0, core::max_(1.f, pos.getLength()));
    vector3df movedir;
 
    matrix4 mat;
    mat.setRotationDegrees(vector3df(rot.X, rot.Y, 0));
    mat.transformVect(target);
 
    movedir = target;
    movedir.normalize();
 
    if(receiver.IsKeyDown(KEY_KEY_W)) pos += movedir * movspeed;
    if(receiver.IsKeyDown(KEY_KEY_S)) pos -= movedir * movspeed;
 
    vector3df strafevect = target;
    strafevect = vector3df(-movedir.Z, 0, movedir.X);
 
    if(receiver.IsKeyDown(KEY_KEY_A)) pos += strafevect * movspeed;
    if(receiver.IsKeyDown(KEY_KEY_D)) pos -= strafevect * movspeed;
 
    camera->setPosition(pos);
    camera->updateAbsolutePosition();
    target += pos;
    camera->setTarget(target);
 
Post Reply