Skip to main content
GameDev.net gamedev.net
🔒 Locked

Moving a camera

Started by KaiserJohan Jan 7, 2013 at 5:40 PM 2 replies 1.6k views
Original Post
KaiserJohan
KaiserJohan

So far I've used a static camera which simply sits at Vec3(0.0f) and stares down negative Z axis. I've got geometry like cubes to play correctly when translating, scaling, rotating etc, but now I want to try get the camera move correctly.

I am using glm, and to get a view matrix I am using the glm::lookAt method with the Vec3 data provided from my camera struct


Mat4 CreateViewMatrix(const Camera& camera)
    {
        return glm::lookAt(camera.CameraPosition, camera.TargetVector, camera.UpVector);
    }

Here is my camera definition:


/* Camera definition */
    struct Camera
    {
        Vec3 CameraPosition;
        Vec3 TargetVector;
        Vec3 UpVector;

        Camera();
    };


    /* Camera inlines */
    inline Camera::Camera() : CameraPosition(0.0f), TargetVector(0.0f, 0.0f, -1.0f), UpVector(0.0f, 1.0f, 0.0f)
    {
    }

I basically try to move it like this between the rendering:


 // move camera
            if (evnt.KeySymbol == LEFT)
                activeScene->GetSceneCamera().CameraPosition -= Vec3(0.1f, 0.0f, 0.0f);

            if (evnt.KeySymbol == RIGHT)
                activeScene->GetSceneCamera().CameraPosition += Vec3(0.1f, 0.0f, 0.0f);

            if (evnt.KeySymbol == UP)
                activeScene->GetSceneCamera().CameraPosition -= Vec3(0.0f, 0.0f, 0.1f);

            if (evnt.KeySymbol == DOWN)
                activeScene->GetSceneCamera().CameraPosition += Vec3(0.0f, 0.0f, 0.1f);

but this creates some strange results. For example, instead of the 'strafing' effect I would expect when moving the camera on the x-axis, it is as if I am rotating it, standing still. And when I try to move it in Z-axis, I really cant quite explain how it looks like, kinda like 'zooming' down the Z axis or something..

So I guess it's not as easy as I think?

Zaoshi Kaba
Zaoshi Kaba
Sounds like you're doing FPS/3rd person camera, in this case you need to offset both camera.CameraPosition and camera.TargetVector by same amount.

P.S. if player is holding RIGHT and DOWN he'll move (0.1f, 0.0f, 0.1f) which is longer distance than just (0.1f, 0.0f, 0.0f).
KaiserJohan
KaiserJohan

Ohhh yes makes sense. Works like a charm, thanks.

Ravnock
Ravnock
Ohhh yes makes sense. Works like a charm, thanks.

One suggestion: It's usual when you move something in a game, always make the movement depending on deltaTime (time increment between frames)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.