Skip to content

Instantly share code, notes, and snippets.

@fujidig
Created March 23, 2014 13:52
Show Gist options
  • Save fujidig/9723329 to your computer and use it in GitHub Desktop.
Save fujidig/9723329 to your computer and use it in GitHub Desktop.
w = 640.0 : h = 480.0
r = 10.0
rp = 3.0
n = 200
ddim x, n
ddim y, n
ddim vx, n
ddim vy, n
dim collided, n ,n
for i, 0, n
x.i = double(rnd(w))
y.i = double(rnd(h))
theta = double(rnd(1000)) / 1000 * (2 * M_PI)
v = 6.0 * (double(rnd(1000)) / 1000)
vx.i = v * cos(theta)
vy.i = v * sin(theta)
next
*mainloop
gosub *collision
gosub *collision_with_wall
redraw 0
color : boxf
for i, 0, n
x.i += vx.i
y.i += vy.i
hsvcolor 192 * i / n, 255, 255
circle x.i - rp, y.i - rp, x.i + rp, y.i + rp, 1
next
redraw
await 33
goto *mainloop
*collision
for i, 0, n
for j, i + 1, n
dx = x.i - x.j
dy = y.i - y.j
if collided.i.j {
collided.i.j -= 1
}
if collided.i.j == 0 and dx * dx + dy * dy <= r * r {
collided.i.j = 5
; http://homepage2.nifty.com/eman/dynamics/collision.html
vx_i = (-vx.i + vx.j) + vx.i
vx_j = (-vx.j + vx.i) + vx.j
vx.i = vx_i : vx.j = vx_j
vy_i = (-vy.i + vy.j) + vy.i
vy_j = (-vy.j + vy.i) + vy.j
vy.i = vy_i : vy.j = vy_j
}
next
next
return
*collision_with_wall
for i, 0, n
if x.i < 0 {
vx.i = -vx.i
x.i = 0.0
}
if x.i > w {
vx.i = -vx.i
x.i = w
}
if y.i < 0 {
vy.i = -vy.i
y.i = 0.0
}
if y.i > h {
vy.i = -vy.i
y.i = h
}
next
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment