Skip to content

Commit

Permalink
Made the frame rate independent lerp backwards compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
britzl committed Aug 11, 2018
1 parent 9dd07be commit 8fde37e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
3 changes: 1 addition & 2 deletions example/camera_controls.gui_script
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
12 changes: 4 additions & 8 deletions example/player.script
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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})
Expand Down
9 changes: 7 additions & 2 deletions orthographic/camera.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8fde37e

Please sign in to comment.