Gravity Well Test

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Gravity Well Test

Post by BlackBulletIV »

After doing a bit of research, I put together a quick gravity well test. This also utilizes my framework, Grace You can read the description (and watch the video), which contains more information, here: http://www.youtube.com/watch?v=chH9j3-ia9Q



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
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests