Swarm of Balls: Arrays and Reflections
Twenty-four balls fall in a circular arena and bounce off its wall. Each one has a position and a velocity, 96 numbers in all – and in REMcalc 1.1 the program keeps them in four arrays. Reflection off a circle is a beautiful piece of vector geometry, and one short function computes it.
Arrays instead of a hundred variables
The statement dim x(n), y(n), vx(n), vy(n) creates four arrays of 24 elements each (numbered from 1). Ball
number k has the position x(k), y(k) and the velocity vx(k), vy(k).
One step of the simulation is then a loop over all the balls:
vy(k) = vy(k) − g x(k) = x(k) + vx(k) y(k) = y(k) + vy(k)
This is the simplest numerical solution of motion (Euler's method): in each step gravity takes away a little of the vertical velocity, and the position moves by the velocity. Without arrays the task would need the variables x1 … x24, y1 … y24 and the same calculation written out 24 times. That is exactly what the photon reflection tasks in the Waves and Optics library used to look like – with arrays, the task with photons in an ellipse shrank from 537 to 132 lines.
Reflection off a circle
When a ball is farther from the centre than the arena's radius, it has hit the wall. The normal to the circle at the point of impact points away from the centre, so the unit normal vector is simply the position divided by the distance:
n = (x, y) / d, d = √(x² + y²)
The reflection reverses only the component of the velocity along the normal; the component along the wall stays the same:
s = v · n = vx·nx + vy·ny, v′ = v − 2s·n
The condition if s > 0 makes sure the ball bounces only when it is really moving outwards. Otherwise
a ball that got slightly past the wall in one step could get “stuck” and bounce back and forth there.
All of this is done by the function odraz(k) (Czech for reflection): its helper variables d, nx, ny, s are local,
it changes only the velocity of ball k in the arrays, and it returns 1 if a bounce occurred – the main program adds
it straight to the counter (pocet = pocet + odraz(k)).
x, y, vx, vy, the arena drawn with the
curve command, the function odraz(k) and balls with trails (shape, trail, move).The arena in one line
The circle is drawn by the new curve command – a parametric curve given by two expressions and a range of the parameter:
curve 100, R*cos(t), R*sin(t), t = 0..2*pi, white, 3
REMcalc computes it in 200 segments (with t = a..b:krok, in steps of the given size krok). Ellipses,
parabolas, spirals or Lissajous figures can be drawn the same way; a straight segment is drawn by the line command.
Try it yourself
- Install REMcalc 1.1 (Download; the Free edition is enough – arrays in it can hold up to 2,500 elements).
- In the Window → Expression Library menu, choose the My Formulas library and click New… with the BASIC task box checked.
- Paste the program and run it. Try more balls (
n = 60), stronger gravity (0.004 → 0.01) or none at all – then the balls fly in straight lines, like light in a round mirror.
Comments and variable names in the program are in Czech.
' Roj: stav 24 kuliček v polích x(k), y(k), vx(k), vy(k); aréna nakreslená příkazem curve,
' odraz od kružnice počítá funkce odraz(k) s lokálními proměnnými
mode rad
scene -16, 16, -10, 10
n = 24
R = 8.6
dim x(n), y(n), vx(n), vy(n)
curve 100, R*cos(t), R*sin(t), t = 0..2*pi, white, 3
text 900, -15.5, 9.6, "Pole a funkce: 24 kuliček v kruhové aréně", 22, white
pocet = 0
for k=1 to n
x(k) = 5*cos(k*0.9)*0.8
y(k) = 5*sin(k*1.7)*0.8
vx(k) = 0.22*cos(k*2.4)
vy(k) = 0.22*sin(k*2.4)
shape k, ball, x(k), y(k), 20, k*15
trail k, 18
next
def odraz(k)
local d, nx, ny, s
odraz = 0
d = sqrt(x(k)^2 + y(k)^2)
if d > R - 0.35 then
nx = x(k)/d
ny = y(k)/d
s = vx(k)*nx + vy(k)*ny
if s > 0 then
vx(k) = vx(k) - 2*s*nx
vy(k) = vy(k) - 2*s*ny
odraz = 1
endif
endif
enddef
for krok=1 to 420
for k=1 to n
vy(k) = vy(k) - 0.004
x(k) = x(k) + vx(k)
y(k) = y(k) + vy(k)
pocet = pocet + odraz(k)
move k, x(k), y(k)
next
text 901, -15.5, 8.6, "odrazů: %g", 18, gray; pocet
delay 30
next
delay 1000