See the related Godot Engine issue. This code can be used to reproduce that scenario.
⚠ That issue has since been closed. It was deemed "user error" and "wrong expectations [from the developer]", not a bug or missing documentation or tooling. Because I feel others might run into these same gotchas as I did, I will leave up this repository. Before the info on how to reproduce the situation, let me start with what I found out and how I worked around it.
Note that this repository is provided "as-is" and will most likely not receive any (security) updates.
The root cause of the bug summarized from the more extensive reply is (paraphrased):
Combining
ControlandNode2Dnodes can give the expectation of them not having special interaction, but sometimes they might break when combined.
In short: the advice there and from the other Contributor is to not mix them at all.
Later I found that there's a Control node called SubViewportContainer which can hold a SubViewport which seems meant for my scenario:
layout with Control notes, and part of that layout dedicated to 2D physics.
You can check my other sample repository to see how that would work.
Steps:
- Clone this repository
- Enable debug options to see collisions and shapes
- Run it in Godot 4 (I used 4.0 on Windows 10)
- Result: ball slides into the polygon
- Expected: ball slightly bouncing on the polygon
Variant: reset the transform Scale.x on CharacterBody2D to 1.
- Result: ball bounces off the polygon super fast
- Expected: ball only slightly bouncing on the polygon
Videos of the unexpected and expected results:
- ❌ repro-godot-bounce-bug-unexpected-result.mp4
- ❌ repro-godot-bounce-bug-unexpected-result-variant.mp4
- ✔ repro-godot-bounce-bug-expected-result.mp4
Or, for as long as the links work (since the raw videos were uploaded via Godot's issues, which may get archived):
Video of the ❌ unexpected result with the base repro:
repro-godot-bounce-bug-unexpected-result.mp4
Video of the ❌ unexpected variant with non-flipped CharacterBody2D polygon:
repro-godot-bounce-bug-expected-result-variant.mp4
Video of the expected result (after removing ColorRect from the aforementioned tree):
repro-godot-bounce-bug-expected-result.mp4
I'm learning Godot at the time of writing so don't take all this as expert advice. Still, I'm hoping it may help someone.