Spring Simulation
Posted: Mon Aug 03, 2009 10:34 pm
I'm trying to make a simulation of a spring.
It is almost working, except that the spring skews about 2 pixels downward every oscillation. Anyone want to point out what is wrong?
http://love.pastebin.com/f62228309
It is almost working, except that the spring skews about 2 pixels downward every oscillation. Anyone want to point out what is wrong?
http://love.pastebin.com/f62228309
Code: Select all
function load()
ball={}
ball.y=50
ball.v=0
gravity=30
spring={}
spring.y=200
spring.top=200
spring.k=200
end
function keypressed(key)
if key == love.key_tab then
love.system.restart()
end
end
function update(dt)
--local dt=dt*5
ball.v=ball.v+gravity*dt
ball.y=ball.y+ball.v*dt
if ball.y>=spring.k then
spring.y=ball.y
ball.v=ball.v-(spring.k-spring.top)*dt
spring.top=spring.top-ball.v*dt
if spring.top>200 then
spring.top=200
end
end
end
function draw()
love.graphics.setColor(0,255,0)
love.graphics.rectangle(love.draw_fill,85,spring.y,30,spring.top)
love.graphics.setColor(255,0,0)
love.graphics.circle(love.draw_fill,100,ball.y,10)
end