-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
[Merged by Bors] - Consistently use PI
to specify angles in examples.
#5825
Conversation
examples/3d/lighting.rs
Outdated
@@ -34,7 +36,7 @@ fn setup( | |||
|
|||
// left wall | |||
let mut transform = Transform::from_xyz(2.5, 2.5, 0.0); | |||
transform.rotate_z(std::f32::consts::FRAC_PI_2); | |||
transform.rotate_z(TAU / 4.); |
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 has less accuracy than FRAC_PI_2.
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 don't think the loss of precision is relevant to any of our current examples,
but I wanted to investigate the difference in precision so i tried this:
assert_eq!(
std::f64::consts::TAU / 4.,
std::f64::consts::FRAC_PI_2
);
This assertion does not fail however. Is this test too naive?
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'd compare the to_bits
outcome to be safe
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.
Same result with to_bits
.
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.
So in theory I think this makes sense, however there are a few things which make me hesitant. In my experience, education about how to think about radians is all in terms of Pi, not Tau. Therefore, this runs into issues of causing challenges for those with an intuition for radians in the standard forms.
Basically, none of this code is self describing to an audience without a formal education in relatively advanced maths, and familiarity with Tau. I think this is unnecessary. There are two paths forward I can see.
One is to recognise that the name Tau
is not at all evocative of its meaning. Therefore, we can add const FULL_ROTATION_RADIANS: f64 = TAU;
in bevy_utils
. This has the issue of being a non-standard constant, but the name should be descriptive enough to make reasoning about it much easier.
The second is to give up on radians entirely, trust llvm to optimise things properly and use degrees, converting as needed. The main issue I can see there is that there is (currently 👀) no type system support for that, so it's potentially easy to accidentally pass e.g. 180 radians to a function, causing very weird bugs.
examples/2d/mesh2d_manual.rs
Outdated
let a = std::f32::consts::FRAC_PI_2 - i as f32 * std::f32::consts::TAU / 10.0; | ||
// Radius of internal vertices (2, 4, 6, 8, 10) is 100, it's 200 for external | ||
// The angle between each vertex is 1/10 of a full rotation. | ||
let a = i as f32 * TAU / 10.0; |
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'd be tempted to use degrees here, and then convert to radians as needed.
examples/3d/lighting.rs
Outdated
@@ -34,7 +36,7 @@ fn setup( | |||
|
|||
// left wall | |||
let mut transform = Transform::from_xyz(2.5, 2.5, 0.0); | |||
transform.rotate_z(std::f32::consts::FRAC_PI_2); | |||
transform.rotate_z(TAU / 4.); |
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'd compare the to_bits
outcome to be safe
I think TAU is a lot easier to understand than PI once you get the hang of it. And almost all game math libraries use radians so I don't really think it makes sense to use degrees anywhere. |
This makes TAU seem like something tricky when it is rather straightforward. And for people new to radians altogether. Teaching that TAU is the same as 360 degrees is more palatable than PI being 180 degrees and having to think in half-rotations. Reading
Fair. Can't understand what you don't know, and schools don't seem to teach it.
Neither is PI, but of course more people already know PI.
😅 kind of difficult when every math lib uses radians. Trust me. I'd replace everything with degrees if I could, hah. TL;DR Using degrees would be nice but since we need to use radians in the end it introduces verbose conversions in the middle of the actual math. I'm going to try to use |
Thats the problem. Examples are intended to be minimal / quick / easy to understand / etc and are often peoples' first exposure to the engine. The percentage of programmers that know what PI represents off the top of their head is probably close to 98%. The percentage of programmers that know what TAU represents off the top of their heard is probably closer to 5%. It doesn't matter that it is a "simple" concept (or marginally more "natural" once you understand it). The vast majority of people think about radians in terms of PI. Using TAU in our examples adds one more (unnecessary) learning hurdle to the Bevy onboarding process, which is already full of necessary learning hurdles. "Optimizing" this part of peoples' workflows doesn't seem worth the price of admission, especially in this part of our learning material. |
I don't have an opinion about the TAU/PI debate, but I appreciated all the little bits of incidental cleanup in this PR, especially importing (your favorite constant). I checked a few of the examples where the changes were less trivial and they checked out. |
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.
Besides the TAU vs PI discussion, I think the reworks is pretty good.
I'm in favor of TAU
, since I think it's easier to understand, but the majority of newcomers won't, most of them still struggle to understand radians vs degree, so maybe we should add on bevy_utils
or bevy_math
some consts like DEGREE_360
, DEGREE_180
, DEGREE_90
, DEGREE_45
and accept that examples will be better written in degrees, since anyone who knows PI
or TAU
will understand what is a transform.rotate_z(DEGREE_90)
You're right. The most important thing is the examples being easy to start out with.
PS: A lot of the discussion around PI/TAU is extremely cringe. |
TAU
to specify angles in examples.PI
to specify angles in examples.
1dc55c2
to
b04a1b6
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.
This looks good to me. My last holdout was the precision of PI / 2.
and PI / 4.
vs FRAC_PI_2
and FRAC_PI_4
. (a previous comment already showed this wasn't a problem for TAU, but that doesn't necessarily make it true for PI).
They are equal!
Under those circumstances, I agree with the choice to use the one PI
constant for this. Simpler and more flexible.
bors r+ |
Examples inconsistently use either `TAU`, `PI`, `FRAC_PI_2` or `FRAC_PI_4`. Often in odd ways and without `use`ing the constants, making it difficult to parse. * Use `PI` to specify angles. * General code-quality improvements. * Fix borked `hierarchy` example. Co-authored-by: devil-ira <[email protected]>
Pull request successfully merged into main. Build succeeded: |
PI
to specify angles in examples.PI
to specify angles in examples.
Examples inconsistently use either `TAU`, `PI`, `FRAC_PI_2` or `FRAC_PI_4`. Often in odd ways and without `use`ing the constants, making it difficult to parse. * Use `PI` to specify angles. * General code-quality improvements. * Fix borked `hierarchy` example. Co-authored-by: devil-ira <[email protected]>
Examples inconsistently use either `TAU`, `PI`, `FRAC_PI_2` or `FRAC_PI_4`. Often in odd ways and without `use`ing the constants, making it difficult to parse. * Use `PI` to specify angles. * General code-quality improvements. * Fix borked `hierarchy` example. Co-authored-by: devil-ira <[email protected]>
Commit 65252bb (Consistently use `PI` to specify angles in examples. (bevyengine#5825)) changed from using push_children to add_child without adjusting the comment
Commit 65252bb (Consistently use `PI` to specify angles in examples. (#5825)) changed from using push_children to add_child without adjusting the comment # Objective - Fix the comments to align with the code Co-authored-by: Alexander Stein <[email protected]>
Commit 65252bb (Consistently use `PI` to specify angles in examples. (bevyengine#5825)) changed from using push_children to add_child without adjusting the comment # Objective - Fix the comments to align with the code Co-authored-by: Alexander Stein <[email protected]>
Examples inconsistently use either
TAU
,PI
,FRAC_PI_2
orFRAC_PI_4
.Often in odd ways and without
use
ing the constants, making it difficult to parse.PI
to specify angles.hierarchy
example.