Showing posts with label Physics. Show all posts
Showing posts with label Physics. Show all posts

Sunday, October 13, 2013

n body simulation

# this is a n-body simulation , use arrow keys to keep it centered
# Number of inital particles


graphsize 600,600
fastgraphics
n=100
dim x(n+1)
dim y(n+1)
dim xspeed(n+1)
dim yspeed(n+1)
dim xac(n+1)
dim yac(n+1)
dim mass(n+1)
dim radius(n+1)
# initializing variables ##############
for p = 1 to n
a=rand*2*pi
r=rand*300
x[p]=r*cos(a)+300
y[p]=r*sin(a)+300
s=rand*2*pi/20
xspeed[p]=cos(a+s)-cos(a)
yspeed[p]=sin(a+s)-sin(a)
radius[p]=3
mass[p]=radius[p]^3
next p

gravity=0
dist=0
colision = 0
dx=0
dy=0
v=0
h=0

loop:
# arrow keys####################
a = key
if 16777234=a then h=h-5
if 16777236 =a then h=h+5
if 16777237=a then v=v-5
if 16777235=a then v=v+5

for u = 1 to n
for t = 1 to n
if u<>t and mass[t]>0 and mass[u]>0 then
dx=x[u]-x[t]
dy=y[u]-y[t]
dist=(dx^2 + dy^2)^(1/2)
collide = false
if dist < radius[u]+radius[t] then collide = true
# gravity code################
if collide = true then
x[u]=(x[u]*mass[u]+x[t]*mass[t])/(mass[u]+mass[t])
y[u]=(y[u]*mass[u]+y[t]*mass[t])/(mass[u]+mass[t])
xspeed[u]=(xspeed[u]*mass[u]+xspeed[t]*mass[t])/(mass[u]+mass[t])
yspeed[u]=(yspeed[u]*mass[u]+yspeed[t]*mass[t])/(mass[u]+mass[t])
mass[u]=mass[u]+mass[t]
radius[u]=mass[u]^(1/3)
mass[t]=0
radius[t]=0
end if

# colision code################
if collide = false then
gravity = mass[t]*mass[u]/dist^2
xac[u]= gravity*dx/(1000*mass[u])
yac[u]= gravity*dy/(1000*mass[u])
xspeed[u]=xspeed[u]-xac[u]
yspeed[u]=yspeed[u]-yac[u]
x[u]=x[u]+xspeed[u]
y[u]=y[u]+yspeed[u]
end if
###############################
end if
next t
color white
circle x[u]+h,y[u]+v,radius[u]
next u
refresh
clg
color black
rect 0,0,600,600

goto loop

Wednesday, November 28, 2012

Doppler efect

#Doppler efect
fastgraphics
x=0
loop:
for b = 1 to 10
color white
rect 0,0,300,300
x=x+1
a=.5
for n = 1 to 255 step 5
a=a*-1+.5
color rgb(255-a*n,255-a*n,255-a*n)
circle x + (n-b)*.5-255,150,255-(n-b)
next n
refresh
clg
next b
goto loop

Friday, November 9, 2012

Golden Waves

graphsize 600,600
fastgraphics
for t=1 to 60 step .1
color darkred
rect 0,0,600,600
For y1 = 0 to 24
For x1 = 0 to 24
x=12*(24-x1)+12*y1
y=-6*(24-x1)+6*y1+300
d= ((10-x1)^2+(10-y1)^2)^.5
h=60*sin(x1/4+t)+65
if t>10 and t<20 then h=60*sin(y1/4+t)+65
if t>20 and t<30 then h=60*sin((x1-y1)/4+t)+65
if t>30 and t<40 then h=30*sin(x1/2+t)+30*sin(y1/2+t)+65
if t>40 and t<50 then h=60*sin((x1+y1)/4+t)+65
if t>50 and t<60 then h=60*sin(d*.3+t)+65
color rgb(100+h,100+h,h)
poly{x,y-h,x+10,y+5-h,x+20,y-h,x+10,y-5-h}
color rgb(60,60,0)
poly{x,y-h,x+10,y+5-h,x+10,y,x,y-5}
color rgb(150,150,0)
poly{x+10,y+5-h,x+10,y,x+20,y-5,x+20,y-h}
next x1
next y1
refresh
clg
next t

Friday, October 12, 2012

Multiparticle collider


