From 82d5518654816886ac5392a5fdbc221410d65215 Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Thu, 16 Jan 2025 13:17:03 +0100 Subject: [PATCH 01/43] Reworked Segment2d into it's cartesian form --- crates/bevy_gizmos/src/primitives/dim2.rs | 25 +++-- .../src/bounding/bounded2d/primitive_impls.rs | 24 ++--- crates/bevy_math/src/primitives/dim2.rs | 93 ++++++++++++++----- examples/2d/bounding_2d.rs | 7 +- examples/math/render_primitives.rs | 9 +- 5 files changed, 101 insertions(+), 57 deletions(-) diff --git a/crates/bevy_gizmos/src/primitives/dim2.rs b/crates/bevy_gizmos/src/primitives/dim2.rs index beb9c6f315f24..ffddfede98887 100644 --- a/crates/bevy_gizmos/src/primitives/dim2.rs +++ b/crates/bevy_gizmos/src/primitives/dim2.rs @@ -540,10 +540,7 @@ where } // draw normal of the plane (orthogonal to the plane itself) let normal = primitive.normal; - let normal_segment = Segment2d { - direction: normal, - half_length: HALF_MIN_LINE_LEN, - }; + let normal_segment = Segment2d::from_direction(normal, HALF_MIN_LINE_LEN * 2.); self.primitive_2d( &normal_segment, // offset the normal so it starts on the plane line @@ -577,8 +574,8 @@ where { gizmos: &'a mut GizmoBuffer, - direction: Dir2, // Direction of the line segment - half_length: f32, // Half-length of the line segment + point1: Vec2, // First point of the segment + point2: Vec2, // Second point of the segment isometry: Isometry2d, // isometric transformation of the line segment color: Color, // color of the line segment @@ -616,8 +613,8 @@ where ) -> Self::Output<'_> { Segment2dBuilder { gizmos: self, - direction: primitive.direction, - half_length: primitive.half_length, + point1: primitive.point1(), + point2: primitive.point2(), isometry: isometry.into(), color: color.into(), @@ -637,14 +634,16 @@ where return; } - let direction = self.direction * self.half_length; - let start = self.isometry * (-direction); - let end = self.isometry * direction; + let segment = Segment2d::new(self.point1, self.point2) + .offset(self.isometry.translation) + .rotated(self.isometry.rotation); if self.draw_arrow { - self.gizmos.arrow_2d(start, end, self.color); + self.gizmos + .arrow_2d(segment.point1(), segment.point2(), self.color); } else { - self.gizmos.line_2d(start, end, self.color); + self.gizmos + .line_2d(segment.point1(), segment.point2(), self.color); } } } diff --git a/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs b/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs index e6c08136881b1..f1632e87a08bb 100644 --- a/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs +++ b/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs @@ -265,18 +265,11 @@ impl Bounded2d for Line2d { impl Bounded2d for Segment2d { fn aabb_2d(&self, isometry: impl Into) -> Aabb2d { - let isometry = isometry.into(); - - // Rotate the segment by `rotation` - let direction = isometry.rotation * *self.direction; - let half_size = (self.half_length * direction).abs(); - - Aabb2d::new(isometry.translation, half_size) + Aabb2d::from_point_cloud(isometry, &[self.point1(), self.point2()]) } fn bounding_circle(&self, isometry: impl Into) -> BoundingCircle { - let isometry = isometry.into(); - BoundingCircle::new(isometry.translation, self.half_length) + BoundingCircle::from_point_cloud(isometry, &[self.point1(), self.point2()]) } } @@ -336,7 +329,8 @@ impl Bounded2d for Triangle2d { if let Some((point1, point2)) = side_opposite_to_non_acute { // The triangle is obtuse or right, so the minimum bounding circle's diameter is equal to the longest side. // We can compute the minimum bounding circle from the line segment of the longest side. - let (segment, center) = Segment2d::from_points(point1, point2); + let segment = Segment2d::new(point1, point2).centered(); + let center = (point1 + point2) / 2.; segment.bounding_circle(isometry * Isometry2d::from_translation(center)) } else { // The triangle is acute, so the smallest bounding circle is the circumcircle. @@ -416,12 +410,8 @@ impl Bounded2d for Capsule2d { fn aabb_2d(&self, isometry: impl Into) -> Aabb2d { let isometry = isometry.into(); - // Get the line segment between the semicircles of the rotated capsule - let segment = Segment2d { - // Multiplying a normalized vector (Vec2::Y) with a rotation returns a normalized vector. - direction: isometry.rotation * Dir2::Y, - half_length: self.half_length, - }; + // Get the line segment between the hemicircles of the rotated capsule + let segment = Segment2d::from_direction(isometry.rotation * Dir2::Y, self.half_length * 2.); let (a, b) = (segment.point1(), segment.point2()); // Expand the line segment by the capsule radius to get the capsule half-extents @@ -886,7 +876,7 @@ mod tests { fn segment() { let translation = Vec2::new(2.0, 1.0); let isometry = Isometry2d::from_translation(translation); - let segment = Segment2d::from_points(Vec2::new(-1.0, -0.5), Vec2::new(1.0, 0.5)).0; + let segment = Segment2d::new(Vec2::new(-1.0, -0.5), Vec2::new(1.0, 0.5)); let aabb = segment.aabb_2d(isometry); assert_eq!(aabb.min, Vec2::new(1.0, 0.5)); diff --git a/crates/bevy_math/src/primitives/dim2.rs b/crates/bevy_math/src/primitives/dim2.rs index cdd5b805cde3a..18264a70eab53 100644 --- a/crates/bevy_math/src/primitives/dim2.rs +++ b/crates/bevy_math/src/primitives/dim2.rs @@ -5,7 +5,7 @@ use thiserror::Error; use super::{Measured2d, Primitive2d, WindingOrder}; use crate::{ ops::{self, FloatPow}, - Dir2, Vec2, + Dir2, Rot2, Vec2, }; #[cfg(feature = "alloc")] @@ -1221,22 +1221,18 @@ impl Primitive2d for Line2d {} )] #[doc(alias = "LineSegment2d")] pub struct Segment2d { - /// The direction of the line segment - pub direction: Dir2, - /// Half the length of the line segment. The segment extends by this amount in both - /// the given direction and its opposite direction - pub half_length: f32, + /// First point of the segment + pub point1: Vec2, + /// Second point of the segment + pub point2: Vec2, } impl Primitive2d for Segment2d {} impl Segment2d { - /// Create a new `Segment2d` from a direction and full length of the segment + /// Create a new `Segment2d` from the two points composing it #[inline(always)] - pub fn new(direction: Dir2, length: f32) -> Self { - Self { - direction, - half_length: length / 2.0, - } + pub fn new(point1: Vec2, point2: Vec2) -> Self { + Self { point1, point2 } } /// Create a new `Segment2d` from its endpoints and compute its geometric center @@ -1245,27 +1241,82 @@ impl Segment2d { /// /// Panics if `point1 == point2` #[inline(always)] + #[deprecated(since = "0.15.1", note = "Use the `new` constructor instead")] pub fn from_points(point1: Vec2, point2: Vec2) -> (Self, Vec2) { - let diff = point2 - point1; - let length = diff.length(); + (Self::new(point1, point2), (point1 + point2) / 2.) + } - ( - // We are dividing by the length here, so the vector is normalized. - Self::new(Dir2::new_unchecked(diff / length), length), - (point1 + point2) / 2., - ) + /// Create a new `Segment2d` at the origin from a `direction` and `half_length` + #[inline(always)] + pub fn from_direction(direction: Dir2, length: f32) -> Segment2d { + let half_length = length / 2.; + Self::new(direction * -half_length, direction * half_length) } /// Get the position of the first point on the line segment #[inline(always)] pub fn point1(&self) -> Vec2 { - *self.direction * -self.half_length + self.point1 } /// Get the position of the second point on the line segment #[inline(always)] pub fn point2(&self) -> Vec2 { - *self.direction * self.half_length + self.point2 + } + + /// Get the segment's center + #[inline(always)] + pub fn center(&self) -> Vec2 { + (self.point1() + self.point2()) / 2. + } + + /// Get the segment's length + #[inline(always)] + pub fn length(&self) -> f32 { + self.point1().distance(self.point2()) + } + + /// Get the segment offset by a vector + #[inline(always)] + pub fn offset(&self, offset: Vec2) -> Segment2d { + Self::new(self.point1() + offset, self.point2() + offset) + } + + /// Get the segment rotated around it's center + #[inline(always)] + pub fn rotated(&self, rotation: Rot2) -> Segment2d { + pub fn rotate_point(p: Vec2, rotation: Rot2) -> Vec2 { + Vec2::new( + p.x * rotation.cos - p.y * rotation.sin, + p.x * rotation.sin + p.y * rotation.cos, + ) + } + // We center the segment for the purpose of the rotation, then offset back to it's original position + let offset_from_origin = self.center(); + let centered = self.centered(); + let centered_rotated: Segment2d = Segment2d::new( + rotate_point(centered.point1(), rotation), + rotate_point(centered.point2(), rotation), + ); + centered_rotated.offset(offset_from_origin) + } + + /// Get the segment with it's center is at the origin + #[inline(always)] + pub fn centered(&self) -> Segment2d { + let center = self.center(); + self.offset(-center) + } + + /// Get the segment with a new length + #[inline(always)] + pub fn resized(&self, length: f32) -> Segment2d { + let offset_from_origin = self.center(); + let centered = self.centered(); + let ratio = length / self.length(); + let segment = Segment2d::new(centered.point1() * ratio, centered.point2() * ratio); + segment.offset(offset_from_origin) } } diff --git a/examples/2d/bounding_2d.rs b/examples/2d/bounding_2d.rs index f4c24e739432f..fe8dccae9c040 100644 --- a/examples/2d/bounding_2d.rs +++ b/examples/2d/bounding_2d.rs @@ -224,13 +224,16 @@ fn setup(mut commands: Commands) { Vec2::new(40., 50.), )), Spin, - DesiredVolume::Aabb, + DesiredVolume::Circle, Intersects::default(), )); commands.spawn(( Transform::from_xyz(-OFFSET_X, -OFFSET_Y, 0.), - Shape::Line(Segment2d::new(Dir2::from_xy(1., 0.3).unwrap(), 90.)), + Shape::Line(Segment2d::from_direction( + Dir2::from_xy(1., 0.3).unwrap(), + 90., + )), Spin, DesiredVolume::Circle, Intersects::default(), diff --git a/examples/math/render_primitives.rs b/examples/math/render_primitives.rs index 4d9b3325510bf..4df5bf5261abe 100644 --- a/examples/math/render_primitives.rs +++ b/examples/math/render_primitives.rs @@ -187,12 +187,13 @@ const LINE2D: Line2d = Line2d { direction: Dir2::X }; const LINE3D: Line3d = Line3d { direction: Dir3::X }; const SEGMENT_2D: Segment2d = Segment2d { - direction: Dir2::X, - half_length: BIG_2D, + point1: Vec2::new(-BIG_2D / 2., 0.), + point2: Vec2::new(BIG_2D / 2., 0.), }; + const SEGMENT_3D: Segment3d = Segment3d { - direction: Dir3::X, - half_length: BIG_3D, + point1: Vec3::new(-BIG_3D / 2., 0., 0.), + point2: Vec3::new(BIG_3D / 2., 0., 0.), }; const POLYLINE_2D: Polyline2d<4> = Polyline2d { From a33987bb058826145542e6827611ff1071c6cc92 Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Thu, 16 Jan 2025 13:46:21 +0100 Subject: [PATCH 02/43] fixed extrusion --- crates/bevy_math/src/bounding/bounded3d/extrusion.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/bevy_math/src/bounding/bounded3d/extrusion.rs b/crates/bevy_math/src/bounding/bounded3d/extrusion.rs index 5403fa176ea53..dcf4b7bb13042 100644 --- a/crates/bevy_math/src/bounding/bounded3d/extrusion.rs +++ b/crates/bevy_math/src/bounding/bounded3d/extrusion.rs @@ -348,7 +348,10 @@ mod tests { #[test] fn segment() { - let extrusion = Extrusion::new(Segment2d::new(Dir2::new_unchecked(Vec2::NEG_Y), 3.), 4.0); + let extrusion = Extrusion::new( + Segment2d::from_direction(Dir2::new_unchecked(Vec2::NEG_Y), 3.), + 4.0, + ); let translation = Vec3::new(3., 4., 5.); let rotation = Quat::from_rotation_x(FRAC_PI_4); let isometry = Isometry3d::new(translation, rotation); From be0416cbd81a9703b4cdc99d8c232b43367c1929 Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Thu, 16 Jan 2025 13:54:38 +0100 Subject: [PATCH 03/43] fix commit order mistake --- examples/math/render_primitives.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/math/render_primitives.rs b/examples/math/render_primitives.rs index 4df5bf5261abe..30dfbaf88efe7 100644 --- a/examples/math/render_primitives.rs +++ b/examples/math/render_primitives.rs @@ -192,8 +192,8 @@ const SEGMENT_2D: Segment2d = Segment2d { }; const SEGMENT_3D: Segment3d = Segment3d { - point1: Vec3::new(-BIG_3D / 2., 0., 0.), - point2: Vec3::new(BIG_3D / 2., 0., 0.), + direction: Dir3::X, + half_length: BIG_3D, }; const POLYLINE_2D: Polyline2d<4> = Polyline2d { From b3d4deb8d0eff5a6f725656446a323359bb09916 Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Thu, 16 Jan 2025 14:01:05 +0100 Subject: [PATCH 04/43] fix typo --- crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs b/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs index f1632e87a08bb..ac1e41cb1f3bc 100644 --- a/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs +++ b/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs @@ -410,7 +410,7 @@ impl Bounded2d for Capsule2d { fn aabb_2d(&self, isometry: impl Into) -> Aabb2d { let isometry = isometry.into(); - // Get the line segment between the hemicircles of the rotated capsule + // Get the line segment between the semicircles of the rotated capsule let segment = Segment2d::from_direction(isometry.rotation * Dir2::Y, self.half_length * 2.); let (a, b) = (segment.point1(), segment.point2()); From e3d846426bf37d3a365a9891a432c997ec8224f7 Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Thu, 16 Jan 2025 19:44:31 +0100 Subject: [PATCH 05/43] fix changed method name and fixed method docs --- crates/bevy_math/src/primitives/dim2.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_math/src/primitives/dim2.rs b/crates/bevy_math/src/primitives/dim2.rs index 18264a70eab53..f0a38deed996f 100644 --- a/crates/bevy_math/src/primitives/dim2.rs +++ b/crates/bevy_math/src/primitives/dim2.rs @@ -1246,9 +1246,9 @@ impl Segment2d { (Self::new(point1, point2), (point1 + point2) / 2.) } - /// Create a new `Segment2d` at the origin from a `direction` and `half_length` + /// Create a new `Segment2d` at the origin from a `direction` and `length` #[inline(always)] - pub fn from_direction(direction: Dir2, length: f32) -> Segment2d { + pub fn from_direction_and_length(direction: Dir2, length: f32) -> Segment2d { let half_length = length / 2.; Self::new(direction * -half_length, direction * half_length) } From b9e3cdf1d42bf6b91e471d70debb36c2472bd652 Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Thu, 16 Jan 2025 19:47:12 +0100 Subject: [PATCH 06/43] apply from_direction rename --- crates/bevy_gizmos/src/primitives/dim2.rs | 2 +- crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs | 5 ++++- crates/bevy_math/src/bounding/bounded3d/extrusion.rs | 2 +- examples/2d/bounding_2d.rs | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/crates/bevy_gizmos/src/primitives/dim2.rs b/crates/bevy_gizmos/src/primitives/dim2.rs index ffddfede98887..26dd7aa302edb 100644 --- a/crates/bevy_gizmos/src/primitives/dim2.rs +++ b/crates/bevy_gizmos/src/primitives/dim2.rs @@ -540,7 +540,7 @@ where } // draw normal of the plane (orthogonal to the plane itself) let normal = primitive.normal; - let normal_segment = Segment2d::from_direction(normal, HALF_MIN_LINE_LEN * 2.); + let normal_segment = Segment2d::from_direction_and_length(normal, HALF_MIN_LINE_LEN * 2.); self.primitive_2d( &normal_segment, // offset the normal so it starts on the plane line diff --git a/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs b/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs index ac1e41cb1f3bc..9386b9bffe0dc 100644 --- a/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs +++ b/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs @@ -411,7 +411,10 @@ impl Bounded2d for Capsule2d { let isometry = isometry.into(); // Get the line segment between the semicircles of the rotated capsule - let segment = Segment2d::from_direction(isometry.rotation * Dir2::Y, self.half_length * 2.); + let segment = Segment2d::from_direction_and_length( + isometry.rotation * Dir2::Y, + self.half_length * 2., + ); let (a, b) = (segment.point1(), segment.point2()); // Expand the line segment by the capsule radius to get the capsule half-extents diff --git a/crates/bevy_math/src/bounding/bounded3d/extrusion.rs b/crates/bevy_math/src/bounding/bounded3d/extrusion.rs index dcf4b7bb13042..47d5d6676356a 100644 --- a/crates/bevy_math/src/bounding/bounded3d/extrusion.rs +++ b/crates/bevy_math/src/bounding/bounded3d/extrusion.rs @@ -349,7 +349,7 @@ mod tests { #[test] fn segment() { let extrusion = Extrusion::new( - Segment2d::from_direction(Dir2::new_unchecked(Vec2::NEG_Y), 3.), + Segment2d::from_direction_and_length(Dir2::new_unchecked(Vec2::NEG_Y), 3.), 4.0, ); let translation = Vec3::new(3., 4., 5.); diff --git a/examples/2d/bounding_2d.rs b/examples/2d/bounding_2d.rs index fe8dccae9c040..0b8017e0487f0 100644 --- a/examples/2d/bounding_2d.rs +++ b/examples/2d/bounding_2d.rs @@ -230,7 +230,7 @@ fn setup(mut commands: Commands) { commands.spawn(( Transform::from_xyz(-OFFSET_X, -OFFSET_Y, 0.), - Shape::Line(Segment2d::from_direction( + Shape::Line(Segment2d::from_direction_and_length( Dir2::from_xy(1., 0.3).unwrap(), 90., )), From 4619993a6f1ec6c3637d3cee37a1d0263c084f37 Mon Sep 17 00:00:00 2001 From: Sigma-dev <72008323+Sigma-dev@users.noreply.github.com> Date: Thu, 16 Jan 2025 19:48:37 +0100 Subject: [PATCH 07/43] Update crates/bevy_math/src/primitives/dim2.rs Co-authored-by: Alice Cecile --- crates/bevy_math/src/primitives/dim2.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_math/src/primitives/dim2.rs b/crates/bevy_math/src/primitives/dim2.rs index f0a38deed996f..9cae9fdcd4d3a 100644 --- a/crates/bevy_math/src/primitives/dim2.rs +++ b/crates/bevy_math/src/primitives/dim2.rs @@ -1283,7 +1283,7 @@ impl Segment2d { Self::new(self.point1() + offset, self.point2() + offset) } - /// Get the segment rotated around it's center + /// Compute a new segment, based on the original segment rotated around its center #[inline(always)] pub fn rotated(&self, rotation: Rot2) -> Segment2d { pub fn rotate_point(p: Vec2, rotation: Rot2) -> Vec2 { From e54a5fcfc7f417e89aee40202730bb191c9eb88f Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Thu, 16 Jan 2025 19:50:16 +0100 Subject: [PATCH 08/43] rework segment3d --- crates/bevy_gizmos/src/primitives/dim3.rs | 8 +- .../src/bounding/bounded3d/primitive_impls.rs | 10 +-- crates/bevy_math/src/primitives/dim3.rs | 74 ++++++++++++++----- 3 files changed, 62 insertions(+), 30 deletions(-) diff --git a/crates/bevy_gizmos/src/primitives/dim3.rs b/crates/bevy_gizmos/src/primitives/dim3.rs index b99ab9a31a793..20d6b28215aea 100644 --- a/crates/bevy_gizmos/src/primitives/dim3.rs +++ b/crates/bevy_gizmos/src/primitives/dim3.rs @@ -228,9 +228,11 @@ where return; } - let isometry = isometry.into(); - let direction = primitive.direction.as_vec3(); - self.line(isometry * direction, isometry * (-direction), color); + let isometry: Isometry3d = isometry.into(); + let transformed = primitive + .rotated(isometry.rotation) + .offset(isometry.translation.into()); + self.line(transformed.point1(), transformed.point1(), color); } } diff --git a/crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs b/crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs index 679d8577f0d41..cb5412afec673 100644 --- a/crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs +++ b/crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs @@ -76,18 +76,12 @@ impl Bounded3d for Line3d { impl Bounded3d for Segment3d { fn aabb_3d(&self, isometry: impl Into) -> Aabb3d { - let isometry = isometry.into(); - - // Rotate the segment by `rotation` - let direction = isometry.rotation * *self.direction; - let half_size = (self.half_length * direction).abs(); - - Aabb3d::new(isometry.translation, half_size) + Aabb3d::from_point_cloud(isometry, [self.point1(), self.point2()].iter().copied()) } fn bounding_sphere(&self, isometry: impl Into) -> BoundingSphere { let isometry = isometry.into(); - BoundingSphere::new(isometry.translation, self.half_length) + BoundingSphere::from_point_cloud(isometry, &[self.point1(), self.point2()]) } } diff --git a/crates/bevy_math/src/primitives/dim3.rs b/crates/bevy_math/src/primitives/dim3.rs index 8d25df5b83d0a..eec54cae64806 100644 --- a/crates/bevy_math/src/primitives/dim3.rs +++ b/crates/bevy_math/src/primitives/dim3.rs @@ -359,22 +359,25 @@ impl Primitive3d for Line3d {} reflect(Serialize, Deserialize) )] pub struct Segment3d { - /// The direction of the line - pub direction: Dir3, - /// Half the length of the line segment. The segment extends by this amount in both - /// the given direction and its opposite direction - pub half_length: f32, + /// The first point of the segment + pub point1: Vec3, + /// The second point of the segment + pub point2: Vec3, } impl Primitive3d for Segment3d {} impl Segment3d { + /// Create a new `Segment3d` from it's points + #[inline(always)] + pub fn new(point1: Vec3, point2: Vec3) -> Self { + Self { point1, point2 } + } + /// Create a new `Segment3d` from a direction and full length of the segment #[inline(always)] - pub fn new(direction: Dir3, length: f32) -> Self { - Self { - direction, - half_length: length / 2.0, - } + pub fn from_direction_and_length(direction: Dir3, length: f32) -> Self { + let half_length = length / 2.; + Self::new(direction * -half_length, direction * -half_length) } /// Create a new `Segment3d` from its endpoints and compute its geometric center @@ -384,26 +387,59 @@ impl Segment3d { /// Panics if `point1 == point2` #[inline(always)] pub fn from_points(point1: Vec3, point2: Vec3) -> (Self, Vec3) { - let diff = point2 - point1; - let length = diff.length(); + let segment = Self::new(point1, point2); - ( - // We are dividing by the length here, so the vector is normalized. - Self::new(Dir3::new_unchecked(diff / length), length), - (point1 + point2) / 2., - ) + (segment, segment.center()) } /// Get the position of the first point on the line segment #[inline(always)] pub fn point1(&self) -> Vec3 { - *self.direction * -self.half_length + self.point1 } /// Get the position of the second point on the line segment #[inline(always)] pub fn point2(&self) -> Vec3 { - *self.direction * self.half_length + self.point2 + } + + /// Get the center of the segment + #[inline(always)] + pub fn center(&self) -> Vec3 { + (self.point1() + self.point2()) / 2. + } + + /// Get the length of the segment + #[inline(always)] + pub fn length(&self) -> f32 { + self.point1().distance(self.point2()) + } + + /// Get the segment offset by a vector + #[inline(always)] + pub fn offset(&self, offset: Vec3) -> Segment3d { + Self::new(self.point1() + offset, self.point2() + offset) + } + + /// Get the segment rotated around it's center + #[inline(always)] + pub fn rotated(&self, rotation: Quat) -> Segment3d { + // We center the segment for the purpose of the rotation, then offset back to it's original position + let offset_from_origin = self.center(); + let centered = self.centered(); + let centered_rotated = Segment3d::new( + rotation.mul_vec3(centered.point1()), + rotation.mul_vec3(centered.point2()), + ); + centered_rotated.offset(offset_from_origin) + } + + /// Get the segment offset so that it's center is at the origin + #[inline(always)] + pub fn centered(&self) -> Segment3d { + let center = self.center(); + self.offset(-center) } } From bd971f6c03d4e3e2ff937e5041daafc7baee7978 Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Thu, 16 Jan 2025 20:00:57 +0100 Subject: [PATCH 09/43] fix segment3d render primitive --- examples/math/render_primitives.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/math/render_primitives.rs b/examples/math/render_primitives.rs index 30dfbaf88efe7..4df5bf5261abe 100644 --- a/examples/math/render_primitives.rs +++ b/examples/math/render_primitives.rs @@ -192,8 +192,8 @@ const SEGMENT_2D: Segment2d = Segment2d { }; const SEGMENT_3D: Segment3d = Segment3d { - direction: Dir3::X, - half_length: BIG_3D, + point1: Vec3::new(-BIG_3D / 2., 0., 0.), + point2: Vec3::new(BIG_3D / 2., 0., 0.), }; const POLYLINE_2D: Polyline2d<4> = Polyline2d { From 334de9bf9f341aa80dbe072aa1a3c3f000702dca Mon Sep 17 00:00:00 2001 From: Sigma-dev <72008323+Sigma-dev@users.noreply.github.com> Date: Thu, 16 Jan 2025 23:12:23 +0100 Subject: [PATCH 10/43] Update crates/bevy_gizmos/src/primitives/dim3.rs Co-authored-by: Joona Aalto --- crates/bevy_gizmos/src/primitives/dim3.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_gizmos/src/primitives/dim3.rs b/crates/bevy_gizmos/src/primitives/dim3.rs index 20d6b28215aea..e7c0a11129b5e 100644 --- a/crates/bevy_gizmos/src/primitives/dim3.rs +++ b/crates/bevy_gizmos/src/primitives/dim3.rs @@ -232,7 +232,7 @@ where let transformed = primitive .rotated(isometry.rotation) .offset(isometry.translation.into()); - self.line(transformed.point1(), transformed.point1(), color); + self.line(transformed.point1(), transformed.point2(), color); } } From 42b9ef15a72802f31aa70559d9d6439f4df2d666 Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Fri, 17 Jan 2025 01:34:40 +0100 Subject: [PATCH 11/43] optimized bounding circle computation for segment2d --- crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs b/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs index 9386b9bffe0dc..83efddb281c6d 100644 --- a/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs +++ b/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs @@ -269,7 +269,11 @@ impl Bounded2d for Segment2d { } fn bounding_circle(&self, isometry: impl Into) -> BoundingCircle { - BoundingCircle::from_point_cloud(isometry, &[self.point1(), self.point2()]) + let isometry: Isometry2d = isometry.into(); + let local_center = self.center(); + let radius = local_center.distance(self.point1); + let local_circle = BoundingCircle::new(local_center, radius); + local_circle.transformed_by(isometry.translation, isometry.rotation) } } From c4602283d1de61e9d4cf977d6bb596448378d669 Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Fri, 17 Jan 2025 01:36:08 +0100 Subject: [PATCH 12/43] missing import --- crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs b/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs index 83efddb281c6d..1ede885611f73 100644 --- a/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs +++ b/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs @@ -1,6 +1,7 @@ //! Contains [`Bounded2d`] implementations for [geometric primitives](crate::primitives). use crate::{ + bounding::BoundingVolume, ops, primitives::{ Annulus, Arc2d, Capsule2d, Circle, CircularSector, CircularSegment, Ellipse, Line2d, From 02783a4c184b4cf9851a966cce57e8f725bb62c1 Mon Sep 17 00:00:00 2001 From: Sigma-dev <72008323+Sigma-dev@users.noreply.github.com> Date: Fri, 17 Jan 2025 01:37:22 +0100 Subject: [PATCH 13/43] Update crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs Co-authored-by: Joona Aalto --- crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs b/crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs index cb5412afec673..104b6ec044185 100644 --- a/crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs +++ b/crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs @@ -76,7 +76,7 @@ impl Bounded3d for Line3d { impl Bounded3d for Segment3d { fn aabb_3d(&self, isometry: impl Into) -> Aabb3d { - Aabb3d::from_point_cloud(isometry, [self.point1(), self.point2()].iter().copied()) + Aabb3d::from_point_cloud(isometry, &[self.point1(), self.point2()]) } fn bounding_sphere(&self, isometry: impl Into) -> BoundingSphere { From c0418c68ddb19181015d65d05543299498eebfd6 Mon Sep 17 00:00:00 2001 From: Sigma-dev <72008323+Sigma-dev@users.noreply.github.com> Date: Fri, 17 Jan 2025 01:43:20 +0100 Subject: [PATCH 14/43] Update crates/bevy_math/src/primitives/dim2.rs Co-authored-by: Joona Aalto --- crates/bevy_math/src/primitives/dim2.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_math/src/primitives/dim2.rs b/crates/bevy_math/src/primitives/dim2.rs index 9cae9fdcd4d3a..36ac713bc4e62 100644 --- a/crates/bevy_math/src/primitives/dim2.rs +++ b/crates/bevy_math/src/primitives/dim2.rs @@ -1221,9 +1221,9 @@ impl Primitive2d for Line2d {} )] #[doc(alias = "LineSegment2d")] pub struct Segment2d { - /// First point of the segment + /// The first point of the line segment pub point1: Vec2, - /// Second point of the segment + /// The second point of the line segment pub point2: Vec2, } impl Primitive2d for Segment2d {} From c7f1e2a40084d41bb54f6ceab2c4f5e629eb7b0f Mon Sep 17 00:00:00 2001 From: Sigma-dev <72008323+Sigma-dev@users.noreply.github.com> Date: Fri, 17 Jan 2025 01:43:57 +0100 Subject: [PATCH 15/43] Update crates/bevy_math/src/primitives/dim3.rs Co-authored-by: Joona Aalto --- crates/bevy_math/src/primitives/dim3.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_math/src/primitives/dim3.rs b/crates/bevy_math/src/primitives/dim3.rs index eec54cae64806..b679e426d85c0 100644 --- a/crates/bevy_math/src/primitives/dim3.rs +++ b/crates/bevy_math/src/primitives/dim3.rs @@ -377,7 +377,7 @@ impl Segment3d { #[inline(always)] pub fn from_direction_and_length(direction: Dir3, length: f32) -> Self { let half_length = length / 2.; - Self::new(direction * -half_length, direction * -half_length) + Self::new(direction * -half_length, direction * half_length) } /// Create a new `Segment3d` from its endpoints and compute its geometric center From f976e996042ac9a18b88511f4e5ed797e5266ff9 Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Fri, 17 Jan 2025 01:45:49 +0100 Subject: [PATCH 16/43] revert example change --- examples/2d/bounding_2d.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/2d/bounding_2d.rs b/examples/2d/bounding_2d.rs index 0b8017e0487f0..a90718058c214 100644 --- a/examples/2d/bounding_2d.rs +++ b/examples/2d/bounding_2d.rs @@ -224,7 +224,7 @@ fn setup(mut commands: Commands) { Vec2::new(40., 50.), )), Spin, - DesiredVolume::Circle, + DesiredVolume::Aabb, Intersects::default(), )); From b9ce95424ba91702beb42350e9a852dd95081bf4 Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Fri, 17 Jan 2025 01:48:16 +0100 Subject: [PATCH 17/43] deprecated from_points of Segment3d --- crates/bevy_math/src/primitives/dim3.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/bevy_math/src/primitives/dim3.rs b/crates/bevy_math/src/primitives/dim3.rs index b679e426d85c0..f03df4177b86d 100644 --- a/crates/bevy_math/src/primitives/dim3.rs +++ b/crates/bevy_math/src/primitives/dim3.rs @@ -386,6 +386,7 @@ impl Segment3d { /// /// Panics if `point1 == point2` #[inline(always)] + #[deprecated(since = "0.15.1", note = "Use the `new` constructor instead")] pub fn from_points(point1: Vec3, point2: Vec3) -> (Self, Vec3) { let segment = Self::new(point1, point2); From 3da4e4607ac3bd29113e9a7b0959eec024be0292 Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Fri, 17 Jan 2025 01:49:48 +0100 Subject: [PATCH 18/43] unified segment2d and 3d from_points implementation --- crates/bevy_math/src/primitives/dim3.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/bevy_math/src/primitives/dim3.rs b/crates/bevy_math/src/primitives/dim3.rs index f03df4177b86d..ae55671112e87 100644 --- a/crates/bevy_math/src/primitives/dim3.rs +++ b/crates/bevy_math/src/primitives/dim3.rs @@ -388,9 +388,7 @@ impl Segment3d { #[inline(always)] #[deprecated(since = "0.15.1", note = "Use the `new` constructor instead")] pub fn from_points(point1: Vec3, point2: Vec3) -> (Self, Vec3) { - let segment = Self::new(point1, point2); - - (segment, segment.center()) + (Self::new(point1, point2), (point1 + point2) / 2.) } /// Get the position of the first point on the line segment From a5e6cad18640c0cc9bc42e84f846a56d87c0cb57 Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Fri, 17 Jan 2025 02:03:13 +0100 Subject: [PATCH 19/43] revert change --- crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs b/crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs index 104b6ec044185..cb5412afec673 100644 --- a/crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs +++ b/crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs @@ -76,7 +76,7 @@ impl Bounded3d for Line3d { impl Bounded3d for Segment3d { fn aabb_3d(&self, isometry: impl Into) -> Aabb3d { - Aabb3d::from_point_cloud(isometry, &[self.point1(), self.point2()]) + Aabb3d::from_point_cloud(isometry, [self.point1(), self.point2()].iter().copied()) } fn bounding_sphere(&self, isometry: impl Into) -> BoundingSphere { From 7ec709f8e88f92d3c216c2b6de121ce4696ee6c5 Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Fri, 17 Jan 2025 02:04:52 +0100 Subject: [PATCH 20/43] optimized segment3d bounding sphere generation --- crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs b/crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs index cb5412afec673..6f839547e78ff 100644 --- a/crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs +++ b/crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs @@ -1,7 +1,7 @@ //! Contains [`Bounded3d`] implementations for [geometric primitives](crate::primitives). use crate::{ - bounding::{Bounded2d, BoundingCircle}, + bounding::{Bounded2d, BoundingCircle, BoundingVolume}, ops, primitives::{ Capsule3d, Cone, ConicalFrustum, Cuboid, Cylinder, InfinitePlane3d, Line3d, Polyline3d, @@ -81,7 +81,8 @@ impl Bounded3d for Segment3d { fn bounding_sphere(&self, isometry: impl Into) -> BoundingSphere { let isometry = isometry.into(); - BoundingSphere::from_point_cloud(isometry, &[self.point1(), self.point2()]) + let local_sphere = BoundingSphere::new(self.center(), self.length() / 2.); + local_sphere.transformed_by(isometry.translation, isometry.rotation) } } From 2ce9647680040a8c35a771f0078c241480984548 Mon Sep 17 00:00:00 2001 From: Sigma-dev <72008323+Sigma-dev@users.noreply.github.com> Date: Fri, 17 Jan 2025 02:05:55 +0100 Subject: [PATCH 21/43] Update crates/bevy_math/src/primitives/dim2.rs Co-authored-by: Joona Aalto --- crates/bevy_math/src/primitives/dim2.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_math/src/primitives/dim2.rs b/crates/bevy_math/src/primitives/dim2.rs index 36ac713bc4e62..b411c5162671c 100644 --- a/crates/bevy_math/src/primitives/dim2.rs +++ b/crates/bevy_math/src/primitives/dim2.rs @@ -1229,7 +1229,7 @@ pub struct Segment2d { impl Primitive2d for Segment2d {} impl Segment2d { - /// Create a new `Segment2d` from the two points composing it + /// Create a new `Segment2d` from its endpoints #[inline(always)] pub fn new(point1: Vec2, point2: Vec2) -> Self { Self { point1, point2 } From 070f9e86be6a9611a485b82edf6c28a11bbe30cc Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Fri, 17 Jan 2025 02:07:21 +0100 Subject: [PATCH 22/43] fix deprecation version number --- crates/bevy_math/src/primitives/dim2.rs | 2 +- crates/bevy_math/src/primitives/dim3.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_math/src/primitives/dim2.rs b/crates/bevy_math/src/primitives/dim2.rs index 36ac713bc4e62..cec5949e44c81 100644 --- a/crates/bevy_math/src/primitives/dim2.rs +++ b/crates/bevy_math/src/primitives/dim2.rs @@ -1241,7 +1241,7 @@ impl Segment2d { /// /// Panics if `point1 == point2` #[inline(always)] - #[deprecated(since = "0.15.1", note = "Use the `new` constructor instead")] + #[deprecated(since = "0.16", note = "Use the `new` constructor instead")] pub fn from_points(point1: Vec2, point2: Vec2) -> (Self, Vec2) { (Self::new(point1, point2), (point1 + point2) / 2.) } diff --git a/crates/bevy_math/src/primitives/dim3.rs b/crates/bevy_math/src/primitives/dim3.rs index ae55671112e87..8570f2feb25e4 100644 --- a/crates/bevy_math/src/primitives/dim3.rs +++ b/crates/bevy_math/src/primitives/dim3.rs @@ -386,7 +386,7 @@ impl Segment3d { /// /// Panics if `point1 == point2` #[inline(always)] - #[deprecated(since = "0.15.1", note = "Use the `new` constructor instead")] + #[deprecated(since = "0.16", note = "Use the `new` constructor instead")] pub fn from_points(point1: Vec3, point2: Vec3) -> (Self, Vec3) { (Self::new(point1, point2), (point1 + point2) / 2.) } From c00a6ee27cd5fb9631d5cafa28c69a12bc1b0077 Mon Sep 17 00:00:00 2001 From: Sigma-dev <72008323+Sigma-dev@users.noreply.github.com> Date: Fri, 17 Jan 2025 02:07:31 +0100 Subject: [PATCH 23/43] Update crates/bevy_math/src/primitives/dim2.rs Co-authored-by: Joona Aalto --- crates/bevy_math/src/primitives/dim2.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_math/src/primitives/dim2.rs b/crates/bevy_math/src/primitives/dim2.rs index b411c5162671c..ba1a53e792045 100644 --- a/crates/bevy_math/src/primitives/dim2.rs +++ b/crates/bevy_math/src/primitives/dim2.rs @@ -1231,7 +1231,7 @@ impl Primitive2d for Segment2d {} impl Segment2d { /// Create a new `Segment2d` from its endpoints #[inline(always)] - pub fn new(point1: Vec2, point2: Vec2) -> Self { + pub const fn new(point1: Vec2, point2: Vec2) -> Self { Self { point1, point2 } } From f1686ca98710b474ea84918ed697635d514e6f3f Mon Sep 17 00:00:00 2001 From: Sigma-dev <72008323+Sigma-dev@users.noreply.github.com> Date: Fri, 17 Jan 2025 02:09:56 +0100 Subject: [PATCH 24/43] Update crates/bevy_math/src/primitives/dim2.rs Co-authored-by: Joona Aalto --- crates/bevy_math/src/primitives/dim2.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/bevy_math/src/primitives/dim2.rs b/crates/bevy_math/src/primitives/dim2.rs index a0818e7d9dfea..f13aabd308292 100644 --- a/crates/bevy_math/src/primitives/dim2.rs +++ b/crates/bevy_math/src/primitives/dim2.rs @@ -1277,10 +1277,10 @@ impl Segment2d { self.point1().distance(self.point2()) } - /// Get the segment offset by a vector + /// Get the segment translated by the given vector #[inline(always)] - pub fn offset(&self, offset: Vec2) -> Segment2d { - Self::new(self.point1() + offset, self.point2() + offset) + pub fn translated(&self, translation: Vec2) -> Segment2d { + Self::new(self.point1() + translation, self.point2() + translation) } /// Compute a new segment, based on the original segment rotated around its center From 8b259491a5c0f254d8ccac706c6635eeb497194e Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Fri, 17 Jan 2025 02:13:17 +0100 Subject: [PATCH 25/43] apply translated rename --- crates/bevy_math/src/primitives/dim2.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/bevy_math/src/primitives/dim2.rs b/crates/bevy_math/src/primitives/dim2.rs index f13aabd308292..d3d0601663239 100644 --- a/crates/bevy_math/src/primitives/dim2.rs +++ b/crates/bevy_math/src/primitives/dim2.rs @@ -1299,14 +1299,14 @@ impl Segment2d { rotate_point(centered.point1(), rotation), rotate_point(centered.point2(), rotation), ); - centered_rotated.offset(offset_from_origin) + centered_rotated.translated(offset_from_origin) } /// Get the segment with it's center is at the origin #[inline(always)] pub fn centered(&self) -> Segment2d { let center = self.center(); - self.offset(-center) + self.translated(-center) } /// Get the segment with a new length @@ -1316,7 +1316,7 @@ impl Segment2d { let centered = self.centered(); let ratio = length / self.length(); let segment = Segment2d::new(centered.point1() * ratio, centered.point2() * ratio); - segment.offset(offset_from_origin) + segment.translated(offset_from_origin) } } From 2988fd0ff3a6a0d6eaeeca2474e3a01a4efb9261 Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Fri, 17 Jan 2025 02:25:35 +0100 Subject: [PATCH 26/43] more renaming translated --- crates/bevy_gizmos/src/primitives/dim2.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_gizmos/src/primitives/dim2.rs b/crates/bevy_gizmos/src/primitives/dim2.rs index 26dd7aa302edb..5991feb5e1516 100644 --- a/crates/bevy_gizmos/src/primitives/dim2.rs +++ b/crates/bevy_gizmos/src/primitives/dim2.rs @@ -635,7 +635,7 @@ where } let segment = Segment2d::new(self.point1, self.point2) - .offset(self.isometry.translation) + .translated(self.isometry.translation) .rotated(self.isometry.rotation); if self.draw_arrow { From c9431c9c507e5d0b098a6e06acc48360abed9b5a Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Fri, 17 Jan 2025 02:25:49 +0100 Subject: [PATCH 27/43] renaming and adding new rotation methods --- crates/bevy_gizmos/src/primitives/dim2.rs | 2 +- crates/bevy_math/src/primitives/dim2.rs | 29 ++++++++++++++++------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/crates/bevy_gizmos/src/primitives/dim2.rs b/crates/bevy_gizmos/src/primitives/dim2.rs index 5991feb5e1516..515a1ad2bb2c0 100644 --- a/crates/bevy_gizmos/src/primitives/dim2.rs +++ b/crates/bevy_gizmos/src/primitives/dim2.rs @@ -636,7 +636,7 @@ where let segment = Segment2d::new(self.point1, self.point2) .translated(self.isometry.translation) - .rotated(self.isometry.rotation); + .rotated_around_center(self.isometry.rotation); if self.draw_arrow { self.gizmos diff --git a/crates/bevy_math/src/primitives/dim2.rs b/crates/bevy_math/src/primitives/dim2.rs index d3d0601663239..37dcf3ee81c6f 100644 --- a/crates/bevy_math/src/primitives/dim2.rs +++ b/crates/bevy_math/src/primitives/dim2.rs @@ -1283,7 +1283,7 @@ impl Segment2d { Self::new(self.point1() + translation, self.point2() + translation) } - /// Compute a new segment, based on the original segment rotated around its center + /// Compute a new segment, based on the original segment rotated around the origin #[inline(always)] pub fn rotated(&self, rotation: Rot2) -> Segment2d { pub fn rotate_point(p: Vec2, rotation: Rot2) -> Vec2 { @@ -1292,14 +1292,25 @@ impl Segment2d { p.x * rotation.sin + p.y * rotation.cos, ) } - // We center the segment for the purpose of the rotation, then offset back to it's original position - let offset_from_origin = self.center(); - let centered = self.centered(); - let centered_rotated: Segment2d = Segment2d::new( - rotate_point(centered.point1(), rotation), - rotate_point(centered.point2(), rotation), - ); - centered_rotated.translated(offset_from_origin) + Segment2d::new( + rotate_point(self.point1(), rotation), + rotate_point(self.point2(), rotation), + ) + } + + /// Compute a new segment, based on the original segment rotated around a given point + #[inline(always)] + pub fn rotated_around(&self, rotation: Rot2, point: Vec2) -> Segment2d { + // We offset our segment so that our segment is rotated as if from the origin, then we can apply the offset back + let offset = self.translated(-point); + let rotated = offset.rotated(rotation); + rotated.translated(point) + } + + /// Compute a new segment, based on the original segment rotated around its center + #[inline(always)] + pub fn rotated_around_center(&self, rotation: Rot2) -> Segment2d { + self.rotated_around(rotation, self.center()) } /// Get the segment with it's center is at the origin From ff8bd4c2880840ee40a0e4e8b0357f707cf6c09d Mon Sep 17 00:00:00 2001 From: Sigma-dev <72008323+Sigma-dev@users.noreply.github.com> Date: Fri, 17 Jan 2025 02:27:21 +0100 Subject: [PATCH 28/43] Update crates/bevy_math/src/primitives/dim2.rs Co-authored-by: Joona Aalto --- crates/bevy_math/src/primitives/dim2.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_math/src/primitives/dim2.rs b/crates/bevy_math/src/primitives/dim2.rs index 37dcf3ee81c6f..b4065a6fdcd4d 100644 --- a/crates/bevy_math/src/primitives/dim2.rs +++ b/crates/bevy_math/src/primitives/dim2.rs @@ -1313,7 +1313,7 @@ impl Segment2d { self.rotated_around(rotation, self.center()) } - /// Get the segment with it's center is at the origin + /// Get the segment with its center at the origin #[inline(always)] pub fn centered(&self) -> Segment2d { let center = self.center(); From 7a779350066af5fa9f879c8f62347a3f48bbb145 Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Fri, 17 Jan 2025 02:36:41 +0100 Subject: [PATCH 29/43] improve segment3d methods --- crates/bevy_gizmos/src/primitives/dim3.rs | 4 +-- crates/bevy_math/src/primitives/dim3.rs | 35 +++++++++++++++-------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/crates/bevy_gizmos/src/primitives/dim3.rs b/crates/bevy_gizmos/src/primitives/dim3.rs index e7c0a11129b5e..9d0657f09bd47 100644 --- a/crates/bevy_gizmos/src/primitives/dim3.rs +++ b/crates/bevy_gizmos/src/primitives/dim3.rs @@ -230,8 +230,8 @@ where let isometry: Isometry3d = isometry.into(); let transformed = primitive - .rotated(isometry.rotation) - .offset(isometry.translation.into()); + .translated(isometry.translation.into()) + .rotated_around_center(isometry.rotation); self.line(transformed.point1(), transformed.point2(), color); } } diff --git a/crates/bevy_math/src/primitives/dim3.rs b/crates/bevy_math/src/primitives/dim3.rs index 8570f2feb25e4..4064fefea582c 100644 --- a/crates/bevy_math/src/primitives/dim3.rs +++ b/crates/bevy_math/src/primitives/dim3.rs @@ -415,30 +415,41 @@ impl Segment3d { self.point1().distance(self.point2()) } - /// Get the segment offset by a vector + /// Get the segment translated by a vector #[inline(always)] - pub fn offset(&self, offset: Vec3) -> Segment3d { - Self::new(self.point1() + offset, self.point2() + offset) + pub fn translated(&self, translation: Vec3) -> Segment3d { + Self::new(self.point1() + translation, self.point2() + translation) } /// Get the segment rotated around it's center #[inline(always)] pub fn rotated(&self, rotation: Quat) -> Segment3d { - // We center the segment for the purpose of the rotation, then offset back to it's original position - let offset_from_origin = self.center(); - let centered = self.centered(); - let centered_rotated = Segment3d::new( - rotation.mul_vec3(centered.point1()), - rotation.mul_vec3(centered.point2()), - ); - centered_rotated.offset(offset_from_origin) + Segment3d::new( + rotation.mul_vec3(self.point1()), + rotation.mul_vec3(self.point2()), + ) + } + + /// Compute a new segment, based on the original segment rotated around a given point + #[inline(always)] + pub fn rotated_around(&self, rotation: Quat, point: Vec3) -> Segment3d { + // We offset our segment so that our segment is rotated as if from the origin, then we can apply the offset back + let offset = self.translated(-point); + let rotated = offset.rotated(rotation); + rotated.translated(point) + } + + /// Compute a new segment, based on the original segment rotated around its center + #[inline(always)] + pub fn rotated_around_center(&self, rotation: Quat) -> Segment3d { + self.rotated_around(rotation, self.center()) } /// Get the segment offset so that it's center is at the origin #[inline(always)] pub fn centered(&self) -> Segment3d { let center = self.center(); - self.offset(-center) + self.translated(-center) } } From 7804e7ee8b832c5681d0d98e4d9a2b4a4d43b4c0 Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Fri, 17 Jan 2025 02:41:06 +0100 Subject: [PATCH 30/43] removed deprecated method call --- crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs b/crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs index 6f839547e78ff..1f4e2a1666d17 100644 --- a/crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs +++ b/crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs @@ -459,8 +459,7 @@ mod tests { fn segment() { let translation = Vec3::new(2.0, 1.0, 0.0); - let segment = - Segment3d::from_points(Vec3::new(-1.0, -0.5, 0.0), Vec3::new(1.0, 0.5, 0.0)).0; + let segment = Segment3d::new(Vec3::new(-1.0, -0.5, 0.0), Vec3::new(1.0, 0.5, 0.0)); let aabb = segment.aabb_3d(translation); assert_eq!(aabb.min, Vec3A::new(1.0, 0.5, 0.0)); From 681f4d3b1e66e6c2f8e9b5a186227fbe5dd701f1 Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Fri, 17 Jan 2025 20:59:50 +0100 Subject: [PATCH 31/43] added midpoint alias --- crates/bevy_math/src/primitives/dim2.rs | 1 + crates/bevy_math/src/primitives/dim3.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/crates/bevy_math/src/primitives/dim2.rs b/crates/bevy_math/src/primitives/dim2.rs index b4065a6fdcd4d..55255cb43aded 100644 --- a/crates/bevy_math/src/primitives/dim2.rs +++ b/crates/bevy_math/src/primitives/dim2.rs @@ -1267,6 +1267,7 @@ impl Segment2d { /// Get the segment's center #[inline(always)] + #[doc(alias = "midpoint")] pub fn center(&self) -> Vec2 { (self.point1() + self.point2()) / 2. } diff --git a/crates/bevy_math/src/primitives/dim3.rs b/crates/bevy_math/src/primitives/dim3.rs index 4064fefea582c..02c51a2d4e852 100644 --- a/crates/bevy_math/src/primitives/dim3.rs +++ b/crates/bevy_math/src/primitives/dim3.rs @@ -405,6 +405,7 @@ impl Segment3d { /// Get the center of the segment #[inline(always)] + #[doc(alias = "midpoint")] pub fn center(&self) -> Vec3 { (self.point1() + self.point2()) / 2. } From fdabe19a82758a5eab44a0958f30af52dd0a84bd Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Fri, 17 Jan 2025 21:02:18 +0100 Subject: [PATCH 32/43] changed internal segment representation to an array --- crates/bevy_math/src/primitives/dim2.rs | 14 +++++++------- crates/bevy_math/src/primitives/dim3.rs | 14 +++++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/crates/bevy_math/src/primitives/dim2.rs b/crates/bevy_math/src/primitives/dim2.rs index 55255cb43aded..86342b8079786 100644 --- a/crates/bevy_math/src/primitives/dim2.rs +++ b/crates/bevy_math/src/primitives/dim2.rs @@ -1221,10 +1221,8 @@ impl Primitive2d for Line2d {} )] #[doc(alias = "LineSegment2d")] pub struct Segment2d { - /// The first point of the line segment - pub point1: Vec2, - /// The second point of the line segment - pub point2: Vec2, + /// The endpoints of the line segment. + pub vertices: [Vec2; 2], } impl Primitive2d for Segment2d {} @@ -1232,7 +1230,9 @@ impl Segment2d { /// Create a new `Segment2d` from its endpoints #[inline(always)] pub const fn new(point1: Vec2, point2: Vec2) -> Self { - Self { point1, point2 } + Self { + vertices: [point1, point2], + } } /// Create a new `Segment2d` from its endpoints and compute its geometric center @@ -1256,13 +1256,13 @@ impl Segment2d { /// Get the position of the first point on the line segment #[inline(always)] pub fn point1(&self) -> Vec2 { - self.point1 + self.vertices[0] } /// Get the position of the second point on the line segment #[inline(always)] pub fn point2(&self) -> Vec2 { - self.point2 + self.vertices[1] } /// Get the segment's center diff --git a/crates/bevy_math/src/primitives/dim3.rs b/crates/bevy_math/src/primitives/dim3.rs index 02c51a2d4e852..1f8550f308560 100644 --- a/crates/bevy_math/src/primitives/dim3.rs +++ b/crates/bevy_math/src/primitives/dim3.rs @@ -359,10 +359,8 @@ impl Primitive3d for Line3d {} reflect(Serialize, Deserialize) )] pub struct Segment3d { - /// The first point of the segment - pub point1: Vec3, - /// The second point of the segment - pub point2: Vec3, + /// The endpoints of the line segment. + pub vertices: [Vec3; 2], } impl Primitive3d for Segment3d {} @@ -370,7 +368,9 @@ impl Segment3d { /// Create a new `Segment3d` from it's points #[inline(always)] pub fn new(point1: Vec3, point2: Vec3) -> Self { - Self { point1, point2 } + Self { + vertices: [point1, point2], + } } /// Create a new `Segment3d` from a direction and full length of the segment @@ -394,13 +394,13 @@ impl Segment3d { /// Get the position of the first point on the line segment #[inline(always)] pub fn point1(&self) -> Vec3 { - self.point1 + self.vertices[0] } /// Get the position of the second point on the line segment #[inline(always)] pub fn point2(&self) -> Vec3 { - self.point2 + self.vertices[1] } /// Get the center of the segment From 1fd5de3205c3087cf6dc59f32d51c125ca1e3be1 Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Fri, 17 Jan 2025 21:05:29 +0100 Subject: [PATCH 33/43] implement representation changes --- crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs b/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs index 1ede885611f73..ba222eb69631b 100644 --- a/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs +++ b/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs @@ -272,7 +272,7 @@ impl Bounded2d for Segment2d { fn bounding_circle(&self, isometry: impl Into) -> BoundingCircle { let isometry: Isometry2d = isometry.into(); let local_center = self.center(); - let radius = local_center.distance(self.point1); + let radius = local_center.distance(self.point1()); let local_circle = BoundingCircle::new(local_center, radius); local_circle.transformed_by(isometry.translation, isometry.rotation) } From dbe951fee67e8a47556b8a546390c3d4bf7524f8 Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Fri, 17 Jan 2025 22:57:57 +0100 Subject: [PATCH 34/43] fixed version --- crates/bevy_math/src/primitives/dim2.rs | 2 +- crates/bevy_math/src/primitives/dim3.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_math/src/primitives/dim2.rs b/crates/bevy_math/src/primitives/dim2.rs index 86342b8079786..ca47535866534 100644 --- a/crates/bevy_math/src/primitives/dim2.rs +++ b/crates/bevy_math/src/primitives/dim2.rs @@ -1241,7 +1241,7 @@ impl Segment2d { /// /// Panics if `point1 == point2` #[inline(always)] - #[deprecated(since = "0.16", note = "Use the `new` constructor instead")] + #[deprecated(since = "0.16.0", note = "Use the `new` constructor instead")] pub fn from_points(point1: Vec2, point2: Vec2) -> (Self, Vec2) { (Self::new(point1, point2), (point1 + point2) / 2.) } diff --git a/crates/bevy_math/src/primitives/dim3.rs b/crates/bevy_math/src/primitives/dim3.rs index 1f8550f308560..51a04db39c387 100644 --- a/crates/bevy_math/src/primitives/dim3.rs +++ b/crates/bevy_math/src/primitives/dim3.rs @@ -386,7 +386,7 @@ impl Segment3d { /// /// Panics if `point1 == point2` #[inline(always)] - #[deprecated(since = "0.16", note = "Use the `new` constructor instead")] + #[deprecated(since = "0.16.0", note = "Use the `new` constructor instead")] pub fn from_points(point1: Vec3, point2: Vec3) -> (Self, Vec3) { (Self::new(point1, point2), (point1 + point2) / 2.) } From fd63766a361e99d4c984fc73b672e645c0fd2306 Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Sat, 18 Jan 2025 01:06:10 +0100 Subject: [PATCH 35/43] fixed internal change --- examples/math/render_primitives.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/math/render_primitives.rs b/examples/math/render_primitives.rs index 4df5bf5261abe..4332516ac27f5 100644 --- a/examples/math/render_primitives.rs +++ b/examples/math/render_primitives.rs @@ -187,13 +187,14 @@ const LINE2D: Line2d = Line2d { direction: Dir2::X }; const LINE3D: Line3d = Line3d { direction: Dir3::X }; const SEGMENT_2D: Segment2d = Segment2d { - point1: Vec2::new(-BIG_2D / 2., 0.), - point2: Vec2::new(BIG_2D / 2., 0.), + vertices: [Vec2::new(-BIG_2D / 2., 0.), Vec2::new(BIG_2D / 2., 0.)], }; const SEGMENT_3D: Segment3d = Segment3d { - point1: Vec3::new(-BIG_3D / 2., 0., 0.), - point2: Vec3::new(BIG_3D / 2., 0., 0.), + vertices: [ + Vec3::new(-BIG_3D / 2., 0., 0.), + Vec3::new(BIG_3D / 2., 0., 0.), + ], }; const POLYLINE_2D: Polyline2d<4> = Polyline2d { From 1721259d660a588b6c137c58a188a58ead62d6bb Mon Sep 17 00:00:00 2001 From: Sigma-dev <72008323+Sigma-dev@users.noreply.github.com> Date: Sat, 18 Jan 2025 15:32:22 +0100 Subject: [PATCH 36/43] Update crates/bevy_math/src/primitives/dim3.rs Co-authored-by: Joona Aalto --- crates/bevy_math/src/primitives/dim3.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_math/src/primitives/dim3.rs b/crates/bevy_math/src/primitives/dim3.rs index 51a04db39c387..fe51c2a4fe662 100644 --- a/crates/bevy_math/src/primitives/dim3.rs +++ b/crates/bevy_math/src/primitives/dim3.rs @@ -365,7 +365,7 @@ pub struct Segment3d { impl Primitive3d for Segment3d {} impl Segment3d { - /// Create a new `Segment3d` from it's points + /// Create a new `Segment3d` from its endpoints #[inline(always)] pub fn new(point1: Vec3, point2: Vec3) -> Self { Self { From 35c0f3c221ace6fba49696a8ce2e5de3680da9e7 Mon Sep 17 00:00:00 2001 From: Sigma-dev <72008323+Sigma-dev@users.noreply.github.com> Date: Sat, 18 Jan 2025 15:32:31 +0100 Subject: [PATCH 37/43] Update crates/bevy_math/src/primitives/dim3.rs Co-authored-by: Joona Aalto --- crates/bevy_math/src/primitives/dim3.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_math/src/primitives/dim3.rs b/crates/bevy_math/src/primitives/dim3.rs index fe51c2a4fe662..6d05cba361d95 100644 --- a/crates/bevy_math/src/primitives/dim3.rs +++ b/crates/bevy_math/src/primitives/dim3.rs @@ -367,7 +367,7 @@ impl Primitive3d for Segment3d {} impl Segment3d { /// Create a new `Segment3d` from its endpoints #[inline(always)] - pub fn new(point1: Vec3, point2: Vec3) -> Self { + pub const fn new(point1: Vec3, point2: Vec3) -> Self { Self { vertices: [point1, point2], } From 00cfaa0aadcbef89b13448f05b2415f49e892604 Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Sat, 18 Jan 2025 15:34:13 +0100 Subject: [PATCH 38/43] added resized method to segment3d --- crates/bevy_math/src/primitives/dim3.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/bevy_math/src/primitives/dim3.rs b/crates/bevy_math/src/primitives/dim3.rs index 6d05cba361d95..03a6bfa227df2 100644 --- a/crates/bevy_math/src/primitives/dim3.rs +++ b/crates/bevy_math/src/primitives/dim3.rs @@ -452,6 +452,16 @@ impl Segment3d { let center = self.center(); self.translated(-center) } + + /// Get the segment with a new length + #[inline(always)] + pub fn resized(&self, length: f32) -> Segment3d { + let offset_from_origin = self.center(); + let centered = self.centered(); + let ratio = length / self.length(); + let segment = Segment3d::new(centered.point1() * ratio, centered.point2() * ratio); + segment.translated(offset_from_origin) + } } /// A series of connected line segments in 3D space. From 2fc35ba961664b82ecbf76fa2d55cd0c198759e3 Mon Sep 17 00:00:00 2001 From: Sigma-dev Date: Sat, 18 Jan 2025 15:44:29 +0100 Subject: [PATCH 39/43] fixed segment2d bounding circle --- crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs b/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs index ba222eb69631b..e1fe6afd774d5 100644 --- a/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs +++ b/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs @@ -334,9 +334,8 @@ impl Bounded2d for Triangle2d { if let Some((point1, point2)) = side_opposite_to_non_acute { // The triangle is obtuse or right, so the minimum bounding circle's diameter is equal to the longest side. // We can compute the minimum bounding circle from the line segment of the longest side. - let segment = Segment2d::new(point1, point2).centered(); - let center = (point1 + point2) / 2.; - segment.bounding_circle(isometry * Isometry2d::from_translation(center)) + let segment = Segment2d::new(point1, point2); + segment.bounding_circle(isometry) } else { // The triangle is acute, so the smallest bounding circle is the circumcircle. let (Circle { radius }, circumcenter) = self.circumcircle(); From 6f6e781e0c204be393446da5872b53e8af264e2f Mon Sep 17 00:00:00 2001 From: Sigma-dev <72008323+Sigma-dev@users.noreply.github.com> Date: Sat, 18 Jan 2025 16:22:52 +0100 Subject: [PATCH 40/43] Update crates/bevy_gizmos/src/primitives/dim3.rs Co-authored-by: Joona Aalto --- crates/bevy_gizmos/src/primitives/dim3.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_gizmos/src/primitives/dim3.rs b/crates/bevy_gizmos/src/primitives/dim3.rs index 9d0657f09bd47..31a2274e841b4 100644 --- a/crates/bevy_gizmos/src/primitives/dim3.rs +++ b/crates/bevy_gizmos/src/primitives/dim3.rs @@ -230,8 +230,8 @@ where let isometry: Isometry3d = isometry.into(); let transformed = primitive - .translated(isometry.translation.into()) - .rotated_around_center(isometry.rotation); + .rotated(isometry.rotation) + .translated(isometry.translation.into()); self.line(transformed.point1(), transformed.point2(), color); } } From eccfd2b205102c17e71f4241d9cfe4e848809086 Mon Sep 17 00:00:00 2001 From: Sigma-dev <72008323+Sigma-dev@users.noreply.github.com> Date: Sat, 18 Jan 2025 16:23:24 +0100 Subject: [PATCH 41/43] Update crates/bevy_math/src/primitives/dim3.rs Co-authored-by: Joona Aalto --- crates/bevy_math/src/primitives/dim3.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_math/src/primitives/dim3.rs b/crates/bevy_math/src/primitives/dim3.rs index 03a6bfa227df2..a828fa8247587 100644 --- a/crates/bevy_math/src/primitives/dim3.rs +++ b/crates/bevy_math/src/primitives/dim3.rs @@ -422,7 +422,7 @@ impl Segment3d { Self::new(self.point1() + translation, self.point2() + translation) } - /// Get the segment rotated around it's center + /// Compute a new segment, based on the original segment rotated around the origin #[inline(always)] pub fn rotated(&self, rotation: Quat) -> Segment3d { Segment3d::new( From af65238f54b52f9f72f1f5e188dc75f276ef95f4 Mon Sep 17 00:00:00 2001 From: Sigma-dev <72008323+Sigma-dev@users.noreply.github.com> Date: Sat, 18 Jan 2025 16:24:45 +0100 Subject: [PATCH 42/43] Update crates/bevy_math/src/primitives/dim2.rs Co-authored-by: Joona Aalto --- crates/bevy_math/src/primitives/dim2.rs | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/crates/bevy_math/src/primitives/dim2.rs b/crates/bevy_math/src/primitives/dim2.rs index ca47535866534..c336edac45a76 100644 --- a/crates/bevy_math/src/primitives/dim2.rs +++ b/crates/bevy_math/src/primitives/dim2.rs @@ -1287,16 +1287,7 @@ impl Segment2d { /// Compute a new segment, based on the original segment rotated around the origin #[inline(always)] pub fn rotated(&self, rotation: Rot2) -> Segment2d { - pub fn rotate_point(p: Vec2, rotation: Rot2) -> Vec2 { - Vec2::new( - p.x * rotation.cos - p.y * rotation.sin, - p.x * rotation.sin + p.y * rotation.cos, - ) - } - Segment2d::new( - rotate_point(self.point1(), rotation), - rotate_point(self.point2(), rotation), - ) + Segment2d::new(rotation * self.point1(), rotation * self.point2()) } /// Compute a new segment, based on the original segment rotated around a given point From 58d62438c57d54fb9d137f06efdf08f18efa61ea Mon Sep 17 00:00:00 2001 From: Sigma-dev <72008323+Sigma-dev@users.noreply.github.com> Date: Sat, 18 Jan 2025 16:25:27 +0100 Subject: [PATCH 43/43] Update crates/bevy_gizmos/src/primitives/dim2.rs Co-authored-by: Joona Aalto --- crates/bevy_gizmos/src/primitives/dim2.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_gizmos/src/primitives/dim2.rs b/crates/bevy_gizmos/src/primitives/dim2.rs index 515a1ad2bb2c0..d38ba3ab4ca95 100644 --- a/crates/bevy_gizmos/src/primitives/dim2.rs +++ b/crates/bevy_gizmos/src/primitives/dim2.rs @@ -635,8 +635,8 @@ where } let segment = Segment2d::new(self.point1, self.point2) - .translated(self.isometry.translation) - .rotated_around_center(self.isometry.rotation); + .rotated(self.isometry.rotation) + .translated(self.isometry.translation); if self.draw_arrow { self.gizmos