Skip to content

Commit

Permalink
Rename Direction2d/3d to Dir2/3 (#12189)
Browse files Browse the repository at this point in the history
# Objective

Split up from #12017, rename Bevy's direction types.

Currently, Bevy has the `Direction2d`, `Direction3d`, and `Direction3dA`
types, which provide a type-level guarantee that their contained vectors
remain normalized. They can be very useful for a lot of APIs for safety,
explicitness, and in some cases performance, as they can sometimes avoid
unnecessary normalizations.

However, many consider them to be inconvenient to use, and opt for
standard vector types like `Vec3` because of this. One reason is that
the direction type names are a bit long and can be annoying to write (of
course you can use autocomplete, but just typing `Vec3` is still nicer),
and in some intances, the extra characters can make formatting worse.
The naming is also inconsistent with Glam's shorter type names, and
results in names like `Direction3dA`, which (in my opinion) are
difficult to read and even a bit ugly.

This PR proposes renaming the types to `Dir2`, `Dir3`, and `Dir3A`.
These names are nice and easy to write, consistent with Glam, and work
well for variants like the SIMD aligned `Dir3A`. As a bonus, it can also
result in nicer formatting in a lot of cases, which can be seen from the
diff of this PR.

Some examples of what it looks like: (copied from #12017)

```rust
// Before
let ray_cast = RayCast2d::new(Vec2::ZERO, Direction2d::X, 5.0);

// After
let ray_cast = RayCast2d::new(Vec2::ZERO, Dir2::X, 5.0);
```

```rust
// Before (an example using Bevy XPBD)
let hit = spatial_query.cast_ray(
    Vec3::ZERO,
    Direction3d::X,
    f32::MAX,
    true,
    SpatialQueryFilter::default(),
);

// After
let hit = spatial_query.cast_ray(
    Vec3::ZERO,
    Dir3::X,
    f32::MAX,
    true,
    SpatialQueryFilter::default(),
);
```

```rust
// Before
self.circle(
    Vec3::new(0.0, -2.0, 0.0),
    Direction3d::Y,
    5.0,
    Color::TURQUOISE,
);

// After (formatting is collapsed in this case)
self.circle(Vec3::new(0.0, -2.0, 0.0), Dir3::Y, 5.0, Color::TURQUOISE);
```

## Solution

Rename `Direction2d`, `Direction3d`, and `Direction3dA` to `Dir2`,
`Dir3`, and `Dir3A`.

---

## Migration Guide

The `Direction2d` and `Direction3d` types have been renamed to `Dir2`
and `Dir3`.

## Additional Context

This has been brought up on the Discord a few times, and we had a small
[poll](https://discord.com/channels/691052431525675048/1203087353850364004/1212465038711984158)
on this. `Dir2`/`Dir3`/`Dir3A` was quite unanimously chosen as the best
option, but of course it was a very small poll and inconclusive, so
other opinions are certainly welcome too.

---------

Co-authored-by: IceSentry <[email protected]>
  • Loading branch information
Jondolf and IceSentry authored Feb 28, 2024
1 parent 043041f commit f418de8
Show file tree
Hide file tree
Showing 23 changed files with 262 additions and 348 deletions.
8 changes: 4 additions & 4 deletions crates/bevy_gizmos/src/circles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::prelude::{GizmoConfigGroup, Gizmos};
use bevy_math::Mat2;
use bevy_math::{Direction3d, Quat, Vec2, Vec3};
use bevy_math::{Dir3, Quat, Vec2, Vec3};
use bevy_render::color::LegacyColor;
use std::f32::consts::TAU;

Expand Down Expand Up @@ -106,12 +106,12 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> {
/// # use bevy_render::prelude::*;
/// # use bevy_math::prelude::*;
/// fn system(mut gizmos: Gizmos) {
/// gizmos.circle(Vec3::ZERO, Direction3d::Z, 1., LegacyColor::GREEN);
/// gizmos.circle(Vec3::ZERO, Dir3::Z, 1., LegacyColor::GREEN);
///
/// // Circles have 32 line-segments by default.
/// // You may want to increase this for larger circles.
/// gizmos
/// .circle(Vec3::ZERO, Direction3d::Z, 5., LegacyColor::RED)
/// .circle(Vec3::ZERO, Dir3::Z, 5., LegacyColor::RED)
/// .segments(64);
/// }
/// # bevy_ecs::system::assert_is_system(system);
Expand All @@ -120,7 +120,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> {
pub fn circle(
&mut self,
position: Vec3,
normal: Direction3d,
normal: Dir3,
radius: f32,
color: LegacyColor,
) -> EllipseBuilder<'_, 'w, 's, T> {
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_gizmos/src/gizmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use bevy_ecs::{
system::{Deferred, ReadOnlySystemParam, Res, Resource, SystemBuffer, SystemMeta, SystemParam},
world::{unsafe_world_cell::UnsafeWorldCell, World},
};
use bevy_math::{Direction3d, Mat2, Quat, Vec2, Vec3};
use bevy_math::{Dir3, Mat2, Quat, Vec2, Vec3};
use bevy_render::color::LegacyColor;
use bevy_transform::TransformPoint;

Expand Down Expand Up @@ -629,7 +629,7 @@ impl<T: GizmoConfigGroup> Drop for SphereBuilder<'_, '_, '_, T> {
self.gizmos
.circle(
self.position,
Direction3d::new_unchecked(self.rotation * axis),
Dir3::new_unchecked(self.rotation * axis),
self.radius,
self.color,
)
Expand Down
14 changes: 7 additions & 7 deletions crates/bevy_gizmos/src/primitives/dim2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bevy_math::primitives::{
BoxedPolygon, BoxedPolyline2d, Capsule2d, Circle, Ellipse, Line2d, Plane2d, Polygon,
Polyline2d, Primitive2d, Rectangle, RegularPolygon, Segment2d, Triangle2d,
};
use bevy_math::{Direction2d, Mat2, Vec2};
use bevy_math::{Dir2, Mat2, Vec2};
use bevy_render::color::LegacyColor;

use crate::prelude::{GizmoConfigGroup, Gizmos};
Expand Down Expand Up @@ -38,12 +38,12 @@ pub trait GizmoPrimitive2d<P: Primitive2d> {

// direction 2d

impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<Direction2d> for Gizmos<'w, 's, T> {
impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<Dir2> for Gizmos<'w, 's, T> {
type Output<'a> = () where Self : 'a;

fn primitive_2d(
&mut self,
primitive: Direction2d,
primitive: Dir2,
position: Vec2,
angle: f32,
color: LegacyColor,
Expand Down Expand Up @@ -163,7 +163,7 @@ impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<Capsule2d> for Gizmos<'w, 's,
pub struct Line2dBuilder<'a, 'w, 's, T: GizmoConfigGroup> {
gizmos: &'a mut Gizmos<'w, 's, T>,

direction: Direction2d, // Direction of the line
direction: Dir2, // Direction of the line

position: Vec2, // position of the center of the line
rotation: Mat2, // rotation of the line
Expand Down Expand Up @@ -262,7 +262,7 @@ impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<Plane2d> for Gizmos<'w, 's, T
.draw_arrow(true);

// draw the plane line
let direction = Direction2d::new_unchecked(-normal.perp());
let direction = Dir2::new_unchecked(-normal.perp());
self.primitive_2d(Line2d { direction }, position, angle, color)
.draw_arrow(false);

Expand All @@ -282,8 +282,8 @@ impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<Plane2d> for Gizmos<'w, 's, T
pub struct Segment2dBuilder<'a, 'w, 's, T: GizmoConfigGroup> {
gizmos: &'a mut Gizmos<'w, 's, T>,

direction: Direction2d, // Direction of the line segment
half_length: f32, // Half-length of the line segment
direction: Dir2, // Direction of the line segment
half_length: f32, // Half-length of the line segment

position: Vec2, // position of the center of the line segment
rotation: Mat2, // rotation of the line segment
Expand Down
10 changes: 5 additions & 5 deletions crates/bevy_gizmos/src/primitives/dim3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bevy_math::primitives::{
BoxedPolyline3d, Capsule3d, Cone, ConicalFrustum, Cuboid, Cylinder, Line3d, Plane3d,
Polyline3d, Primitive3d, Segment3d, Sphere, Torus,
};
use bevy_math::{Direction3d, Quat, Vec3};
use bevy_math::{Dir3, Quat, Vec3};
use bevy_render::color::LegacyColor;

use crate::prelude::{GizmoConfigGroup, Gizmos};
Expand Down Expand Up @@ -35,12 +35,12 @@ pub trait GizmoPrimitive3d<P: Primitive3d> {

// direction 3d

impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive3d<Direction3d> for Gizmos<'w, 's, T> {
impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive3d<Dir3> for Gizmos<'w, 's, T> {
type Output<'a> = () where Self: 'a;

fn primitive_3d(
&mut self,
primitive: Direction3d,
primitive: Dir3,
position: Vec3,
rotation: Quat,
color: LegacyColor,
Expand Down Expand Up @@ -139,7 +139,7 @@ pub struct Plane3dBuilder<'a, 'w, 's, T: GizmoConfigGroup> {
gizmos: &'a mut Gizmos<'w, 's, T>,

// direction of the normal orthogonal to the plane
normal: Direction3d,
normal: Dir3,

// Rotation of the sphere around the origin in 3D space
rotation: Quat,
Expand Down Expand Up @@ -218,7 +218,7 @@ impl<T: GizmoConfigGroup> Drop for Plane3dBuilder<'_, '_, '_, T> {
.map(|angle| Quat::from_axis_angle(normal, angle))
.for_each(|quat| {
let axis_direction = quat * normals_normal;
let direction = Direction3d::new_unchecked(axis_direction);
let direction = Dir3::new_unchecked(axis_direction);

// for each axis draw dotted line
(0..)
Expand Down
23 changes: 7 additions & 16 deletions crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
BoxedPolygon, BoxedPolyline2d, Capsule2d, Circle, Ellipse, Line2d, Plane2d, Polygon,
Polyline2d, Rectangle, RegularPolygon, Segment2d, Triangle2d,
},
Direction2d, Mat2, Vec2,
Dir2, Mat2, Vec2,
};

use super::{Aabb2d, Bounded2d, BoundingCircle};
Expand Down Expand Up @@ -236,7 +236,7 @@ impl Bounded2d for Capsule2d {
// Get the line segment between the hemicircles of the rotated capsule
let segment = Segment2d {
// Multiplying a normalized vector (Vec2::Y) with a rotation returns a normalized vector.
direction: Direction2d::new_unchecked(Mat2::from_angle(rotation) * Vec2::Y),
direction: Dir2::new_unchecked(Mat2::from_angle(rotation) * Vec2::Y),
half_length: self.half_length,
};
let (a, b) = (segment.point1(), segment.point2());
Expand Down Expand Up @@ -266,7 +266,7 @@ mod tests {
Capsule2d, Circle, Ellipse, Line2d, Plane2d, Polygon, Polyline2d, Rectangle,
RegularPolygon, Segment2d, Triangle2d,
},
Direction2d,
Dir2,
};

#[test]
Expand Down Expand Up @@ -322,31 +322,22 @@ mod tests {
fn line() {
let translation = Vec2::new(2.0, 1.0);

let aabb1 = Line2d {
direction: Direction2d::Y,
}
.aabb_2d(translation, 0.0);
let aabb1 = Line2d { direction: Dir2::Y }.aabb_2d(translation, 0.0);
assert_eq!(aabb1.min, Vec2::new(2.0, -f32::MAX / 2.0));
assert_eq!(aabb1.max, Vec2::new(2.0, f32::MAX / 2.0));

let aabb2 = Line2d {
direction: Direction2d::X,
}
.aabb_2d(translation, 0.0);
let aabb2 = Line2d { direction: Dir2::X }.aabb_2d(translation, 0.0);
assert_eq!(aabb2.min, Vec2::new(-f32::MAX / 2.0, 1.0));
assert_eq!(aabb2.max, Vec2::new(f32::MAX / 2.0, 1.0));

let aabb3 = Line2d {
direction: Direction2d::from_xy(1.0, 1.0).unwrap(),
direction: Dir2::from_xy(1.0, 1.0).unwrap(),
}
.aabb_2d(translation, 0.0);
assert_eq!(aabb3.min, Vec2::new(-f32::MAX / 2.0, -f32::MAX / 2.0));
assert_eq!(aabb3.max, Vec2::new(f32::MAX / 2.0, f32::MAX / 2.0));

let bounding_circle = Line2d {
direction: Direction2d::Y,
}
.bounding_circle(translation, 0.0);
let bounding_circle = Line2d { direction: Dir2::Y }.bounding_circle(translation, 0.0);
assert_eq!(bounding_circle.center, translation);
assert_eq!(bounding_circle.radius(), f32::MAX / 2.0);
}
Expand Down
29 changes: 9 additions & 20 deletions crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
BoxedPolyline3d, Capsule3d, Cone, ConicalFrustum, Cuboid, Cylinder, Line3d, Plane3d,
Polyline3d, Segment3d, Sphere, Torus, Triangle2d,
},
Direction3d, Mat3, Quat, Vec2, Vec3,
Dir3, Mat3, Quat, Vec2, Vec3,
};

use super::{Aabb3d, Bounded3d, BoundingSphere};
Expand Down Expand Up @@ -150,7 +150,7 @@ impl Bounded3d for Capsule3d {
// Get the line segment between the hemispheres of the rotated capsule
let segment = Segment3d {
// Multiplying a normalized vector (Vec3::Y) with a rotation returns a normalized vector.
direction: Direction3d::new_unchecked(rotation * Vec3::Y),
direction: Dir3::new_unchecked(rotation * Vec3::Y),
half_length: self.half_length,
};
let (a, b) = (segment.point1(), segment.point2());
Expand Down Expand Up @@ -313,7 +313,7 @@ mod tests {
Capsule3d, Cone, ConicalFrustum, Cuboid, Cylinder, Line3d, Plane3d, Polyline3d,
Segment3d, Sphere, Torus,
},
Direction3d,
Dir3,
};

#[test]
Expand Down Expand Up @@ -359,38 +359,27 @@ mod tests {
fn line() {
let translation = Vec3::new(2.0, 1.0, 0.0);

let aabb1 = Line3d {
direction: Direction3d::Y,
}
.aabb_3d(translation, Quat::IDENTITY);
let aabb1 = Line3d { direction: Dir3::Y }.aabb_3d(translation, Quat::IDENTITY);
assert_eq!(aabb1.min, Vec3::new(2.0, -f32::MAX / 2.0, 0.0));
assert_eq!(aabb1.max, Vec3::new(2.0, f32::MAX / 2.0, 0.0));

let aabb2 = Line3d {
direction: Direction3d::X,
}
.aabb_3d(translation, Quat::IDENTITY);
let aabb2 = Line3d { direction: Dir3::X }.aabb_3d(translation, Quat::IDENTITY);
assert_eq!(aabb2.min, Vec3::new(-f32::MAX / 2.0, 1.0, 0.0));
assert_eq!(aabb2.max, Vec3::new(f32::MAX / 2.0, 1.0, 0.0));

let aabb3 = Line3d {
direction: Direction3d::Z,
}
.aabb_3d(translation, Quat::IDENTITY);
let aabb3 = Line3d { direction: Dir3::Z }.aabb_3d(translation, Quat::IDENTITY);
assert_eq!(aabb3.min, Vec3::new(2.0, 1.0, -f32::MAX / 2.0));
assert_eq!(aabb3.max, Vec3::new(2.0, 1.0, f32::MAX / 2.0));

let aabb4 = Line3d {
direction: Direction3d::from_xyz(1.0, 1.0, 1.0).unwrap(),
direction: Dir3::from_xyz(1.0, 1.0, 1.0).unwrap(),
}
.aabb_3d(translation, Quat::IDENTITY);
assert_eq!(aabb4.min, Vec3::splat(-f32::MAX / 2.0));
assert_eq!(aabb4.max, Vec3::splat(f32::MAX / 2.0));

let bounding_sphere = Line3d {
direction: Direction3d::Y,
}
.bounding_sphere(translation, Quat::IDENTITY);
let bounding_sphere =
Line3d { direction: Dir3::Y }.bounding_sphere(translation, Quat::IDENTITY);
assert_eq!(bounding_sphere.center, translation);
assert_eq!(bounding_sphere.radius(), f32::MAX / 2.0);
}
Expand Down
Loading

0 comments on commit f418de8

Please sign in to comment.