Rem 2D collisions
fastgraphics
m=6
n=m^2
Dim x(n)
Dim y(n)
Dim vx(n)
Dim vy(n)
Dim mass(n)
Dim colisionflag(n,n)
ed=1
u=0
rem initial variables
for a = 0 to m-1
for b = 0 to m-1
x[u]=50*a+10
y[u]=50*b+10
vx[u]=rand-.5
vy[u]=rand-.5
mass[u]=7
u=u+1
next b
next a
Rem main loop
While 1=1
for u = 0 to n-1
If x[u]<mass[u] or x[u]>(300-mass[u]) then vx[u]=-vx[u]
If y[u]<mass[u] or y[u]>(300-mass[u]) then vy[u]=-vy[u]
x[u]=x[u]+vx[u]
y[u]=y[u]+vy[u]
color rgb (u*7,0,255-u*7)
circle x[u],y[u],mass[u]
next u
refresh
clg
gosub colision
end while
colision:
rem collision detection
for u1 = 0 to n-2
for u2 = u1+1 to n-1
dx = x[u2]-x[u1]
dy = y[u2]-y[u1]
distance = (dx*dx+dy*dy)^.5
if distance < (mass[u1]+mass[u2]) then
if colisionflag[u1,u2]=0 then
rem vx and vy calc
ax=dx/distance
ay=dy/distance
va1=vx[u1]*ax+vy[u1]*ay
vb1=-vx[u1]*ay+vy[u1]*ax
va2=vx[u2]*ax+vy[u2]*ay
vb2=-vx[u2]*ay+vy[u2]*ax
vaP1=va1 + (1+ed)*(va2-va1)/(1+mass[u1]/mass[u2])
vaP2=va2 + (1+ed)*(va1-va2)/(1+mass[u2]/mass[u1])
vx[u1]=vaP1*ax-vb1*ay
vy[u1]=vaP1*ay+vb1*ax
vx[u2]=vaP2*ax-vb2*ay
vy[u2]=vaP2*ay+vb2*ax
colisionflag[u1,u2]=1
end if
else
colisionflag[u1,u2]=0
end if
next u2
next u1
return

Tuesday, September 27, 2011

Static Wave


Rem the purple wave is made out of two simple sinusoidal waves. Change the frequencies and amplitudes and see what happens .
fastgraphics
Amplitude1=30
Frequency1=2
Amplitude2=30
Frequency2=8
for t = 1 to 1000 step .1
clg
for n = 1 to 31.4 step 0.1
r=Amplitude1*sin(t )*Sin(n/(10/Frequency1))
b=Amplitude2*sin(t )*Sin(n/(10/Frequency2))
p= r+b
color red
circle 10*n,r+250,3
color blue
circle 10*n,b+200,3
color purple
circle 10*n,p+100,3
next n
refresh
next t

Tuesday, July 12, 2011

Complex wave


Rem the purple wave is made out of two simple sinosoidal waves
fastgraphics
for t = 1 to 1000 step 0.1
clg
for n = 1 to 30  step 0.1
r=30*sin(t+n*0.2)
b=10*sin(t+n*0.8)
p= r+b
color red
circle 10*n,r+250,3
color blue
circle 10*n,b+200,3
color purple
circle 10*n,p+100,3
next n
refresh
next t

Thursday, November 4, 2010

Orbit


rem parameters are for a geostationary orbit arround earth
font "arial",10,100
height = 36000000
speed=3070
radius=6371000
time=0 : scale=400000 :dt=40 :g=9.81
x=0 : y=radius+height
px=0:py=0
xspeed=speed : yspeed=0
fastgraphics
loop:
dist=((x-px)^2 + (y-py)^2)^(1/2)
gravity= g*(radius/dist)^2
dx=x-px : dy=y-py
xac= (dx/dist)*gravity : yac= (dy/dist)*gravity
xspeed=xspeed-xac*dt : yspeed=yspeed-yac*dt
x=x+xspeed*dt : y=y+yspeed*dt
clg
Text 150 + x/scale,150 + y/scale,"[]o[]"
circle 150+px,150+py,radius/scale
time=time+dt
speed = int(((xspeed)^2 + (yspeed)^2)^(1/2))
text 10,10,"height = "+dist/radius+"radius"
text 10,30,"speed = "+speed+"m/s"
text 10,50,"time = "+int(time/3600)+"h"
text 10,70,"grav-acc = "+(radius/dist)^2+"g"
text 10,90,"centri-acc = "+(speed^2/dist)/g+"g"
refresh
goto loop

Monday, November 1, 2010

Wave Interference


