Magnetic force and torque for 2D neodymium ball simulator I'm trying to write a simulator for neoballs. I'm doing only 2D for a start, in which each ball has a magnetic moment, m, that is in the x-y plane.
The simulator will use time-step integration at 60 fps, and typical collision detection stuff. For the magnetism I intend to compute force and torque between each pair of balls on every frame.
I think it will be ok to approximate each little ball as a "point dipole". What is the formula for force and torque?
For the force I have this from wikipedia
${\displaystyle \mathbf {F} (\mathbf {r} ,\mathbf {m} _{1},\mathbf {m} _{2})={\frac {3\mu _{0}}{4\pi r^{5}}}\left[(\mathbf {m} _{1}\cdot \mathbf {r} )\mathbf {m} _{2}+(\mathbf {m} _{2}\cdot \mathbf {r} )\mathbf {m} _{1}+(\mathbf {m} _{1}\cdot \mathbf {m} _{2})\mathbf {r} -{\frac {5(\mathbf {m} _{1}\cdot \mathbf {r} )(\mathbf {m} _{2}\cdot \mathbf {r} )}{r^{2}}}\mathbf {r} \right]}$
But I think it gives the wrong answer. No matter what the direction of $\mathbf{m} _{1}$ or $\mathbf{m} _{2}$, I think the force must be always parallel to r to conserve angular momentum. However, suppose $\mathbf{m} _{1}$ is parallel to r and $\mathbf{m} _{2}$ is perpendicular: 

All terms except the first vanish, and F is in the direction $\mathbf{m} _{2}$, which is not possible? Common sense tells me F should be zero in this case! What am I doing wrong?
Also I need a formula for the torque. I know $ \mathbf{\tau=m\times B } $, and on the same wiki page I got
${\displaystyle \mathbf {B} (\mathbf {m} ,\mathbf {r} )={\frac {\mu _{0}}{4\pi r^{3}}}\left(3(\mathbf {m} \cdot {\hat {\mathbf {r} }}){\hat {\mathbf {r} }}-\mathbf {m} \right)}$
(I dropped the dirac term, because I don't need it).
I haven't tried it yet, but I think I can compute the the torque around the center of mass of $\mathbf{m} _{2}$ with $ \mathbf{\tau=m _{2}\times B(m _{1}, r) } $ where r is position of $\mathbf{m} _{2}$ relative to $\mathbf{m} _{1}$. Since all the balls have identical mass and magnetism, the torque on $\mathbf{m} _{1}$ is the same but with opposite sign.
Does that look reasonable?
EDIT: the above force formula seems to be rubbish, but the flux density(B) is reasonable. I'm wondering if I can just use  $ \mathbf{\tau=m\times B } $ for torque and  $ \mathbf{F=(m\cdot B)\hat {\mathbf {r} } } $ for force. It has a nice symmetry, and gives good results in the simulation.
EDIT2 Another curious contradiction. According to this wiki page, the potential energy of a magnetic field is given by:
${\displaystyle H=-{\frac {\mu _{0}}{4\pi |{\mathbf {r}}|^{3}}}\left(3({\mathbf {m}}_{1}\cdot {\hat {\mathbf {r}}})({\mathbf {m}}_{2}\cdot {\hat {\mathbf {r}}})-{\mathbf {m}}_{1}\cdot {\mathbf {m}}_{2}\right)}$
And this implies as long as m2 is perpendicular to m1 and  r, the potential energy is ZERO. Since force is the gradient of potential over space, then the force must be also zero in that case. What am I misunderstanding?
 A: 
But I think it gives the wrong answer. No matter what the direction of $\mathbf{m} _{1}$ or $\mathbf{m} _{2}$, I think the force must be always parallel to r to conserve angular momentum.

The force acting on magnetic moment due to another magnetic moment is not always parallel to the line that connects them. This does not violate conservation of angular momentum though. Although the above formulae suggest the magnets are points so that only orbital angular momentum is relevant, the magnets in fact have rotational angular momentum as well.
When the magnets move due to mutual force, orbital angular momentum of magnets (treated as moving points) changes, but the magnets also can rotate so rotational angular momentum of the magnets may change as well. 
Since the system is equivalent to system of electric dipoles and can be described via Coulombic forces, it conserves both energy and angular moment. When total angular momentum - sum of orbital and rotational contributions - is calculated, the result should not change in time, if the motions are quasistatic.

This will create spontaneous spin. It would not conserve the angular momentum of the system. All you would need to do is put a wooden rod between them and voila! perpetual motion machine!!

That's a good puzzle. The resolution is that the orbital spin of the system, if it is allowed to occur, is compensated by opposite rotational spin of the individual magnetic moments. If there is a rod connecting centers of the magnets while allowing them to rotate around the pivots on both ends of the rod, the rod will alternate between clockwise and counterclockwise motion, while the magnets will be doing the same in the opposite sense, so total angular momentum will be conserved.
If the rod and the magnets are cemented into a rigid body so no motion of magnets relative to the rod is allowed, no motion of the system will occur at all; the rod will experience mechanical forces of shear and will act back on the magnets, cancelling the EM forces that tend to move them (see also picture).

