This project has moved. For the latest updates, please go here.

Violent Jitter with Great Forces

Topics: Developer Forum, User Forum
Apr 21, 2009 at 2:45 AM
I am toying around with dynamic gravity to simulate magnetism. I have a very simple algorithm for applying forces to each body:

protected override void Update(GameTime gameTime)
    foreach (Magnet magnet in magnets)

        foreach (Magnet other in magnets)
            if (magnet == other)

            float distance = Vector2.DistanceSquared(magnet.body.Position, other.body.Position);
            Vector2 force = other.body.Position - magnet.body.Position;
            force *= ((magnet.magnetism + other.magnetism) / (distance));  // attractive by default


    physics.Update(gameTime.ElapsedGameTime.Milliseconds * 0.001f);


This works well for magnetism values 100000f or under, but isn't very exciting to watch for even relatively close objects.  For values 1000000f or larger, the magnets will begin to collide violently, they start to rotate, and eventually enter an orbit with one another (cool but undesirable). Currently I am using circle geometries with 64 sides. A friction coef of 0 and a restitution coef of 0.  I am not entirely sure how this rotational force is being applied to the bodies other than by errors in finding the tangent to the point of contact since they are not perfect circles.

In a perfect world I would like for the circles to eventually settle in a resting contact even with very great forces. Has anyone else run into this or have any ideas?