Page 1 of 1

Camera programming

Posted: Thu May 02, 2013 7:08 pm
by genzm
Hy, I want to program a third person camera. Apperently Irrlicht only has first person camera, so I was going to make it myself.
To get used to the methods and members of the classes in Irrlicht I thought I'd might first try out a first person camera.

So pure theoretically my code seems right, but the results are far from good. the camera flickers around, I'm getting very fast changes of view,...
I've been looking at it for quite some time now, but I can't figure it out. Anyone cares to give me a hint?

Code: Select all

void update(u32 delta) {
    // check input first
    check_keyboard_input(delta);
    check_mouse_input(delta);
 
    core::vector3df v = camera->getPosition();  v += dvector;
    camera->setPosition(v);
 
    v.X += sin(rotation->Y)<0 ? -abs(cos(rotation->Y)) : abs(cos(rotation->Y));
    v.Y += cos(rotation->X)<0 ? -abs(sin(rotation->X)) : abs(sin(rotation->X));
    v.Z += sin(rotation->X)<0 ? -abs(cos(rotation->X)) : abs(cos(rotation->X));
 
    camera->setTarget(v);
 
    dvector.X = 0; dvector.Y = 0; dvector.Z = 0;
}
 
void check_mouse_input(u32 dt) {
    core::vector2di dv = reg->getMouseState().get_position_rel();
    if (dv.X != 0) {
        rotation->X -= dv.X * s32(dt) * MOUSE_SENSITIVITY;
        if (rotation->X > core::PI/2)
            rotation->X = core::PI/2;
        if (rotation->X < -core::PI/2)
            rotation->X = -core::PI/2;
    }
    if (dv.Y != 0) {
        rotation->Y += dv.Y * s32(dt) * MOUSE_SENSITIVITY;
    }
 
    reg->getMouseState().reset_rel();
}
Note: I've cut out the only part which is relevant to this problem, so it's best for you to assume that all other code is written correctly has contains no bugs.

Thanks

Re: Camera programming

Posted: Fri May 03, 2013 1:21 am
by chronologicaldot
?
I can't really tell what's going on. Where is this update() called? When is it called? What's dvector? How is it's value set?
What's values are stored in your rotation vector?

The flickering may have something to do with the fact that you're snapping values everywhere and not accounting for all of the angles. (e.g. You account for angles less than 0 but not greater than 2*PI. What's your range of values?)

Sorry I can't offer more help, but you need to provide more info.

Re: Camera programming

Posted: Fri May 03, 2013 3:50 pm
by genzm
Update() is called every frame from the main loop of the game. dVector contains the position difference of the camera (compared with the previous frame). That's why I get the position of the camera and than add dvector. (to update the camera to the new position). How it's set is not really relevant here as my problem lies with mousemovement. (moving the camera with the cursors, and thus setting dvector works just fine).

In rotation I stored the rotation of the camera. For example rotation(0,core::PI/2,0) would mean that the camera has been rotated to the right, while rotation(core::PI/2,0,0) means the camera is looking upwards a bit.
I allready found a mistake in my reasoning, so I'll be rethinking the algoritme anyway.
Still I would appreciate any comments and help.

Re: Camera programming

Posted: Fri May 03, 2013 4:51 pm
by Abraxas)
Normally you would catch mouse movement in your event receiver.

Re: Camera programming

Posted: Fri May 03, 2013 5:46 pm
by hybrid
Actually, cameras should be implemented as scene node animators. As it's done with the default Irrlicht cams. The animators also receive the events just like an ordinary event receiver. But they are also properly connected to the nodes they animate and are automatically updated in the pre and post render calls like all other scene nodes. This gives a clean interface and a standard integration into Irrlicht's scene graph.

Re: Camera programming

Posted: Sat May 04, 2013 8:56 am
by genzm
MouseMovement is registered in a subclass I made from the event receiver.
And for the scene node animators. Could you give me some more explenation on that? Maybe a little example? Cause I don't really understand yet how that might work.

Re: Camera programming

Posted: Tue May 14, 2013 1:56 am
by lymantok
fyi in case you have not seen this in code snippets:

http://irrlicht.sourceforge.net/forum/v ... =9&t=27741