Spel22: Forsränning

IMG_88011

Vi programmerar vattenströmning där vattnet består av ett antal vattenpartiklar som påverkar varandra via elastiska och viskösa fjädrar (som stötdämparna i en bil).

Template 1: (Titta på Flume Ride)

— Fluid Flow
N=20
M=30
x={}
v={}
a={}
f={}

for i=1,N do
x[i]={}
v[i]={}
f[i]={}
a[i]={}
end

for n=1,N do
for m=1,M do
x[n][m]=vec2(-100+2*n,640+2*m)
v[n][m]=vec2(20,0)
f[n][m]=vec2(0,0)
a[n][m]=vec2(0,0)
end
end

length=0
dt=0.1

function setup()
parameter.number(“V”,0,100,30)
parameter.number(“E”,0,1000,100)
end

function draw()
background(40, 40, 50)
for i=1,700 do
fill(64, 255, 0, 255)
ellipse(i,ground(i),20)
end
q=1
for i=1,N do
for m=1,M do
f[i][m]=vec2(0,0)
if q==1 then
for j=1,N do
length=(x[i][m]-x[j][m]):len()
length=length
f[i][m]=f[i][m]-V*length*(v[i][m]-v[j][m])/(length+1)^4
f[i][m]=f[i][m]-E*(length-10)*(x[i][m]-x[j][m])/(length+1)^4
end
end
a[i][m]=f[i][m]+vec2(0,-50)
v[i][m]=v[i][m]+a[i][m]*dt
if x[i][m].y<ground(x[i][m].x)+20 then
v[i][m].y=0
end
x[i][m]=x[i][m]+v[i][m]*dt
fill(0, 54, 255, 255)
ellipse(x[i][m].x,x[i][m].y,15)
end
end
sprite(“SpaceCute:Beetle Ship”,x[10][15].x,x[10][15].y,30)
end

function ground(x)
local ground=0
if x<100 then
ground =600
end
if x>=100 then
ground =500
end
if x>200 then
ground = 400
end
if x>300 then
ground =300
end
if x>=400 then
ground =200
end
if x>500 then
ground = 100
end
return ground
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