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

Add new_and_length method to Direction2d and Direction3d #11172

Merged

Conversation

Jondolf
Copy link
Contributor

@Jondolf Jondolf commented Jan 1, 2024

Objective

When creating a normalized direction from a vector, it can be useful to get both the direction and the original length of the vector.

This came up when I was recreating some Parry APIs using bevy_math, and doing it manually is quite painful. Nalgebra calls this method Unit::try_new_and_get.

Solution

Add a new_and_length method to Direction2d and Direction3d.

Usage:

if let Ok((direction, length)) = Direction2d::new_and_length(Vec2::X * 10.0) {
    assert_eq!(direction, Vec2::X);
    assert_eq!(length, 10.0);
}

I'm open to different names, couldn't come up with a perfectly clear one that isn't too long. My reasoning with the current name is that it's like using new and calling length on the original vector.

@Jondolf Jondolf added C-Usability A targeted quality-of-life change that makes Bevy easier to use A-Math Fundamental domain-agnostic mathematical operations labels Jan 1, 2024
@mockersf
Copy link
Member

mockersf commented Jan 1, 2024

wouldn't it make more sense to add those to glam Vec2 and Vec3?

@Jondolf
Copy link
Contributor Author

Jondolf commented Jan 1, 2024

I'd say that it should exist for both. The cases where I currently need this use these direction types, and it'd be annoying and confusing to have to do

let (dir, len) = vector.normalize_and_get_length();
let dir = Direction2d::from_normalized(dir);

But yeah, it'd be nice if Glam also had something similar, I could probably make a PR for that too, even though I don't currently have a need for it.

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.

In favor of merging this now, and then migrating over to the glam version if and when that ships.

@rparrett rparrett added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Jan 7, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jan 8, 2024
Merged via the queue into bevyengine:main with commit bcbb7bb Jan 8, 2024
23 checks passed
@Jondolf Jondolf deleted the direction-construct-with-length branch January 9, 2024 15:55
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 S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants