Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gizmos: arc_2d utility helpers #14932

Merged
merged 7 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
use Rot2 for arc_angle
  • Loading branch information
RobWalt committed Aug 28, 2024
commit 894264247d1b00efb62aef4e428d27ee5e55a32b
18 changes: 9 additions & 9 deletions crates/bevy_gizmos/src/arcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use crate::circles::DEFAULT_CIRCLE_RESOLUTION;
use crate::prelude::{GizmoConfigGroup, Gizmos};
use bevy_color::Color;
use bevy_math::{Isometry2d, Isometry3d, Quat, Vec2, Vec3};
use std::f32::consts::{FRAC_PI_2, TAU};
use bevy_math::{Isometry2d, Isometry3d, Quat, Rot2, Vec2, Vec3};
use std::f32::consts::TAU;

// === 2D ===

Expand Down Expand Up @@ -49,7 +49,7 @@ where
pub fn arc_2d(
&mut self,
isometry: Isometry2d,
arc_angle: f32,
arc_angle: Rot2,
radius: f32,
color: impl Into<Color>,
) -> Arc2dBuilder<'_, 'w, 's, Config, Clear> {
Expand All @@ -72,7 +72,7 @@ where
{
gizmos: &'a mut Gizmos<'w, 's, Config, Clear>,
isometry: Isometry2d,
arc_angle: f32,
arc_angle: Rot2,
radius: f32,
color: Color,
resolution: Option<u32>,
Expand Down Expand Up @@ -102,19 +102,19 @@ where

let resolution = self
.resolution
.unwrap_or_else(|| resolution_from_angle(self.arc_angle));
.unwrap_or_else(|| resolution_from_angle(self.arc_angle.as_radians()));

let positions =
arc_2d_inner(self.arc_angle, self.radius, resolution).map(|vec2| self.isometry * vec2);
self.gizmos.linestrip_2d(positions, self.color);
}
}

fn arc_2d_inner(arc_angle: f32, radius: f32, resolution: u32) -> impl Iterator<Item = Vec2> {
fn arc_2d_inner(arc_angle: Rot2, radius: f32, resolution: u32) -> impl Iterator<Item = Vec2> {
(0..=resolution)
.map(move |n| n as f32 * arc_angle / resolution as f32)
.map(|angle| angle + FRAC_PI_2)
.map(f32::sin_cos)
.map(move |n| Rot2::IDENTITY.slerp(arc_angle, n as f32 / resolution as f32))
.map(|angle| angle * Rot2::FRAC_PI_2)
.map(Rot2::sin_cos)
.map(|(sin, cos)| Vec2::new(cos, sin))
.map(move |vec2| vec2 * radius)
}
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_gizmos/src/primitives/dim2.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! A module for rendering each of the 2D [`bevy_math::primitives`] with [`Gizmos`].

use std::f32::consts::{FRAC_PI_2, PI};
use std::f32::consts::FRAC_PI_2;

use super::helpers::*;

Expand All @@ -10,7 +10,7 @@ use bevy_math::primitives::{
CircularSegment, Ellipse, Line2d, Plane2d, Polygon, Polyline2d, Primitive2d, Rectangle,
RegularPolygon, Rhombus, Segment2d, Triangle2d,
};
use bevy_math::{Dir2, Isometry2d, Rot2, Vec2};
use bevy_math::{Dir2, Isometry2d, Vec2};

use crate::prelude::{GizmoConfigGroup, Gizmos};

Expand Down Expand Up @@ -380,13 +380,13 @@ where
// draw arcs
self.arc_2d(
Isometry2d::new(top_center, Rot2::radians(start_angle_top)),
PI,
Rot2::PI,
primitive.radius,
polymorphic_color,
);
self.arc_2d(
Isometry2d::new(bottom_center, Rot2::radians(start_angle_bottom)),
PI,
Rot2::PI,
primitive.radius,
polymorphic_color,
);
Expand Down