lovexp/asteroids/phys.lua

37 lines
1 KiB
Lua
Raw Permalink Normal View History

2025-11-16 22:51:51 -05:00
local inspect = require('inspect')
2025-11-16 12:06:35 -05:00
local phys = {
tau = 2 * math.pi
}
-- returns true if the two actors intersect
function phys.intersects(actor1, actor2)
return (actor1.x - actor2.x) ^ 2 + (actor1.y - actor2.y) ^ 2 <= (actor1.radius + actor2.radius) ^ 2
end
-- updates the actor's location by applying the passage of time
function phys.update(actor, dt)
2025-11-16 22:51:51 -05:00
assert(actor.speed >= 0)
2025-11-16 12:06:35 -05:00
local dv = actor.speed * dt
actor.x = actor.x + math.cos(actor.angle) * dv
actor.y = actor.y + math.sin(actor.angle) * dv
2025-11-17 23:15:21 -05:00
local dr = actor.spin * dt
actor.facing = (actor.facing + dr) % phys.tau
2025-11-16 12:06:35 -05:00
end
2025-11-16 22:51:51 -05:00
function phys.apply(actor, force)
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)
2025-11-17 20:31:14 -05:00
local angle = math.atan2(y, x)
2025-11-16 22:51:51 -05:00
actor.speed = speed
actor.angle = angle
end
2025-11-16 12:06:35 -05:00
return phys