Skip to content
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

Merged
merged 1 commit into from
Dec 22, 2023

Conversation

golfinq
Copy link
Contributor

@golfinq golfinq commented Oct 20, 2023

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.

@golfinq golfinq requested a review from a team as a code owner October 20, 2023 03:45
@@ -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;
Copy link
Member

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.

Copy link
Contributor Author

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;

Copy link
Contributor Author

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;

Copy link
Member

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.

Copy link
Contributor Author

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

@golfinq golfinq force-pushed the small-capsule-shape-fix branch from 20fd3e2 to e03a65f Compare October 20, 2023 22:08
Copy link
Member

@aaronfranke aaronfranke left a 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.

@YuriSizov YuriSizov modified the milestones: 4.x, 4.3 Dec 22, 2023
@YuriSizov YuriSizov merged commit 6b40371 into godotengine:master Dec 22, 2023
15 checks passed
@YuriSizov
Copy link
Contributor

Thanks!

@akien-mga akien-mga changed the title Replace confusing code in GodotCapsuleShape2D::get_supports Replace confusing code in GodotCapsuleShape2D::get_supports Jan 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants