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

Reworked Segment types into their cartesian forms #17404

Open
wants to merge 39 commits into
base: main
Choose a base branch
from

Conversation

Sigma-dev
Copy link
Contributor

Objective

Segment2d and Segment3d are currently hard to work with because unlike many other primary shapes, they are bound to the origin.
The objective of this PR is to allow these segments to exist anywhere in cartesian space, making them much more useful in a variety of contexts.

Solution

Reworking the existing segment type's internal fields and methods to allow them to exist anywhere in cartesian space.
I have done both reworks for 2d and 3d segments but I was unsure if I should just have it all here or not so feel free to tell me how I should proceed, for now I have only pushed Segment2d changes.

As I am not a very seasoned contributor, this first implementation is very likely sloppy and will need some additional work from my end, I am open to all criticisms and willing to work to get this to bevy's standards.

Testing

I am not very familiar with the standards of testing. Of course my changes had to pass the thorough existing tests for primitive shapes.
I also checked the gizmo 2d shapes intersection example and everything looked fine.

I did add a few utility methods to the types that have no tests yet. I am willing to implement some if it is deemed necessary

Migration Guide

The segment type constructors changed so if someone previously created a Segment2d with a direction and length they would now need to use the from_direction constructor

@Jondolf Jondolf self-requested a review January 16, 2025 12:30
@Jondolf Jondolf added C-Usability A targeted quality-of-life change that makes Bevy easier to use A-Math Fundamental domain-agnostic mathematical operations M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jan 16, 2025
@IQuick143 IQuick143 self-requested a review January 16, 2025 12:41
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good so far. but I'd prefer to do the 3D segment changes in the same PR. This is straightforward to review and I really don't want to ship one without the other.

@alice-i-cecile alice-i-cecile added the X-Uncontroversial This work is generally agreed upon label Jan 16, 2025
crates/bevy_gizmos/src/primitives/dim3.rs Outdated Show resolved Hide resolved
crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs Outdated Show resolved Hide resolved
Comment on lines +332 to +333
let segment = Segment2d::new(point1, point2).centered();
let center = (point1 + point2) / 2.;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this centering the line segment at the origin and then translating it back to the original position? I'm pretty sure this should just compute the bounding circle of the actual line segment, passing in the isometry, but not centering anything or applying stuff like Isometry2d::from_translation(center).

Copy link
Contributor Author

@Sigma-dev Sigma-dev Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried the simple approach but it failed in the example and the tests.
I believe it has to do with the old implementation automatically "centering" the segment even if the given points were not centered, but the center returned from from_points was not centered itself and was just the middle of the given points (if any of this makes sense).
This weird behavior is what caused me to do this weird code to reproduce the original behavior as close as I could.

This could be a case of something being messed up somewhere else, or just me not understanding the math properly or something else idk.

crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs Outdated Show resolved Hide resolved
@@ -359,22 +359,25 @@ impl Primitive3d for Line3d {}
reflect(Serialize, Deserialize)
)]
pub struct Segment3d {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I won't copy all of my comments from the 2D version, but most of them also apply here in 3D.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the things you havn't specificially mentionned for the Segment3d, I have done the translation and rotation changes, tell me if I missed anything else

crates/bevy_math/src/primitives/dim3.rs Outdated Show resolved Hide resolved
crates/bevy_math/src/primitives/dim3.rs Outdated Show resolved Hide resolved
examples/2d/bounding_2d.rs Outdated Show resolved Hide resolved
crates/bevy_math/src/primitives/dim2.rs Outdated Show resolved Hide resolved
@Sigma-dev
Copy link
Contributor Author

Sigma-dev commented Jan 17, 2025

If I didn't miss anything (unlikely) I should have addressed everything except the stuff I left questions about.
I'm open to any new required changes as well of course

@alice-i-cecile alice-i-cecile added this to the 0.16 milestone Jan 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Math Fundamental domain-agnostic mathematical operations C-Usability A targeted quality-of-life change that makes Bevy easier to use M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Needs-Review Needs reviewer attention (from anyone!) to move forward X-Uncontroversial This work is generally agreed upon
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants