+3 votes

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.

in Engine by (24 points)

2 Answers

+1 vote

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.

by (262 points)
edited by

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 ...

0 votes

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.

by (7,866 points)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.