Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
Updated bezier path moving with zoom offsets.
Positions syncing with object instead attaching to the parent.
  • Loading branch information
astrochili committed Jun 21, 2022
1 parent 78236e8 commit 9272fb4
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 211 deletions.
16 changes: 2 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ How much time in seconds needed to change the current zoom to the desired zoom a

### collision_check

Checks a path from the camera to the operator anchor for the collisions and move it closer if necessary.
Checks a path from the camera to the operator anchor for the collisions and moves it closer if necessary.

### collision_distance

Expand Down Expand Up @@ -406,19 +406,7 @@ local easing_linear = function(x) return x end
operator.flight_look_easing = easing_linear
```

### operator.flight_zoom_easing

Easing function to update the camera zoom distance between motion points.

```lua
-- set a ready to use easing function
operator.flight_zoom_easing = operator.EASING_INOUT_QUAD
-- or use your own
local easing_linear = function(x) return x end
operator.flight_zoom_easing = easing_linear
```

### Included Easing Functions
Included easing functions:

- `operator.EASING_INOUT_SINE`
- `operator.EASING_INOUT_CUBIC`
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion game.project
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
include_dirs = operator

[project]
title = Defold Operator
title = Operator
version = 1.1
dependencies#0 = https://github.com/indiesoftby/defold-pointer-lock/archive/1b5e05b94491bb7e041052990122dd846e1605a9.zip

[bootstrap]
Expand Down
38 changes: 27 additions & 11 deletions operator/bezier.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,53 @@ local function int_lerp(t, a, b)
return a + (b - a) * t
end

local function linear_bezier(time, lerp, p1, p2)
local result = p1 == p2 and p1 or lerp(time, p1, p2)
return result
end

local function quad_bezier(time, lerp, p1, p2, p3)
local a = lerp(time, p1, p2)
local b = lerp(time, p2, p3)
local result = lerp(time, a, b)
local a = linear_bezier(time, lerp, p1, p2)
local b = linear_bezier(time, lerp, p2, p3)
local result = linear_bezier(time, lerp, a, b)
return result
end

local function cubic_bezier(time, lerp, p1, p2, p3, p4)
local a = quad_bezier(time, lerp, p1, p2, p3)
local b = quad_bezier(time, lerp, p2, p3, p4)
local result = lerp(time, a, b)
local result = linear_bezier(time, lerp, a, b)
return result
end

local bezier_funcs = {
[2] = linear_bezier,
[3] = quad_bezier,
[4] = cubic_bezier
}

-- Public

function bezier.new(points, samples_count, length_func, lerp_func)
assert(#points >= 3 and #points <= 4, 'Only 3 or 4 bezier points supported.')
assert(points[1] ~= points[#points], 'No distance between the start and end points.')
assert(#points >= 2 and #points <= 4, 'Only 2-4 bezier points are supported.')

local self = setmetatable({ }, { __index = bezier })
local is_integer = type(points[1]) == 'integer'

self.length_func = length_func or (is_integer and int_length or vmath.length)
self.lerp_func = lerp_func or (is_integer and int_lerp or vmath.lerp)
self.bezier_func = #points == 4 and cubic_bezier or quad_bezier

self.points = points
self.bezier_func = bezier_funcs[#points]
self.samples = { }

local samples_count = (samples_count and samples_count >= 1) and samples_count or 1
self.origin = points[1]
self.points = { }
for index, point in ipairs(points) do
self.points[index] = point - self.origin
end

local samples_count = samples_count or 1
samples_count = (#points > 2 and samples_count > 1) and samples_count or 1

local previous_position = points[1]
local passed_length = 0

Expand All @@ -66,7 +82,7 @@ function bezier.new(points, samples_count, length_func, lerp_func)
end

function bezier.position(self, time)
return self.bezier_func(time, self.lerp_func, unpack(self.points))
return self.origin + self.bezier_func(time, self.lerp_func, unpack(self.points))
end

function bezier.uniform_position(self, time)
Expand Down
1 change: 0 additions & 1 deletion operator/operator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ end
operator.camera_collisions_groups = { hash 'default' }
operator.flight_bezier_samples_count = 32
operator.flight_look_easing = operator.EASING_INOUT_QUAD
operator.flight_zoom_easing = operator.EASING_INOUT_QUAD

-- Private Properties

Expand Down
Loading

0 comments on commit 9272fb4

Please sign in to comment.