# adding magnetic behavior to a KinenematicBody2D

Attention Topic was automatically imported from the old Question2Answer platform.

Is it possible to give magnetic properties to a KinematicBody2D?
I have some balls to attract each other.
Is there any code examples?
I would need a little tip on how to proceed.
I feel like I’ve read the whole internet.
If there is a very simple solution, I’ll hit my head against the wall

Area2D can influence gravity in its region. However, that only works with physics, i.e. RigidBody2D.

If you’re using two KinematicBody2D nodes, just move them towards each other. Use the standard Equations of Motion.

Here’s a quick example:

``````extends KinematicBody2D

var acceleration = Vector2()
var velocity = Vector2()
var attract_force = 100

func _physics_process(delta):
acceleration = Vector2()
for other in get_tree().get_nodes_in_group("magnets"):
if other != self:
var dir = (other.position - position).normalized()
acceleration += dir * attract_force

velocity += acceleration * delta
velocity = move_and_slide(velocity)
``````

Result:

Hello OsOgar,

Don’t make promises about banging your head against the well, especially in topics related to programming :p…

Let’s just go from a basic principle : in real world physics, the gravitational force between two objects is Gm1m2/d². The most important part of this is d² : the force is decreased by the inverse square of the distance. It means that for most objects you’d use in game, the gravitational force is negligible beyond a certain not-so-big distance, and since we’re dealing with limited resources, we will be able to confidently use an Area2D with a circle CollisionShape2D : the results shouldn’t look weird at all.

The method from Area2D that is relevant to your case is “get_overlapping_bodies()”. It will allow you to add forces to bodies that are near. I would advise you to add a “add_central_force” method in your KinematicBody2D. For any number of balls, the solution I would use would be the following :

Assuming the tree looks like this (notice the bigger “GravityRadius” CollisionShape2D)

Ball.gd

``````extends KinematicBody2D

var currentTotalForce : Vector2 = Vector2(0,0)
const gravityStrength : float = 10000.0#Works fine, but you can tune it

func _physics_process(delta):
for i in \$Area2D.get_overlapping_bodies():
if i != self:
var dist : float = (i.get_position() - self.get_position()).length()
var attractStrength : float = gravityStrength / (dist*dist)
var attractDirection : Vector2 = (self.get_position() - i.get_position()).normalized()