Simulation of magnets in Godot.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Lavistios
:warning: Old Version Published before Godot 3 was released.

Hello,

For a while now, I have not stopped changing the game engine for my game. Only the problem remains the same: I can not simulate magnets and their properties in Godot. One of the key elements of my game

In addition, I discovered the game engine and I’m not yet used to GDscript. So, if anyone can help me create a system that:

  1. As soon as two objects have the property “+” (or as soon as two objects have the property “-”), they repel each other (like real magnets).

  2. As soon as one of the two objects has a property “-” (Object A: “+” | Object B: “-”), they attract each other. (Still like real magnets).

If this is impossible, please tell me.

If you know the solution, can you explain it clearly? I’m trying to learn and become a game developer ^ ^

THANK YOU MUCH in advance.

:bust_in_silhouette: Reply From: Artium Nihamkin

See answer here that deals with applying custom forces to rigid bodies.

Inside _integrate_forces callback, you will have to iterate over all you magnets and calculate the cumulative force’s direction and magnitude. You can use the following formula:

Magnitude: F =(-1) * (C*Q1*Q2) / (Distance*Distance)
Direction: Vector from magnet 1 to magnet 2.

Play with the C constant until the results look natural. Q represents how strong the magnet is and can be also negative. Notice that this is not how real magnets work but this is what you described in the answer. Real magnets have poles and are non uniform. This resembles electrostatic force.

You will have to iterate over all “magnets” which requires you to identify which nodes are magnets. One way to achieve this is to put all your magnet nodes under the same father node.

Hmm …
it looks a lot too complicated for me. Thank you so much for your answer but I will aim less and start another project. Sorry for making you waste your time …

Lavistios | 2017-10-26 13:14

:bust_in_silhouette: Reply From: eons

If you want to rely on the engine physics (which is usually not recommended), could be possible to make a set of bodies with areas, on different masks and layers for + and - .

Positive charged bodies affected by one type of gravity areas and rejected by another, all using area space override.

Like:

Charge (body, pos/neg)
|-Rejection zone (Area)
|-Attraction zone (Area)

Charged bodies are on a specific layer.
Attraction zone affects bodies on the opposite charge-layer with a positive gravity.
Rejection zone affects bodies on the same charge-layer with a negative gravity.
Zones overlap, and need to exclude the parent body.


But if you are not used to the engine I suggest you to start with something more simple, not dealing with physics yet until you understand the engine features and feel confident to try complex setups.

Yes, I will definitely do that. Start with a simpler project. thank you for your reply

Lavistios | 2017-10-26 13:15