Repository: godot
Issue: Strange behavior of RigidBody2D instances and PackedScene
Labels: bug,topic:physics,needs testing

### Tested versions

Godot 4.0, 4.1.1, 4.2, Godot v4.3.stable -- Issue 1 occurs, Issue 2 occurs
Godot 4.4-dev3 through 4.4-dev7 -- Issue 1 occurs, **Issue 2 does not occur**
Godot  4.4-dev2 -- Issue 1 occurs, Issue 2 occurs
(I could not find where, but the fix for Issue 2 seems to be somewhere in 4.4-dev3.)

### System information

Godot v4.3.stable - Windows 10.0.22631 - GLES3 (Compatibility) - NVIDIA GeForce RTX 4070 Ti (NVIDIA; 32.0.15.6614) - AMD Ryzen 9 7950X 16-Core Processor (32 Threads)

### Issue description

Possibly related to:
https://github.com/godotengine/godot/issues/45886

Other references:
https://forum.godotengine.org/t/how-do-i-save-a-node-and-its-children/1276/3
https://docs.godotengine.org/en/4.3/classes/class_packedscene.html

I've been having some issues in my project with RigidBody2D that are generated by code and added at runtime.
Some issues are likely due to my own incompetence.
Possibly might need to split this into 2 issues, but we'll see.


**Issue 1: RigidBodies moving on their own; strange gravity behavior**

I've noticed this with the default physics engine; however since I switched to Rapier2D I've kind of ignored it since the issue went away.
RigidBody2D that I create from code always have weird behavior, and I have no idea why.
They move around randomly, and seem to rotate/move in weird ways, despite nothing happening to them.
I thought maybe it was due to setting the positions, but I've tried not setting the positions at all and the same thing occurred.
Maybe it is a simple fix?
See example below in the MRP.




**Issue 2: PackedScenes instanced from the PolyShader class have duplicate child polygons.**



### Steps to reproduce

Open the MRP.


**Issue 1: RigidBodies moving on their own; strange gravity behavior**

Run the project and click the "Make" button. This will spawn some cubes.
Click the button 3-6 times, creating a tower of cubes.
Observe that eventually, the cube tower will seem to topple on its own.
Even if the cubes are stacked perfect, they will start rotating to the left and fall over.
They will also continue rotating to the left as if being pulled by some leftwards, rotating gravity; rather than resting flat on the ground.
Some may even rotate back the other way and rest like a coin that somehow landed perfectly on its side.


**Issue 2: PackedScenes instanced from the PolyShader class have duplicate child polygons.**

Click the Reset button to reset the scene.
Enable the PolyShader class by clicking the CheckButton.
Click Make. This does the same thing as before, except a custom class is used to generate Polygon2D nodes to color the object, instead of using the visible collision shapes property.
Observe the remote scene tree (under dynamic_level node.) Two Polygon2D nodes are created (one is hidden.)
Click Save Last to save the last object that was made as a packed scene under res://saves/test.tscn
Click Load to create a new instance of the scene
Notice that the color of the new instance is different (brighter.)
Go to the remote scene tree and observe that the polygons were duplicated (4 polygons instead of 2.)
(You can also try disabling PolyShader and save/load objects; and notice that no nodes are duplicated.)



Bonus:

Go to the asset store.
Install Rapier Physics 2D - Fast Version with Parallel SIMD Solver.
Restart the project.
Go to Project Settings > Physics > 2D > Physics Engine and select Rapier2D.
Save and restart the project.
Follow the steps again for Issue 1. Notice that Issue 1 is fixed (boxes no longer topple over themselves or move weirdly.)
(Issue 2 still occurs as before.)

### Minimal reproduction project (MRP)

[packed_scene_duplicate_test.zip](https://github.com/user-attachments/files/18216352/packed_scene_duplicate_test.zip)