Simulate magnets and magnetism in the Blender Game Engine


For some time now, I tried to simulate the behavior of magnets in the “Blender Game Engine”.
This is one of the key systems of my game and I really need it.

I wish that if two cubes have a property “+”, (or if both cubes have a property “-”), they repel each other but if one of the two cubes has a property “-” and the other a property "+ ", they attract.

I tried a lot of things with python but I can not do it.

Could you help me ?

Thanks in advance, Lavistios.

Hi Lavistios

i try some code on UPBE 0.20 (for learning and testing)

As it seem dangerous to name property with operator symbol (+ - * …)
only object with property “p” are concerned by force interaction

p=1 for red Cube
p=-1 for Blue Cube
if object1 [“p”] * object2 [“p”] == 1 => object reject
if object1 [“p”] * object2 [“p”] == -1 => object attract
BUT with 3 or more objects I calculate the “resulting” force off all other “p objects” on the current object.

To exclude one cube from the interaction rename “p” property by another name ie “pp”

P tu run
see text editor for


champ.blend (514 KB)

Hi Lavistios

A more realistic magnet simulation : a real magnet has 2 poles.

I try to create a link between a Red Cube and a Blue one. ( by the n parameter)
Parenting give unstable effect
too large force mask interaction with other magnet
a real rigid object (Red + Blue cubes) need torque and gravity center calculation.

I let the rough code for experimentation. (text editor :

It’s not perfect but a more accurate simulation need more calculation (torque, local forces …)

tested on UPBGE 0.20 with game engine

P to run

PJ : magnet.blend

magnet.blend (519 KB)