-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Nezuo/linear-goal
Linear goal
- Loading branch information
Showing
4 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
local Linear = {} | ||
Linear.__index = Linear | ||
|
||
function Linear.new(targetValue, options) | ||
assert(targetValue, "Missing argument #1: targetValue") | ||
|
||
options = options or {} | ||
|
||
return setmetatable({ | ||
_targetValue = targetValue, | ||
_targetVelocity = options.velocity or 1 | ||
}, Linear) | ||
end | ||
|
||
function Linear:step(state, dt) | ||
local position = state.value | ||
local velocity = self._targetVelocity -- Linear motion ignores the state's velocity | ||
local goal = self._targetValue | ||
|
||
local dPos = dt * velocity | ||
|
||
local complete = dPos >= math.abs(goal - position) | ||
position = position + dPos * (goal > position and 1 or -1) | ||
if complete then | ||
position = self._targetValue | ||
velocity = 0 | ||
end | ||
|
||
return { | ||
complete = complete, | ||
value = position, | ||
velocity = velocity | ||
} | ||
end | ||
|
||
return Linear |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
return function() | ||
local SingleMotor = require(script.Parent.SingleMotor) | ||
local Linear = require(script.Parent.Linear) | ||
|
||
describe("completed state", function() | ||
local motor = SingleMotor.new(0, false) | ||
|
||
local goal = Linear.new(1, { velocity = 1 }) | ||
motor:setGoal(goal) | ||
|
||
for _ = 1, 60 do | ||
motor:step(1/60) | ||
end | ||
|
||
it("should complete", function() | ||
expect(motor._state.complete).to.equal(true) | ||
end) | ||
|
||
it("should be exactly the goal value when completed", function() | ||
expect(motor._state.value).to.equal(1) | ||
end) | ||
end) | ||
|
||
describe("uncompleted state", function() | ||
local motor = SingleMotor.new(0, false) | ||
|
||
local goal = Linear.new(1, { velocity = 1 }) | ||
motor:setGoal(goal) | ||
|
||
for _ = 1, 59 do | ||
motor:step(1/60) | ||
end | ||
|
||
it("should be uncomplete", function() | ||
expect(motor._state.complete).to.equal(false) | ||
end) | ||
end) | ||
|
||
describe("negative velocity", function() | ||
local motor = SingleMotor.new(1, false) | ||
|
||
local goal = Linear.new(0, { velocity = 1 }) | ||
motor:setGoal(goal) | ||
|
||
for _ = 1, 60 do | ||
motor:step(1/60) | ||
end | ||
|
||
it("should complete", function() | ||
expect(motor._state.complete).to.equal(true) | ||
end) | ||
|
||
it("should be exactly the goal value when completed", function() | ||
expect(motor._state.value).to.equal(0) | ||
end) | ||
end) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
interface LinearState { | ||
complete: boolean | ||
position: number | ||
velocity: number | ||
} | ||
|
||
interface LinearOptions { | ||
velocity?: number | ||
} | ||
|
||
declare interface Linear { | ||
step(state: LinearState, deltaTime: number): LinearState | ||
} | ||
|
||
declare interface LinearConstructor { | ||
new(targetValue: number, options?: LinearOptions): Linear | ||
} | ||
|
||
declare const Linear: LinearConstructor | ||
export = Linear |