Skip to content
groverburger edited this page Dec 23, 2021 · 9 revisions

Camera

Last updated for g3d version 1.5.1

The camera is what is used to render your g3d Models, and is initialized automatically when you load g3d.
Unlike Model, camera cannot be instantiated. There can only ever be one camera.

Camera Properties

The camera is initialized with the following values:

g3d.camera.fov = math.pi/2
g3d.camera.nearClip = 0.01
g3d.camera.farClip = 1000
g3d.camera.aspectRatio = love.graphics.getWidth()/love.graphics.getHeight()
g3d.camera.position = {0,0,0}
g3d.camera.target = {0,0,0}
g3d.camera.up = {0,-1,0}

g3d.camera.fov

Stands for Field of View, and is an angle in radians specifying how "wide" your camera is.

g3d.camera.nearClip

A number denoting how far your Near Clipping Plane is from your camera's position.
Check out the Wikipedia article for more information.

g3d.camera.farClip

A number denoting how far your Far Clipping Plane is from your camera's position
Check out the Wikipedia article for more information.

g3d.camera.aspectRatio

A ratio (width/height) that adjusts the camera according to your game window so things aren't stretched weirdly when your game window isn't a perfect square.
Also see Everything is stretched when I change the window size for more information.

g3d.camera.position

The position of your camera in the world.
This is a 3D vector.

g3d.camera.target

The point where your camera is looking in the world.
This is a 3D vector.

g3d.camera.up

A 3D vector which represents which way up is.
This is used for matrix math calculations.

Camera Functions

g3d.camera.lookAt(x, y, z, xAt, yAt, zAt)

Sets the camera at a position, looking towards the target point.

Arguments:

  • x
    • number specifying the x position of the camera.
  • y
    • number specifying the y position of the camera.
  • z
    • number specifying the z position of the camera.
  • xAt
    • the x coordinate of the position where the camera look.
  • yAt
    • the x coordinate of the position where the camera look.
  • zAt
    • the x coordinate of the position where the camera look.

g3d.camera.lookInDirection(x, y, z, direction, pitch)

Sets the camera at a position with a specified direction to point in. This is also used internally in g3d.camera.firstPersonLook().

Arguments:

  • x
    • number specifying the x position of the camera.
  • y
    • number specifying the y position of the camera.
  • z
    • number specifying the z position of the camera.
  • direction optional, defaults to the camera's current direction
    • number specifying the side-to-side angle of the camera
  • pitch optional, defaults to the camera's current pitch
    • number specifying the up-down angle of the camera

g3d.camera.firstPersonLook(dx,dy)

Call this function in your love.mousemoved() function, as shown in the demo.

function love.mousemoved(x,y, dx,dy)
    g3d.camera.firstPersonLook(dx,dy)
end

This function automatically captures your mouse pointer, and uses g3d's built-in first person camera look functionality.
You can easily customize the sensitivity by multiplying or dividing dx and dy.

g3d.camera.firstPersonMovement(dt)

Call this function in your love.update() function to use g3d's built-in first person camera movement.

function love.update(dt)
    g3d.camera.firstPersonMovement(dt)
end

This uses standard WASD movement, with left shift to fly down and spacebar to fly up.
This does not implement collisions with any other models, that is something you must implement.

If you want collisions, take a look at the Collision page.

g3d.camera.updateViewMatrix()

Updates the camera's view matrix.
The view matrix houses data about where the camera is, and in what direction the camera is looking.
This function is also called by g3d.camera.lookAt and g3d.camera.lookInDirection.

g3d.camera.updateProjectionMatrix()

Updates the camera's perspective projection matrix.
The projection matrix houses data about the camera's fov and aspect ratio.
This function is called automatically when g3d is initialized to construct the default camera.

g3d.camera.updateOrthographicMatrix(size)

Updates the camera's orthographic projection matrix.
The orthographic projection matrix houses data about the camera's fov and aspect ratio, but projects the camera orthographically.

Arguments:

  • size optional, defaults to 5.
    • a number representing the size of the orthographic view.