The velocity of a particle is its displacement per frame.
Given a particle of radius r, at position p and velocity v in frame n, the particle will collide with a wall when it is travelling towards the wall in frame n and in frame n+1 the centre of the particle, at p + v, is within a distance r from the wall or is moving away from the wall.
For example consider a left hand wall, with plane parallel to the YZ plane, and a particle travelling towards it with velocity (dx, dy, dz), dx < 0.
In frame n, the position of the particle is (x, y, z) and during the time between frame n and frame n+1 it overlapped the wall. At frame n+1, the particle may still be overlapping the wall and its centre may be to the right or left of the wall or, the particle may have gone beyond the wall completely. The 2D diagrams show the latter case but the calculations apply in all cases. Fig 16 indicates the particle passing through the wall.
The particle at frame n+1, is at position (x + dx, y + dy, z + dz)
Sometime between frame n and frame n+1 the particle would just touch the wall as in Fig 17.
At this position let the displacement of the particle from its position in frame n be (dx1, dy1, dz1) and its displacement from this position to its position in frame n+1 be (dx2, dy2, dz2). It follows that (dx, dy, dz) = (dx1, dy1, dz1) + (dx2, dy2, dz2), see Fig 18.
At the point of contact with this wall the component of velocity in the x direction would reverse and so the remaining x axis displacement before frame n+1 would be -dx2, see Fig 19.
The particle velocity, after the collision is now (-dx, dy, dz). For frame n+1, we need the position of the particle to be at (x + dx1 - dx2 , y + dy, z + dz). Since before frame n+1, is displayed the velocity (-dx, dy, dz) will be added and so we need it position before this is done to be (x + dx1 - dx2 , y + dy, z + dz) - (-dx, dy, dz), see Fig 20.
(x + dx1 - dx2 , y + dy, z + dz) - (-dx, dy, dz)
= (x + dx1 - dx2 + dx, y, z)
= (x + dx1 - dx2 + dx1 + dx2, y, z)
= (x + 2dx1, y, z)
Which is the reflection of the particle at frame n in the plane parallel to the YZ plane through the centre of the particle, see Fig 21.
This means after frame n is displayed the particle's position has to be reset as indicated.
Given a two particles P and Q of radius r, at positions p and q with velocities v and u repectively travelling towards each other in frame n. In frame n+1 the centres of the particle are at p + v and q + u. Between frame n and frame n+1* the two particles would collide when there is a time t <= 1 when the distance between the positions of the particles is 2r. See Fig 22.
At time t, P is at p + vt and Q is at p + ut and P an Q will collide if there is a solution for t to the equation
(p + vt - (q + ut)).(p + vt - (q + ut)) = 4r2
with 0 < t <= 1
This equation is a quadratic equation in t of the form at2 + bt + c = 0
(p + vt - (q + ut)).(p + vt - (q + ut)) = 4r2
(p - q +(v - u)t).(p - q +(v - u)t) = 4r2
(p - q).(p - q) + 2(p - q).(v - u)t + (v -u).(v - u)t2 = 4r2
(v -u).(v - u)t2 + 2(p - q).(v - u)t + (p - q).(p - q) - 4r2
so a = (v -u).(v - u), b = 2(p - q).(v - u), c = (p - q).(p - q) - 4r2
There will be a collision between frame n and frame n+1 provided b2 - 4ac > 0. In this case there will be two solutions for t the smallest value being where the particles first touch and the other at the end of the overlap.
Based on the above the following Playground shows two spheres and their paths. Should they collide their positions at first touch are shown. Their start positions and velocities may be set at lines 35 to 38.
Since only the velocities along the line joining the centres of the particles are affected during the collision we need to consider axes along this radial line and a tangential line. The particles are at first contact at time t, with P at p + vt and Q at q + ut
Let m = p + vt - (q + ut) and n = m/|m| a unit vector.
Then vr = (v.n)n is the component of v along n, and ur = (u.n)n the component of u along n and after collision these components are swapped. The tangential components for P and Q before collision are vt = v - (v.n)n and ut = u - (u.n)n respectively.
After collision the velocity of
P is va = ur + vt = (u.n)n + v - (v.n)n = v + ((u - v).n)n and that of
Q is ua = vr + ut = (v.n)n + u - (u.n)n = u + ((v - u).n)n
Given that the particles collide between frame n and frame n+1 what will the positions of P and Q be during frame n+1?
In frame n, the position of particle P is (px, py, pz) and the position of Q is (qx, qy, qz). For some time t <= 1 the particles will make first contact. Fig 23 shows the positions of P and Q for frame n, their positions at time t, and their positions for frame n+1 when the collision is not detected. The radial and tangential components of their displacements (= velocities) are also shown upto and after first contact.
The displacement v can be split in vt and v(1 - t) and u can be split into u and u(1 - t) which take the particles to the point of contact and after the first contact. During the frame transition it is at the point of contact the displacements along the line of centres are exchanged.
Fig 24 shows the rearrangement of the component displacements and positions of the spheres when the collision is detected and component velocities along the line joining the centres are exchanged. Just before frame n+1 this gives
particle P at p + vrt + vtt + ur(1 - t) + vt(1 - t) = p + vrt + ur(1 - t) + vt
and particle Q at q + urt + utt + vr(1 - t) + ut(1 - t) = q + urt + vr(1 - t) + ut
as in Fig 24
However, since before frame n+1, is displayed the velocities of P and Q will be added to the positions of P and Q and so we need these positions to have their velocities subtracted so that the correct positions are displayed during frame n+1. See Fig 25
p + vrt + ur(1 - t) + vt - (ur + vt)
= p + vrt + ur(1 - t) - ur
= p + vrt + ur(1 - t) - urt - ur(1 - t)
= p + vrt - urt
= p + (vr - ur)t
q + urt + vr(1 - t) + ut - (vr + ut)
= q + urt + vr(1 - t) - vr
= q + urt + vr(1 - t) - vrt - vr(1 - t)
= q + urt - vrt
= q + (ur - vr)t
After a collision these values are used to recalculate the positions of particles before frame n+1.
The following Playground shows the first contact position of two particles and their paths before and after collision. Start position and velocities may be set on lines 36 to 39.
This Playground fires particles randomly towards each other to see the effect when they collide.
Combining all of the above the following Playgrounds put many particles inside a box and have them collide with the box and each other.
Maximum speeds, for both, can be changed on lines 21 to 23.