6
\$\begingroup\$

How should I go about implementing "natural" magnet on a certain body in chipmunk space? Context is of simple bodies lying in the space (think chessboard). When one of the figures is activated as a magnet - others should start moving towards it.

Currently I'm applying force (cpBodyApplyForce)to the other figures with vector calculated towards the activated figure. However this doesn't really feel "natural". Are there any known algorithms for imitating magnets?

\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

It's been a while since I did magnets, but I believe that the equation you want is the naive model of the force between two poles.

def force(a, b): magnitude = a.magnitude * b.magnitude separation_sq = distance_squared(a.position, b.position) return (permeability * magnitude) / (4 * pi * separation_sq) 

For your purposes, you can probably fix permeability at 4 * pi and tweak only the per-object magnitudes to get the feel you want, simplifying the equation:

def force(a, b): magnitude = a.magnitude * b.magnitude separation_sq = distance_squared(a.position, b.position) return magnitude / separation_sq 

The important part is that the force varies inversely with the square of the distance between the objects.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.