Skip to content

Instantly share code, notes, and snippets.

@qwzybug
Created February 27, 2009 06:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qwzybug/71321 to your computer and use it in GitHub Desktop.
Save qwzybug/71321 to your computer and use it in GitHub Desktop.
Plank = {}
Plank.new = function(world, x, y, w, h, offset)
body = love.physics.newBody(world, 0, 0, 0)
body:setAngularDamping(1000)
shape = love.physics.newRectangleShape(body, x, y, w, h)
body:setMassFromShapes()
return {
shape = shape, body = body, offset = offset,
force =
function(self, t)
return math.sin(self.offset * math.pi / 10 + t)
end
}
end
function load()
world = love.physics.newWorld(760, 500)
world:setGravity(0, 100)
ballBody = love.physics.newBody(world, 740, 20)
circleShape = love.physics.newCircleShape(ballBody, 30)
circleShape:setRestitution(2)
ballBody:setMassFromShapes()
ballBody:setVelocity(-100, 0)
border = love.physics.newBody(world, 0, 0, 0)
borderShapeLeft = love.physics.newRectangleShape(border, 0, 200, 1, 400)
borderShapeRight = love.physics.newRectangleShape(border, 760, 200, 1, 400)
bars = {}
for i = 1,25 do
bars[i] = Plank.new(world, 30 * i - 10, 400, 20, 500, i)
end
t = 1
end
function update(dt)
t = t + dt
for _,bar in pairs(bars) do
bar.body:setVelocity(0, 50 * bar:force(t))
end
world:update(dt)
end
function draw()
love.graphics.setColor(255, 0, 0)
love.graphics.circle(love.draw_fill, ballBody:getX(), ballBody:getY(), 30, 30)
for _,bar in pairs(bars) do
love.graphics.polygon(love.draw_fill, bar.shape:getPoints())
end
love.graphics.polygon(love.draw_line, borderShapeLeft:getPoints())
love.graphics.polygon(love.draw_line, borderShapeRight:getPoints())
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment