Skip to content

Commit

Permalink
Use Isometry in bevy_gizmos wherever we can (bevyengine#14676)
Browse files Browse the repository at this point in the history
# Objective

- Solves the last bullet in and closes bevyengine#14319
- Make better use of the `Isometry` types
- Prevent issues like bevyengine#14655
- Probably simplify and clean up a lot of code through the use of Gizmos
as well (i.e. the 3D gizmos for cylinders circles & lines don't connect
well, probably due to wrong rotations)

## Solution

- go through the `bevy_gizmos` crate and give all methods a slight
workover

## Testing

- For all the changed examples I run `git switch main && cargo rr
--example <X> && git switch <BRANCH> && cargo rr --example <X>` and
compare the visual results
- Check if all doc tests are still compiling
- Check the docs in general and update them !!! 

---

## Migration Guide

The gizmos methods function signature changes as follows:

- 2D
- if it took `position` & `rotation_angle` before ->
`Isometry2d::new(position, Rot2::radians(rotation_angle))`
- if it just took `position` before ->
`Isometry2d::from_translation(position)`
- 3D
- if it took `position` & `rotation` before ->
`Isometry3d::new(position, rotation)`
- if it just took `position` before ->
`Isometry3d::from_translation(position)`
  • Loading branch information
RobWalt authored Aug 28, 2024
1 parent 45281e6 commit 8895113
Show file tree
Hide file tree
Showing 20 changed files with 632 additions and 790 deletions.
32 changes: 13 additions & 19 deletions crates/bevy_gizmos/src/arcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use crate::circles::DEFAULT_CIRCLE_RESOLUTION;
use crate::prelude::{GizmoConfigGroup, Gizmos};
use bevy_color::Color;
use bevy_math::{Isometry2d, Quat, Vec2, Vec3};
use bevy_math::{Isometry2d, Isometry3d, Quat, Vec2, Vec3};
use std::f32::consts::{FRAC_PI_2, TAU};

// === 2D ===
Expand Down Expand Up @@ -140,9 +140,9 @@ where
/// - `angle`: sets how much of a circle circumference is passed, e.g. PI is half a circle. This
/// value should be in the range (-2 * PI..=2 * PI)
/// - `radius`: distance between the arc and its center point
/// - `position`: position of the arcs center point
/// - `rotation`: defines orientation of the arc, by default we assume the arc is contained in a
/// plane parallel to the XZ plane and the default starting point is (`position + Vec3::X`)
/// - `isometry` defines the translation and rotation of the arc.
/// - the translation specifies the center of the arc
/// - the rotation is counter-clockwise starting from `Vec3::Y`
/// - `color`: color of the arc
///
/// # Builder methods
Expand All @@ -163,8 +163,7 @@ where
/// .arc_3d(
/// 270.0_f32.to_radians(),
/// 0.25,
/// Vec3::ONE,
/// rotation,
/// Isometry3d::new(Vec3::ONE, rotation),
/// ORANGE
/// )
/// .resolution(100);
Expand All @@ -176,15 +175,13 @@ where
&mut self,
angle: f32,
radius: f32,
position: Vec3,
rotation: Quat,
isometry: Isometry3d,
color: impl Into<Color>,
) -> Arc3dBuilder<'_, 'w, 's, Config, Clear> {
Arc3dBuilder {
gizmos: self,
start_vertex: Vec3::X,
center: position,
rotation,
isometry,
angle,
radius,
color: color.into(),
Expand Down Expand Up @@ -317,8 +314,7 @@ where
Arc3dBuilder {
gizmos: self,
start_vertex,
center,
rotation,
isometry: Isometry3d::new(center, rotation),
angle,
radius,
color: color.into(),
Expand All @@ -344,8 +340,7 @@ where
//
// DO NOT expose this field to users as it is easy to mess this up
start_vertex: Vec3,
center: Vec3,
rotation: Quat,
isometry: Isometry3d,
angle: f32,
radius: f32,
color: Color,
Expand Down Expand Up @@ -380,8 +375,7 @@ where

let positions = arc_3d_inner(
self.start_vertex,
self.center,
self.rotation,
self.isometry,
self.angle,
self.radius,
resolution,
Expand All @@ -392,8 +386,7 @@ where

fn arc_3d_inner(
start_vertex: Vec3,
center: Vec3,
rotation: Quat,
isometry: Isometry3d,
angle: f32,
radius: f32,
resolution: u32,
Expand All @@ -406,7 +399,8 @@ fn arc_3d_inner(
.map(move |frac| frac as f32 / resolution as f32)
.map(move |percentage| angle * percentage)
.map(move |frac_angle| Quat::from_axis_angle(Vec3::Y, frac_angle) * start_vertex)
.map(move |p| rotation * (p * radius) + center)
.map(move |vec3| vec3 * radius)
.map(move |vec3| isometry * vec3)
}

// helper function for getting a default value for the resolution parameter
Expand Down
Loading

0 comments on commit 8895113

Please sign in to comment.