If anyone's wondering, here's the code for the GravityWell class:
Code: Select all
GravityWell = class('GravityWell', PhysicalEntity)
function GravityWell:initialize(power, x, y, epsilon)
super.initialize(self)
self.power = power * 10000 or 10000
epsilon = epsilon or 100
self.epsilonSq = epsilon * epsilon
self:setPosition(x or 0, y or 0)
end
function GravityWell:update(dt, rdt)
for e in self.world:getIterator() do
if e.body then
lx = self.x - e.x
ly = self.y - e.y
ldSq = lx * lx + ly * ly
if ldSq ~= 0 then
ld = math.sqrt(ldSq)
if ldSq < self.epsilonSq then ldSq = self.epsilonSq end
lfactor = (self.power * dt) / (ldSq * ld)
oldX, oldY = e.body:getLinearVelocity()
e.body:setLinearVelocity(oldX + lx * lfactor, oldY + ly * lfactor)
end
end
end
end