# Glitch or Mistake: Rigidbody2D's Spawning/Rendering Twice

4

### Question

I’ve created a 2D simulation for 2 RigidBody2D (planets) to be attracted to each other by gravity, but when the program is run, 4 planets spawn instead of two. They’re identical clones of planet1 and planet2 respectively, and they interact with each other with gravity. How do I remedy this?

here’s the planet1’s code (which is replicated on planet 2 but switched node paths)

``````extends RigidBody2D

const GRAVITATIONAL_CONSTANT: float = 6.674 * pow(10,-11)
const SCALE_FACTOR: float = 1000
var scaled_gravitational_constant: float = GRAVITATIONAL_CONSTANT * SCALE_FACTOR

var force
var distance = 0
var gravdir
var velocity = Vector2.ZERO
var gravity
custom_integrator = true

func _integrate_forces(state):
set_mass(5190000000)
print(distance)
print(force)
print(gravdir)
print(gravity)
distance = global_position.distance_to(\$"../planet2".global_position)
gravdir = (\$"../planet2".global_position - global_position).normalized()

force = scaled_gravitational_constant * ((get_mass() * \$"../planet2".get_mass()) / (distance * distance))

gravity = gravdir * force

#state.apply_central_force(gravity)

func _process(delta):
if force == null:
force = Vector2.ZERO
else:
#velocity += gravity * delta
apply_central_impulse(gravity)
pass

``````

and the main.gd script

``````extends Node2D