fastgraphics
clg
rem wave source coordetanes
x1=150 : y1= 300 : x2= 0 : y2=0
for n = 1 to 100 step .2
For x = 0 to 300 step 5
For y = 0 to 300 step 5
d1 = ((x-x1)^2+(y-y1)^2)^0.5
d2 = ((x-x2)^2+(y-y2)^2)^0.5
s1=60*sin(d1/5-n)
s2=60*sin(d2/5-n)
if 5*n>=d2 then
color rgb(122+s2,122+s2,122+s2)
else
color rgb(122+s1,122+s1,122+s1)
end if
if 5*n>=d1 and 5*n>=d2 then color rgb( 122+s1+s2, 122+s1+s2,122+s1+s2)
if 5*n<=d1 and 5*n<=d2 then color grey
circle x,y,3
next y
next x
refresh
next n

Wednesday, October 27, 2010

Force and Drag

clg
fastgraphics
rem Initial variables
speed = 0:density=1.2 : area=2:pullforce=1000
mass=1400: dist =0 :time=0 :dragcoef=.35
dt=0.005 : c=0
font "arial",15,15
while speed<200
drag=0.5*density*speed^2*area*dragcoef
acc=((pullforce-drag)/mass)*dt
clg
speed = speed + acc
dist = dist + (speed)*dt
time = time + dt
text 10,10 ,"Time "+int(time)+" sec"
text 10,30, "Speed "+int(speed)+"m/s "+int(speed*3.6)+"km/h"
text 10,50 ,"Distance "+int(dist)+"m"
text 10,70 ,"Acceleration "+((pullforce-drag)/mass)+"m/s^2"
text 10,90 ,"Work "+int(dist*pullforce/1000)+" kj"
text 10,110 ,"Power "+int(speed*pullforce/1000)+" kw "+int(speed*pullforce/750)+"hp"
c=c + (speed)*dt
text 150,220," |''''''|'''''''|''''\"
text 150,235," |''''''|'''''''|'''''o"
text 150,250," '-(0)---(0)='"
text 1-c,255,"......................................."+(dist-c)+"m .............................................................."
if 150-c<0 then c=c-150
refresh
end while

Tuesday, October 26, 2010

Force and Friction

clg
fastgraphics
rem Initial variables
speed = 0:pullforce=500
mass=40: dist =0 :time=0 :frictioncoef=.5
dt=0.005 : c=0 : fforce=mass*9.81*frictioncoef
acc=((pullforce-fforce)/mass)*dt
while time<30
clg
if pullforce > fforce then speed = speed + acc
dist = dist + (speed)*dt
time = time + dt
text 10,10 ,"Time "+time+" sec"
text 10,20, "Speed "+speed+"m/s"
text 10,30 ,"Distance "+int(dist)+"m"
text 10,40 ,"Acceleration "+((pullforce-fforce)/mass)+"m/s^2"
text 130,120 ,"Mass "+mass+"Kg"
text 10,140 ,fforce+" N <---Friction ---------------------------Pull--->"+pullforce+" N"
rect 120,145,60,20
c=c + (speed)*dt
text 1-c,155,"......................................."+(dist-c)+"m .............................................................."
if 150-c<0 then c=c-150
refresh
end while

Sunday, October 24, 2010

Linear colisions

fastgraphics

m1=4 : v1=10 :p1=57
m2=20 : v2=0 : p2=150
elast=.5
font "arial",15,15
While abs(p1-150)<151 and abs(p2-150)<151
clg
Text 0,10,"Pos1= "+int(p1)
Text 130,10,"Vel1= "+v1
Text 0,40,"Pos2="+int(p2)
Text 130,40,"Vel2="+v2
circle p1,150,m1
circle p2,150,m2
refresh
p1=p1+v1
p2=p2+v2
if abs(p1-p2)<=m1+m2 then
v11=v1
v1=(elast*m2*(v2-v1)+m1*v1+m2*v2)/(m1+m2)
v2=(elast*m1*(v11-v2)+m1*v11+m2*v2)/(m1+m2)
end if
pause.05
end while

Friday, October 15, 2010

Projectile

clg
fastgraphics
rem Initial variables
speed = 70 :gravity = 9.81
angle=70: height=0 :h1=0 :distance=0
t=0:dt=0.005

angle=pi*angle/180
hspeed = speed*cos(angle)
vspeed = speed*sin(angle)
while height>=h1
clg
t=t+dt : h1=height
vspeed = vspeed - gravity*dt
height = height + vspeed*dt
distance = distance + hspeed*dt
circle distance,300-height,3
text 10,10 ,"Time "+t
text 10,20, "Height "+height
text 10,30 ,"Distance "+distance
refresh
end while