# [ODE] Simulating Magnetism...

Chunky Kibbles chunky at icculus.org
Tue Jan 11 16:01:54 MST 2005

```On Tue, Jan 11, 2005 at 08:31:55PM +0000, Dominique Louis wrote:
> Has anyone here played the game Katamari Damacy (
> http://www.namco.com/games/katamari_damacy/ ). Anyway basically I was
> wondering if it would be possible to use ODE to simulate a large
> spherical magnet.
>
> How would others suggest I attach other objects to this magnet at
> runtime. Would creating a certain type of joint for each new object, be
> the way to go?

Well, attaching items you could do with fixed joints.

"Simulating Magnetism"... well, magnets obey an inverse square, right?

const dReal *pos1, *pos2;
const dReal direction[3];
dReal DistBetweenBodiesSquared;

pos1 = dBodyGetPosition(body1);
pos2 = dBodyGetPosition(body2);

direction[0] = pos1[0] - pos2[0];
direction[1] = pos1[1] - pos2[1];
direction[2] = pos1[2] - pos2[2];

DistBetweenBodiesSquared = abs(
pow(direction[0], 2) +
pow(direction[1], 2) +
pow(direction[2], 2)));

dBodyAddForce(body1, -MAGNET_STRNGTH * direction[0] / DistBetweenBodiesSquared,
-MAGNET_STRNGTH * direction[1] / DistBetweenBodiesSquared,
-MAGNET_STRNGTH * direction[2] / DistBetweenBodiesSquared);
dBodyAddForce(body2, MAGNET_STRNGTH * direction[0] / DistBetweenBodiesSquared,
MAGNET_STRNGTH * direction[1] / DistBetweenBodiesSquared,
MAGNET_STRNGTH * direction[2] / DistBetweenBodiesSquared);

There's probably some problems with that [I expect I messed up signage -
taking out the abs will probably fix it.]

I've never played Katamari Damarcy, so I don't know if it really is a
magnet, or if you just want a stack of fixed joints.

In practice, though, it may serve you to not attach bodies at all, but
instead have the working ball as just a spherical dGeom whose size you
can increase. No joints means much better performance.

Gary (-;
```