diff --git a/example/camera_controls.gui_script b/example/camera_controls.gui_script index aa4c6c9..1993922 100644 --- a/example/camera_controls.gui_script +++ b/example/camera_controls.gui_script @@ -41,7 +41,7 @@ function on_input(self, action_id, action) return true elseif gui.pick_node(gui.get_node("follow_basic/button"), action.x, action.y) then self.follow = true - self.lerp = 0 + self.lerp = 1 return true elseif gui.pick_node(gui.get_node("follow_lerp/button"), action.x, action.y) then self.follow = true @@ -100,7 +100,6 @@ end function on_message(self, message_id, message, sender) if message_id == hash("camera_offset") then self.camera_offset = message.offset - print(self.camera_offset) end end diff --git a/example/player.script b/example/player.script index fe6e890..7aa767a 100644 --- a/example/player.script +++ b/example/player.script @@ -2,10 +2,6 @@ local camera = require "orthographic.camera" local SPEED = 400 -local function lerp(t, dt, from, to) - return vmath.lerp(1 - math.pow(t, dt), from, to) -end - function init(self) self.input = {} self.crosshair = vmath.vector3() @@ -21,17 +17,17 @@ function update(self, dt) local pos = go.get_position() if self.input[hash("up")] then pos.y = pos.y + SPEED * dt - self.camera_offset.y = lerp(0.25, dt, self.camera_offset.y, 300) + self.camera_offset.y = vmath.lerp(0.05, self.camera_offset.y, 300) elseif self.input[hash("down")] then pos.y = pos.y - SPEED * dt - self.camera_offset.y = lerp(0.25, dt, self.camera_offset.y, -300) + self.camera_offset.y = vmath.lerp(0.05, self.camera_offset.y, -300) end if self.input[hash("left")] then pos.x = pos.x - SPEED * dt - self.camera_offset.x = lerp(0.25, dt, self.camera_offset.x, -500) + self.camera_offset.x = vmath.lerp(0.05, self.camera_offset.x, -500) elseif self.input[hash("right")] then pos.x = pos.x + SPEED * dt - self.camera_offset.x = lerp(0.25, dt, self.camera_offset.x, 500) + self.camera_offset.x = vmath.lerp(0.05, self.camera_offset.x, 500) end msg.post("controls", "camera_offset", { offset = self.camera_offset}) diff --git a/orthographic/camera.lua b/orthographic/camera.lua index 6247422..5f9c08c 100644 --- a/orthographic/camera.lua +++ b/orthographic/camera.lua @@ -13,6 +13,7 @@ M.PROJECTOR.FIXED_ZOOM = hash("FIXED_ZOOM") local DISPLAY_WIDTH = tonumber(sys.get_config("display.width")) or 960 local DISPLAY_HEIGHT = tonumber(sys.get_config("display.height")) or 640 +local UPDATE_FREQUENCY = tonumber(sys.get_config("display.update_frequency")) or 60 local WINDOW_WIDTH = DISPLAY_WIDTH local WINDOW_HEIGHT = DISPLAY_HEIGHT @@ -62,12 +63,16 @@ projectors[M.PROJECTOR.FIXED_ZOOM] = function(camera_id, near_z, far_z, zoom) return vmath.matrix4_orthographic(xoffset, xoffset + projected_width, yoffset, yoffset + projected_height, near_z, far_z) end + -- http://www.rorydriscoll.com/2016/03/07/frame-rate-independent-damping-using-lerp/ +-- return vmath.lerp(1 - math.pow(t, dt), v1, v2) +-- https://www.gamasutra.com/blogs/ScottLembcke/20180404/316046/Improved_Lerp_Smoothing.php local function lerp_with_dt(t, dt, v1, v2) - return vmath.lerp(1 - math.pow(t, dt), v1, v2) + local rate = UPDATE_FREQUENCY * math.log10(1 - t) + return vmath.lerp(1 - math.pow(10, rate * dt), v1, v2) + --return vmath.lerp(t, v1, v2) end - --- Add a custom projector -- @param projector_id Unique id of the projector (hash) -- @param projector_fn The function to call when the projection matrix needs to be calculated