Skip to content
groverburger edited this page Jan 16, 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:

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},
    direction = 0,
    pitch = 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.

direction

An angle in radians which stores the side-to-side rotation of the Camera about the y-axis.
This is a number that is useful for first-person Camera controls, and is used internally in the FirstPersonCameraLook() function.

Note: this can also sometimes be called "yaw," a word that comes from airplane terminology.

pitch

An angle in radians which stores the up-down rotation of the Camera.
This is a number that is useful for first-person Camera controls, and is used internally in the FirstPersonCameraLook() function.

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

SetCamera(x, y, z, direction, pitch)

Sets the Camera at a position with a specified direction to point in.

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

SetCameraAndLookAt(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.

FirstPersonCameraMovement(dt)

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

function love.update(dt)
    FirstPersonCameraMovement(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.

FirstPersonCameraLook(dx,dy)

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

function love.mousemoved(x,y, dx,dy)
    FirstPersonCameraLook(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.

Clone this wiki locally