• Advertisement
Sign in to follow this  

When to use lookAt function for a 3d game?

This topic is 2301 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey,

i am working on a 3d game/engine in webGL (fully custom i only use the glMatrix library for some basic functions).
The game i am making will be 3th person (atleast the main view when playing) but for cutscenes or intro's i will need other camera types like free, orbiting etc.

For now the game camera is 1st person or freecamera depending on what i working on for testing.
For the camera i now simply do 2 rotates to the matix and then translate it.
Then i loop my objects, particles etc.
code:
matrix = mat4.rotateX(matrix, -game.cam.pitch * pi180); //not altered yet
matrix = mat4.rotateY(matrix, -game.cam.yaw * pi180);
matrix = mat4.t(matrix, -game.cam.x,-game.cam.y,-game.cam.z);

The lookAt function looks also nice, just give it a position eye and up vector and its rotated and translated (played with it)

questions:
1: is it normal or better to control the camera with lookAt function or are mine 3 lines above also common used? (keeping in mind a more advanced camera system).

(for now in the 1st person camera when walking i just add the player.speed to the cur position, when turning i make a yawRate what adds up on the camera yaw and put that directly in the matrix rotations and translate works fine, but an 3th person orbiting camera with this method is more complicated, aldo did succeed some months ago but the code just looks alien and not nice)

demo from some months ago: http://www.flexxcode.nl/webgl/projects7/main.html (camera is free on x,z) works probably only in firefox webgl enabled

Share this post


Link to post
Share on other sites
Advertisement
"Better" is entirely subjective. If LookAt works, use it. If something else works, it's probably fine as well.

Share this post


Link to post
Share on other sites
LookAt is highly useful when applying a camera to the view matrix. I pretty much always use whatever lookat function is available; righ tnow im using XNA and that has a matrix creation method called... Matrix.LookAt. My cameras consist of a position, a direction, an up vector, and an FOV, so my code to set the view and projection matrices looks like this,


ViewMatrix.Current = Matrix.CreateLookAt(
list.Camera.Position,
list.Camera.Target,
list.Camera.Up
);

ProjectionMatrix.Current = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(list.Camera.FOV),this.GraphicsDevice.Viewport.AspectRatio, 0.2f, 65535f);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement