Camera programming

Post your questions, suggestions and experiences regarding game design, integration of external libraries here. For irrEdit, irrXML and irrKlang, see the
ambiera forums
Post Reply
genzm
Posts: 6
Joined: Sun Mar 24, 2013 4:42 pm

Camera programming

Post 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
chronologicaldot
Competition winner
Posts: 688
Joined: Mon Sep 10, 2012 8:51 am

Re: Camera programming

Post 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.
genzm
Posts: 6
Joined: Sun Mar 24, 2013 4:42 pm

Re: Camera programming

Post 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.
Abraxas)
Posts: 227
Joined: Sun Oct 18, 2009 7:24 am

Re: Camera programming

Post by Abraxas) »

Normally you would catch mouse movement in your event receiver.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Camera programming

Post 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.
genzm
Posts: 6
Joined: Sun Mar 24, 2013 4:42 pm

Re: Camera programming

Post 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.
lymantok
Posts: 67
Joined: Mon Dec 31, 2007 6:13 am

Re: Camera programming

Post by lymantok »

fyi in case you have not seen this in code snippets:

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