Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.