Skip to content

Instantly share code, notes, and snippets.

@kilbith
Created Aug 30, 2022
Embed
What would you like to do?
Camera API: Client-Side Interpolation
local vec = vector.new
local vec_round = vector.round
local cam, player
local hud, init_hud, smiley
local old_rot
minetest.register_on_joinplayer(function(player)
player = player
init_hud = player:hud_add {
hud_elem_type = "text",
position = {x = 0.5, y = 0.5},
offset = {x = 0, y = -95},
alignment = {x = 0, y = 1},
size = {x = 3, y = 3},
number = 0xffffff,
text = "Jean-Patrick Guerrero presents...",
z_index = 0xDEAD,
style = 1,
}
local flags = player:hud_get_flags()
flags.crosshair = false
flags.hotbar = false
flags.healthbar = false
flags.wielditem = false
flags.breathbar = false
flags.minimap = true
player:hud_set_flags(flags)
end)
core.after(3, function()
cam = Camera()
cam:set_pos(vec(-68, 7, 1))
cam:set_target(vec(-25, 7, 1))
cam:set_speed{fov = 2, zoom = 10, move = 3, rotate = 5}
cam:set_zoom{min = 10, max = 1000}
player = core.get_player_by_name"singleplayer"
player:set_active_camera(cam:get_id())
hud = player:hud_add {
hud_elem_type = "text",
position = {x = 0.5, y = 0.5},
offset = {x = 0, y = 25},
alignment = {x = 0, y = 1},
size = {x = 4, y = 4},
number = 0xffff00,
text = "",
z_index = 0xDEAD,
style = 1,
}
player:hud_change(hud, "text", "The Camera API")
core.after(5, function()
player:hud_remove(init_hud)
player:hud_change(hud, "offset", {x = 0, y = 320})
player:hud_change(hud, "size", {x = 3, y = 3})
player:hud_change(hud, "text", "Translation interpolated")
cam:set_pos(vec(0, 25, 1), true)
-- cam:set_fov(100, true)
end)
end)
core.register_globalstep(function()
if not cam then return end
local cam_pos = vec_round(cam:get_pos())
-- print(dump(cam_pos))
if cam_pos == vec(0, 25, 1) then
player:hud_change(hud, "text", "Rotation interpolated")
cam:set_pos(vec(0, 25, 1))
cam:add_rotation360({x = 180, y = 0}, true)
core.after(4, function()
player:hud_change(hud, "text", "FOV interpolated (speed: " .. cam:get_speed().fov .. ")")
cam:set_fov(100, true)
end)
end
if cam:get_fov() == 100 then
cam:set_speed{fov = 5}
player:hud_change(hud, "text", "FOV interpolated (speed: " .. cam:get_speed().fov .. ")")
cam:set_fov(72, true)
core.after(2, function()
player:hud_change(hud, "text", "Translation interpolated (speed: " .. cam:get_speed().move .. ")")
cam:set_pos(vec(-68, 9, 1), true)
cam:set_target(vec(-68, 9, 1))
core.after(3, function()
cam:set_speed{move = 8}
player:hud_change(hud, "text", "Translation interpolated (speed: " .. cam:get_speed().move .. ")")
core.after(3, function()
cam:set_speed{move = 4}
player:hud_change(hud, "text", "Translation interpolated (speed: " .. cam:get_speed().move .. ")")
end)
end)
end)
end
if cam_pos == vec(-68, 9, 1) then
core.after(1, function()
cam:set_speed{move = 1}
player:hud_change(hud, "text", "Translation interpolated (speed: " .. cam:get_speed().move .. ")")
cam:set_pos(vec(-88, 8, 1), true)
cam:set_target(vec(-88, 8, 1))
end)
end
if cam_pos == vec(-88, 8, 1) then
core.after(1, function()
if not smiley then
smiley = player:hud_add {
hud_elem_type = "image",
position = {x = 0.8, y = 0.5},
scale = {x = 2, y = 2},
text = "smiley.png",
z_index = 0xDEAD,
}
end
core.after(2, function()
if smiley then
player:hud_remove(smiley)
end
if not old_rot then
old_rot = cam:get_rotation360()
cam:set_target(vec(-88, 8, 1))
cam:add_rotation360({x = 180, y = 0}, true)
end
core.after(3, function()
cam:set_pos(vec(-68, 10, 1), true)
cam:set_target(vec(-68, 10, 1))
end)
end)
end)
end
if cam_pos == vec(-68, 10, 1) then
player:hud_change(hud, "text", "ObjectRef Attachment")
local ppos = player:get_pos()
local cam_pos = vec(ppos)
cam_pos.x = cam_pos.x + 5
cam_pos.y = cam_pos.y + 5
cam_pos.z = cam_pos.z - 5
cam:set_pos(cam_pos)
local cam_target = vec(ppos)
cam_target.y = cam_target.y + 1.4
cam:set_target(cam_target)
cam:set_zoom{min = 10, max = 1000}
cam:set_controls{mouse = true}
player:attach_camera(cam:get_id())
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment