Skip to content
groverburger edited this page Jan 20, 2021 · 9 revisions

Camera

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 as follows:

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

fov

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

nearClip

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

farClip

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

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.

position

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

target

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

down

A 3D vector which represents which way down is.
This is used for matrix math calculations.
Most of the time, cameras in 3D graphics use an up vector instead, but because of how negative y is up in Love, a down vector is more convenient.




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 Detection page.

Clone this wiki locally