Two RigidBodies attracting each other with Area nodes?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By spiderbyte87

I’m trying to get two RigidBodies with Area node gravity to attract each other without issues. So far no matter how I set the area nodes, I either get one attracting the other, a standstill, or one or both vibrating/wobbling. I’ve tried messing with override, priority, gravity, mass, etc. to no avail.

I need to instance a bunch of these so I’d like to let the engine handle the physics if possible. Also, if possible, I’d like to have one of the RigidBodies be a controllable player.

Thoughts?

:bust_in_silhouette: Reply From: kidscancode

This should work pretty much “out of the box” without any code. I just tried it out and it works fine with this setup:

  • RigidBody2D
    • Sprite
    • CollisionShape2D (circle)
    • Area2D
      • CollisionShape2D (circle)

I set the global gravity to 0 in the Project Settings. I changed nothing on the RigidBody2D, except to set its linear/angular damp to 0, so the body won’t experience drag.

On the Area2D I set the following:

Space Override: Combine
Gravity Point: On
Gravity Vec: (0, 0)

Adjust gravity strength and body mass as desired. Here’s an example with two bodies, each with a random initial linear_velocity:

enter image description here

Can you add all the code used Please? I am new to godot and cant seem to get something like this working (attracting enemies to player using point gravity.)

Elliot__C | 2021-02-26 07:48

There is no code in this example. I only set the node properties as described.

kidscancode | 2021-02-26 16:35

Thanks, it’s all good I figured it out.

Elliot__C | 2021-02-26 21:23