-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace confusing code in GodotCapsuleShape2D::get_supports
#83655
Conversation
@@ -373,8 +373,7 @@ void GodotCapsuleShape2D::get_supports(const Vector2 &p_normal, Vector2 *r_suppo | |||
if (h > 0 && Math::abs(n.x) > segment_is_valid_support_threshold) { | |||
// make it flat | |||
n.y = 0.0; | |||
n.normalize(); | |||
n *= radius; | |||
n.x = radius; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would change the behavior if n.x
used to be negative. Is that a valid case that can happen?
I agree that the old code is bad though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, the fix would be
n.x = Math::sign(n.x) * radius;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or
n.x = n.x > 0 ? radius : -radius;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The top makes more sense to me. Also, I think it preserves the behavior when n.x
is zero.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wrongly assumed there was a Math::sign
its a macro which is SIGN
still changed it to use it
20fd3e2
to
e03a65f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have not tested running the code, but the behavior should be identical to the old code, but more straightforward and more performant. With the old code, normalizing a vector with Y equal to 0 will result in X being either -1, 0, or 1, which is identical to SIGN.
Thanks! |
GodotCapsuleShape2D::get_supports
I am doing some bug hunting and found a place where the code is unnecessarily complicated. Its not a big change, but it helps with understanding what this function is doing when reading it.