Spel29: Studsande Boll på Fjädrande Underlag

bouncing ball                                           Watch bouncing sibling balls

Vi programmerar dynamiken hos en stel boll som studsar och rullar på ett fjädrande underlag.

Template: (Titta på simulering)

— Bouncing Rolling Ball on Elastic Support

function setup()
print(“Boll som studsar och rullar på ett elastiskt underlag.”)
print(“Parametrar: tidssteg = 1/M, bollradie R, dämpning D.”)
print(“Nytt utgångsläge genom touch.”)
b={}
I=100
E=10

parameter.number(“M”,10,1000,100)
parameter.number(“R”,10,1000,200)
parameter.number(“D”,0,100,1)

for i=1,I do
b[i]=R*vec2(math.cos(2*math.pi*i/I),math.sin(2*math.pi*i/I))
end

cyl=vec2(200,700)
vel=vec2(0,0)
r=vec2(0,0)
force=vec2(0,0)
fill(0, 49, 255, 255)
t=0

end

function draw()
dt=1/M
t=t+1
background(216, 255, 0, 255)
fill(0, 38, 255, 255)
strokeWidth(0)
force=vec2(0.5,-4000)
m=0
for i=1,I do
dx= cyl.x+b[i].x
dy= cyl.y+b[i].y
if dy<400 then
m=m+1
force.y=force.y+E*(400-dy)
if m<3 then
fill(8, 231, 14, 255)
strokeWidth(0)
rect(0,200,800,dy-200)
end
stroke(255*(400-dy)/40,255*(dy-380)/20,0, 255)
strokeWidth(5)
line(dx,200,dx,dy)
end
end
vel=(1-0.01*D)*vel+force*dt
cyl=cyl+vel*dt

strokeWidth(0)
fill(0, 34, 255, 255)
ellipse(cyl.x,cyl.y,2*R)
fill(25, 26, 22, 255)
rect(0,190,800,20)
for i=1,I do
b[i]=R*vec2(math.cos(2*math.pi*(i/I-t/1000)),math.sin(2*math.pi*(i/I-t/1000)))
fill(255, 0, 242, 255)
ellipse(cyl.x+b[i].x,cyl.y+b[i].y,10)
end
end
function touched(touch)
if touch.state==ENDED then
cyl=vec2(touch.x,touch.y)
end
end

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s