Files
qphys/ball.lua
T
2021-12-28 15:40:08 -08:00

66 lines
1.4 KiB
Lua

ball = {
__count = 0,
id = nil,
pos = vec2:new(0,0),
last_pos = vec2:new(0,0),
vel = vec2:new(0,0),
rad = 1,
col = 7,
mass = function(self)
return self.rad
end,
draw = function(self, shadow)
if (shadow == true) then
circfill(self.last_pos.x, self.last_pos.y, self.rad, self.col + 8)
end
circfill(self.pos.x, self.pos.y, self.rad, self.col)
end,
move = function(self)
self.last_pos = self.pos
self.pos += self.vel
end,
edge_bounce = function(self)
edge_normal = vec2:new()
for axis in all({"x","y"}) do
val = self.pos[axis]
if (val - self.rad < 0) then
self.pos[axis] = self.rad
edge_normal[axis] = 1
elseif (val + self.rad > 127) then
self.pos[axis] = 127 - self.rad
edge_normal[axis] = -1
end
end
if (edge_normal.x != 0 or edge_normal.y != 0) then
self.vel = self.vel:bounce(edge_normal)
end
end,
check_collisions = function(self, balls)
for b in all(balls) do
if (b.id != self.id and (collisions[b] == nil or collisions[b][self] == nil)) then
local diff = self.pos - b.pos
if (diff:length() < self.rad + b.rad) then
collisions[self] = collisions[self] or {}
collisions[self][b] = diff
end
end
end
end,
}
ball.__index = ball
function ball:new(o)
ball.__count += 1
local o = o or {}
o.id = ball.__count
o.col = o.col or o.id % 8
setmetatable(o, self)
o.__index = self
return o
end