I have a physics simulator that simulates a bunch of balls moving and colliding with each other, and I would like to be able to "link" two balls together so they stick to each other (are always tangent to each other), i.e. I want to be able to make a chain of balls. What is the general approach to this? I've tried various things including averaging the velocities parallel to the delta vector (position1 - position2), but none yet work very well. Of course I could just apply a very large attraction force to the balls, but that seems like a hack; I want an elegant solution.

PS. The balls have equal masses.

-

The change is not so simple, since I assume your balls are not solid objects, and they cannot rotate. This is the usual situation in these types of simulations--- you don't let the balls start spinning around an axis. This is reasonable if all the forces are attractions and repulsions along the center-line, because then none of the balls will start spinning if they aren't spinning initially.

But when balls stick together, the combined stuck system can always spin around, and then there is the question of when the two stuck balls spin, whether each ball is spinning (like two solid balls stuck together in real life), or whether the two balls just orbit each other without either one spinning (like two atoms stuck together in a molecule). The two situations have different dynamics, since it takes more energy to spin up a system where the balls are spinning than one in which the two balls orbit.

For maximal simplicity, I will assume the balls are orbiting, not spinning. Then you describe the stuck system by the positions of the two centers of mass.

In this situation, the complete description is by the position coordinates $(x_i,y_i,z_i)$ for ball number i, and the velocity $(\dot{x}_i,\dot{y}_i,\dot{z}_i)$. If balls 1 and 2 are stuck together, you then want to ensure that that

$$(x_1-x_2)^2 + (y_1-y_2)^2 + (z_1-z_2)^2 = R^2$$

Differentiating this with respect to time, you learn that

$$(X_1 - X_2) \cdot (V_1-V_2) = 0$$

in other words, the relative velocity of the two balls is always perpendicular to their mutual acceleration. This is the constraint you want to enforce. In order to ensure this holds, you need a constraint force on the balls N, with N acting on ball 1 and -N acting on ball 2, N along the line of separation, whose role is to make sure this stays true as the balls accelerate.

As a first approximation, you update the X's using the V's $X_i \rightarrow X_i +\epsilon V_i$ where $\epsilon$ is the timestep. Then you update the V's using the forces (without enforcing the constraint) $V_i \rightarrow V_i +\epsilon F$.

Now the new $(V_1-V_2)$ is not perpendicular to $(X_1-X_2)$. So you add a constant multiple of $(X_1 - X_2)$ to $V_1$ and subtract the same vetor quantity from $V_2$.

$$V_1 \rightarrow V_1 + C(X_1-X_2)$$ $$V_2 \rightarrow V_2 - C(X_1-X_2)$$

so that the new $(V_1-V_2)$ is shifted by $2C(X_1-X_2)$. The constant C is determined the make the new $(V_1-V_2)$ perpendicular to $(X_1-X_2)$

$$(V_1 - V_2)\cdot (X_1-X_2) + 2C (X_1 - X_2)\cdot(X_1-X_2) = 0$$

$$C = - { \Delta V \cdot \Delta X \over ||\Delta X||^2 }$$

This will update things without hassle. If you are using an improved algorithm (for example, update naively, recalculate the forces, and average with the original force), you do this first for the naive update, then you calculate the new forces at this new position, and you average the forces, and repeat the update and subtraction.

-

If you can simulate pendulums, you could make Newton's cradle.

-
This is very easy in a force simulation--- you don't even need pendulums, since there is no gravity, just have balls touching each other, and have a ball come in for a collision. It will be a sequence of momentum-transferring elastic collisions, which end with the final ball moving. Also this isn't an answer, but a comment. –  Ron Maimon May 21 '12 at 6:23