Rotating camera

A camera orbits two models as a child of a pivot placed at the centre of the scene. Only the pivot rotates.


    
main.lua
local natiny = require("natiny")

if not natiny.backend.init(arg and arg[1] or natiny.backend.AUTO) then
    os.exit(1)
end

local window = natiny.window.create("Natiny - Rotating camera", 960, 640)
natiny.window.set_scale_mode(window, natiny.window.SCALE_MODE_STRETCH)

local BG = { 21 / 255, 30 / 255, 44 / 255 } -- #151E2C

local function load_mesh(path)
    local resource = natiny.resource.load(path)
    local mesh = natiny.mesh.load(resource)
    natiny.resource.destroy(resource)
    return mesh
end

-- Two models stand next to each other around the centre of the scene.
local platform_mesh = load_mesh("data/common/models/platform.glb")
local barrel_mesh = load_mesh("data/common/models/barrel.glb")
local box_mesh = load_mesh("data/common/models/box_01.glb")

local platform = natiny.model.create(platform_mesh)
local barrel = natiny.model.create(barrel_mesh)
local box = natiny.model.create(box_mesh)
natiny.entity.set_position(barrel, natiny.entity.SPACE_WORLD, -0.45, 0.0, 0.0)
natiny.entity.set_position(box, natiny.entity.SPACE_WORLD, 0.45, 0.25, 0.0)

-- The pivot is the centre of the orbit. The camera is its child, so rotating
-- only the pivot moves the camera around both models.
local pivot = natiny.pivot.create()
natiny.entity.set_position(pivot, natiny.entity.SPACE_WORLD, 0.0, 0.4, 0.0)

local camera = natiny.camera.create()
natiny.entity.set_parent(camera, pivot)
natiny.entity.set_position(camera, natiny.entity.SPACE_LOCAL, 0.0, 2.0, 3.0)
natiny.entity.look_at(camera, 0.0, 0.4, 0.0)
natiny.camera.set_fov(camera, 55.0)
natiny.camera.set_clip(camera, 0.1, 100.0)

local ORBIT_SPEED = 25.0 -- degrees per second

natiny.backend.loop(function(dt)
    natiny.entity.rotate(
        pivot,
        natiny.entity.SPACE_LOCAL,
        0.0,
        ORBIT_SPEED * dt,
        0.0
    )

    natiny.window.bind(window, function()
        natiny.render.clear(BG[1], BG[2], BG[3], 1.0)
        natiny.camera.bind(camera, function()
            natiny.render.tag("model")
        end)
    end)
end)

natiny.backend.shutdown()