add phys.apply
This commit is contained in:
parent
fddb06ed8b
commit
9cdf032c39
4 changed files with 3880 additions and 0 deletions
|
|
@ -1,3 +1,5 @@
|
|||
local inspect = require('inspect')
|
||||
|
||||
local phys = {
|
||||
tau = 2 * math.pi
|
||||
}
|
||||
|
|
@ -10,9 +12,30 @@ end
|
|||
-- updates the actor's location by applying the passage of time
|
||||
-- TODO spin
|
||||
function phys.update(actor, dt)
|
||||
assert(actor.speed >= 0)
|
||||
local dv = actor.speed * dt
|
||||
actor.x = actor.x + math.cos(actor.angle) * dv
|
||||
actor.y = actor.y + math.sin(actor.angle) * dv
|
||||
end
|
||||
|
||||
function phys.rotate(actor, angle)
|
||||
actor.angle = (actor.angle + angle) % phys.tau
|
||||
end
|
||||
|
||||
function phys.apply(actor, force)
|
||||
assert(actor.speed >= 0)
|
||||
assert(force.speed >= 0)
|
||||
local ax = math.cos(actor.angle) * actor.speed
|
||||
local ay = math.sin(actor.angle) * actor.speed
|
||||
local fx = math.cos(force.angle) * force.speed
|
||||
local fy = math.sin(force.angle) * force.speed
|
||||
local x = ax + fx
|
||||
local y = ay + fy
|
||||
|
||||
local speed = math.sqrt(x ^ 2 + y ^ 2)
|
||||
local angle = math.atan(y, x) % phys.tau
|
||||
actor.speed = speed
|
||||
actor.angle = angle
|
||||
end
|
||||
|
||||
return phys
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue