Skip to content

Commit 039ba28

Browse files
IQuick143mweatherleyjf908
authored
Fix documentation for Rot2 (#20667)
# Objective - Certain pieces of documentation on `Rot2` are imprecise or misleading - Try to make docs more comprehensive ## Solution - Rewrite a bunch of doc comment, dropping `in radians` where there's no values in radians, - Clear up comments about "clamping" input angles by a hopefully more accurate and clear explanation. ## Testing - CI --------- Co-authored-by: Matty Weatherley <[email protected]> Co-authored-by: jf908 <[email protected]>
1 parent 7e29da9 commit 039ba28

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

crates/bevy_math/src/rotation2d.rs

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use bevy_reflect::{std_traits::ReflectDefault, Reflect};
1212
#[cfg(all(feature = "serialize", feature = "bevy_reflect"))]
1313
use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
1414

15-
/// A counterclockwise 2D rotation.
15+
/// A 2D rotation.
1616
///
1717
/// # Example
1818
///
@@ -21,7 +21,7 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
2121
/// # use bevy_math::{Rot2, Vec2};
2222
/// use std::f32::consts::PI;
2323
///
24-
/// // Create rotations from radians or degrees
24+
/// // Create rotations from counterclockwise angles in radians or degrees
2525
/// let rotation1 = Rot2::radians(PI / 2.0);
2626
/// let rotation2 = Rot2::degrees(45.0);
2727
///
@@ -50,11 +50,11 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
5050
)]
5151
#[doc(alias = "rotation", alias = "rotation2d", alias = "rotation_2d")]
5252
pub struct Rot2 {
53-
/// The cosine of the rotation angle in radians.
53+
/// The cosine of the rotation angle.
5454
///
5555
/// This is the real part of the unit complex number representing the rotation.
5656
pub cos: f32,
57-
/// The sine of the rotation angle in radians.
57+
/// The sine of the rotation angle.
5858
///
5959
/// This is the imaginary part of the unit complex number representing the rotation.
6060
pub sin: f32,
@@ -68,46 +68,60 @@ impl Default for Rot2 {
6868

6969
impl Rot2 {
7070
/// No rotation.
71+
/// Also equals a full turn that returns back to its original position.
72+
/// ```
73+
/// # use approx::assert_relative_eq;
74+
/// # use bevy_math::Rot2;
75+
/// #[cfg(feature = "approx")]
76+
/// assert_relative_eq!(Rot2::IDENTITY, Rot2::degrees(360.0), epsilon = 2e-7);
77+
/// ```
7178
pub const IDENTITY: Self = Self { cos: 1.0, sin: 0.0 };
7279

7380
/// A rotation of π radians.
81+
/// Corresponds to a half-turn.
7482
pub const PI: Self = Self {
7583
cos: -1.0,
7684
sin: 0.0,
7785
};
7886

7987
/// A counterclockwise rotation of π/2 radians.
88+
/// Corresponds to a counterclockwise quarter-turn.
8089
pub const FRAC_PI_2: Self = Self { cos: 0.0, sin: 1.0 };
8190

8291
/// A counterclockwise rotation of π/3 radians.
92+
/// Corresponds to a counterclockwise turn by 60°.
8393
pub const FRAC_PI_3: Self = Self {
8494
cos: 0.5,
8595
sin: 0.866_025_4,
8696
};
8797

8898
/// A counterclockwise rotation of π/4 radians.
99+
/// Corresponds to a counterclockwise turn by 45°.
89100
pub const FRAC_PI_4: Self = Self {
90101
cos: core::f32::consts::FRAC_1_SQRT_2,
91102
sin: core::f32::consts::FRAC_1_SQRT_2,
92103
};
93104

94105
/// A counterclockwise rotation of π/6 radians.
106+
/// Corresponds to a counterclockwise turn by 30°.
95107
pub const FRAC_PI_6: Self = Self {
96108
cos: 0.866_025_4,
97109
sin: 0.5,
98110
};
99111

100112
/// A counterclockwise rotation of π/8 radians.
113+
/// Corresponds to a counterclockwise turn by 22.5°.
101114
pub const FRAC_PI_8: Self = Self {
102115
cos: 0.923_879_5,
103116
sin: 0.382_683_43,
104117
};
105118

106119
/// Creates a [`Rot2`] from a counterclockwise angle in radians.
120+
/// A negative argument corresponds to a clockwise rotation.
107121
///
108122
/// # Note
109123
///
110-
/// The input rotation will always be clamped to the range `(-π, π]` by design.
124+
/// Angles larger than or equal to 2π (in either direction) loop around to smaller rotations, since a full rotation returns an object to its starting orientation.
111125
///
112126
/// # Example
113127
///
@@ -124,6 +138,10 @@ impl Rot2 {
124138
/// let rot3 = Rot2::radians(PI);
125139
/// #[cfg(feature = "approx")]
126140
/// assert_relative_eq!(rot1 * rot1, rot3);
141+
///
142+
/// // A rotation by 3π and 1π are the same
143+
/// #[cfg(feature = "approx")]
144+
/// assert_relative_eq!(Rot2::radians(3.0 * PI), Rot2::radians(PI));
127145
/// ```
128146
#[inline]
129147
pub fn radians(radians: f32) -> Self {
@@ -132,16 +150,17 @@ impl Rot2 {
132150
}
133151

134152
/// Creates a [`Rot2`] from a counterclockwise angle in degrees.
153+
/// A negative argument corresponds to a clockwise rotation.
135154
///
136155
/// # Note
137156
///
138-
/// The input rotation will always be clamped to the range `(-180°, 180°]` by design.
157+
/// Angles larger than or equal to 360° (in either direction) loop around to smaller rotations, since a full rotation returns an object to its starting orientation.
139158
///
140159
/// # Example
141160
///
142161
/// ```
143162
/// # use bevy_math::Rot2;
144-
/// # use approx::assert_relative_eq;
163+
/// # use approx::{assert_relative_eq, assert_abs_diff_eq};
145164
///
146165
/// let rot1 = Rot2::degrees(270.0);
147166
/// let rot2 = Rot2::degrees(-90.0);
@@ -151,17 +170,22 @@ impl Rot2 {
151170
/// let rot3 = Rot2::degrees(180.0);
152171
/// #[cfg(feature = "approx")]
153172
/// assert_relative_eq!(rot1 * rot1, rot3);
173+
///
174+
/// // A rotation by 365° and 5° are the same
175+
/// #[cfg(feature = "approx")]
176+
/// assert_abs_diff_eq!(Rot2::degrees(365.0), Rot2::degrees(5.0), epsilon = 2e-7);
154177
/// ```
155178
#[inline]
156179
pub fn degrees(degrees: f32) -> Self {
157180
Self::radians(degrees.to_radians())
158181
}
159182

160183
/// Creates a [`Rot2`] from a counterclockwise fraction of a full turn of 360 degrees.
184+
/// A negative argument corresponds to a clockwise rotation.
161185
///
162186
/// # Note
163187
///
164-
/// The input rotation will always be clamped to the range `(-50%, 50%]` by design.
188+
/// Angles larger than or equal to 1 turn (in either direction) loop around to smaller rotations, since a full rotation returns an object to its starting orientation.
165189
///
166190
/// # Example
167191
///
@@ -177,13 +201,17 @@ impl Rot2 {
177201
/// let rot3 = Rot2::turn_fraction(0.5);
178202
/// #[cfg(feature = "approx")]
179203
/// assert_relative_eq!(rot1 * rot1, rot3);
204+
///
205+
/// // A rotation by 1.5 turns and 0.5 turns are the same
206+
/// #[cfg(feature = "approx")]
207+
/// assert_relative_eq!(Rot2::turn_fraction(1.5), Rot2::turn_fraction(0.5));
180208
/// ```
181209
#[inline]
182210
pub fn turn_fraction(fraction: f32) -> Self {
183211
Self::radians(TAU * fraction)
184212
}
185213

186-
/// Creates a [`Rot2`] from the sine and cosine of an angle in radians.
214+
/// Creates a [`Rot2`] from the sine and cosine of an angle.
187215
///
188216
/// The rotation is only valid if `sin * sin + cos * cos == 1.0`.
189217
///
@@ -200,25 +228,25 @@ impl Rot2 {
200228
rotation
201229
}
202230

203-
/// Returns the rotation in radians in the `(-pi, pi]` range.
231+
/// Returns a corresponding rotation angle in radians in the `(-pi, pi]` range.
204232
#[inline]
205233
pub fn as_radians(self) -> f32 {
206234
ops::atan2(self.sin, self.cos)
207235
}
208236

209-
/// Returns the rotation in degrees in the `(-180, 180]` range.
237+
/// Returns a corresponding rotation angle in degrees in the `(-180, 180]` range.
210238
#[inline]
211239
pub fn as_degrees(self) -> f32 {
212240
self.as_radians().to_degrees()
213241
}
214242

215-
/// Returns the rotation as a fraction of a full 360 degree turn.
243+
/// Returns a corresponding rotation angle as a fraction of a full 360 degree turn in the `(-0.5, 0.5]` range.
216244
#[inline]
217245
pub fn as_turn_fraction(self) -> f32 {
218246
self.as_radians() / TAU
219247
}
220248

221-
/// Returns the sine and cosine of the rotation angle in radians.
249+
/// Returns the sine and cosine of the rotation angle.
222250
#[inline]
223251
pub const fn sin_cos(self) -> (f32, f32) {
224252
(self.sin, self.cos)

0 commit comments

Comments
 (0)