I have an array of KinematicBody2D nodes that I use as bullets in my game. Whenever a bullet expires or collides, it is returned to the cache roughly like this:
playfield.remove_child(bullet_instance)
bullet_array.push_back(bullet_instance)
Bullets are spawned again roughly like this:
new_bullet = bullet_array.pop_back()
new_bullet.position = spawn_position
new_bullet.initialize()
playfield.add_child(new_bullet)
Whenever I spawn a once used bullet, it sometimes gets killed by the same PhysicsBody2D that killed it originally - even though it has a completely new position. It's as if the physics engine still considers it to be in the same position it died previously.
I've tried printing the position of the bullet when it dies and it is nowhere near the PhysicsBody2D that supposedly collided with it.
Am I missing some best practise that should be followed when caching physics objects?