Created
July 21, 2014 20:52
-
-
Save josefnpat/b9bbb242a690d71d238f to your computer and use it in GitHub Desktop.
Pendulums Class - Love
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- http://i.imgur.com/0X0LTRs.gif by /r/davebees | |
-- recreated (to some degree) by @josefnpat | |
-- pendulumsclass.lua | |
local pendulums = {} | |
function pendulums.new(x,y,r,lcount) | |
local self = {} | |
-- Overrides | |
self._x = x or love.graphics.getWidth()/2 | |
self._y = y or love.graphics.getHeight()/8 | |
self._r = r or love.graphics.getHeight()*6/8 | |
self._lcount=lcount or 10 | |
-- Inits | |
self._dt = 0 | |
-- Methods | |
self.draw = pendulums.draw | |
self.update = pendulums.update | |
self.drawpendulum = pendulums.drawpendulum | |
return self | |
end | |
function pendulums:draw() | |
for i = 1,self._lcount do | |
self:drawpendulum( (self._lcount - i + 1)/2, i/self._lcount*self._r ) | |
end | |
end | |
function pendulums:update(dt) | |
self._dt = self._dt + dt | |
end | |
function pendulums:drawpendulum(i,r) | |
local angle = math.sin(self._dt*i) + math.pi/2 | |
local xt = self._x + r*math.cos(angle) | |
local yt = self._y + r*math.sin(angle) | |
love.graphics.line(self._x,self._y,xt,yt) | |
love.graphics.circle("fill",xt,yt,6) | |
end | |
return pendulums |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment