Skip to content

Commit

Permalink
Fix math types
Browse files Browse the repository at this point in the history
  • Loading branch information
Jondolf committed Feb 17, 2024
1 parent 9683717 commit 0c79119
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 35 deletions.
10 changes: 7 additions & 3 deletions src/components/collider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,11 @@ impl Collider {
/// Creates a collider with an ellipse shape defined by a half-width and half-height.
#[cfg(feature = "2d")]
pub fn ellipse(half_width: Scalar, half_height: Scalar) -> Self {
SharedShape::new(EllipseWrapper(Ellipse::new(half_width, half_height))).into()
SharedShape::new(EllipseWrapper(Ellipse::new(
half_width as f32,
half_height as f32,
)))
.into()
}

/// Creates a collider with a rectangle shape defined by its extents.
Expand Down Expand Up @@ -586,7 +590,7 @@ impl Collider {

/// Creates a collider with a regular polygon shape defined by the circumradius and the number of sides.
#[cfg(feature = "2d")]
pub fn regular_polygon(circumradius: Scalar, sides: usize) -> Self {
pub fn regular_polygon(circumradius: f32, sides: usize) -> Self {
RegularPolygon::new(circumradius, sides).collider()
}

Expand Down Expand Up @@ -911,7 +915,7 @@ fn scale_shape(
} else {
// A 2D circle becomes an ellipse when scaled non-uniformly.
Ok(SharedShape::new(EllipseWrapper(Ellipse {
half_size: Vec2::splat(b.radius) * scale,
half_size: Vec2::splat(b.radius as f32) * scale.f32(),
})))
}
}
Expand Down
56 changes: 37 additions & 19 deletions src/components/collider/primitives2d.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{AdjustPrecision, Scalar};
use crate::{AdjustPrecision, AsF32, Scalar, Vector};

use super::{Collider, IntoCollider};
use bevy::prelude::Deref;
Expand Down Expand Up @@ -32,7 +32,7 @@ pub(crate) struct EllipseWrapper(pub(crate) Ellipse);
impl SupportMap for EllipseWrapper {
#[inline]
fn local_support_point(&self, direction: &Vector2<Scalar>) -> Point2<Scalar> {
let (a, b) = (self.half_size.x, self.half_size.y);
let [a, b] = self.half_size.adjust_precision().to_array();
let denom = (direction.x.powi(2) * a * a + direction.y.powi(2) * b * b).sqrt();
Point2::new(a * a * direction.x / denom, b * b * direction.y / denom)
}
Expand All @@ -41,18 +41,27 @@ impl SupportMap for EllipseWrapper {
impl Shape for EllipseWrapper {
fn compute_local_aabb(&self) -> parry::bounding_volume::Aabb {
let aabb = self.aabb_2d(Vec2::ZERO, 0.0);
parry::bounding_volume::Aabb::new(aabb.min.into(), aabb.max.into())
parry::bounding_volume::Aabb::new(
aabb.min.adjust_precision().into(),
aabb.max.adjust_precision().into(),
)
}

fn compute_aabb(&self, position: &Isometry<Scalar>) -> parry::bounding_volume::Aabb {
let aabb = self.aabb_2d(position.translation.into(), position.rotation.angle());
parry::bounding_volume::Aabb::new(aabb.min.into(), aabb.max.into())
let aabb = self.aabb_2d(
Vector::from(position.translation).f32(),
position.rotation.angle() as f32,
);
parry::bounding_volume::Aabb::new(
aabb.min.adjust_precision().into(),
aabb.max.adjust_precision().into(),
)
}

fn compute_local_bounding_sphere(&self) -> parry::bounding_volume::BoundingSphere {
let sphere = self.bounding_circle(Vec2::ZERO, 0.0);
parry::bounding_volume::BoundingSphere::new(
sphere.center.into(),
sphere.center.adjust_precision().into(),
sphere.radius().adjust_precision(),
)
}
Expand All @@ -61,9 +70,12 @@ impl Shape for EllipseWrapper {
&self,
position: &Isometry<Scalar>,
) -> parry::bounding_volume::BoundingSphere {
let sphere = self.bounding_circle(position.translation.into(), position.rotation.angle());
let sphere = self.bounding_circle(
Vector::from(position.translation).f32(),
position.rotation.angle() as f32,
);
parry::bounding_volume::BoundingSphere::new(
sphere.center.into(),
sphere.center.adjust_precision().into(),
sphere.radius().adjust_precision(),
)
}
Expand All @@ -72,10 +84,10 @@ impl Shape for EllipseWrapper {
Box::new(*self)
}

fn mass_properties(&self, density: parry::math::Real) -> MassProperties {
let volume = self.area();
fn mass_properties(&self, density: Scalar) -> MassProperties {
let volume = self.area().adjust_precision();
let mass = volume * density;
let inertia = mass * self.half_size.length_squared() / 4.0;
let inertia = mass * self.half_size.length_squared().adjust_precision() / 4.0;
MassProperties::new(Point2::origin(), mass, inertia)
}

Expand All @@ -91,11 +103,11 @@ impl Shape for EllipseWrapper {
parry::shape::TypedShape::Custom(1)
}

fn ccd_thickness(&self) -> parry::math::Real {
self.half_size.max_element()
fn ccd_thickness(&self) -> Scalar {
self.half_size.max_element().adjust_precision()
}

fn ccd_angular_thickness(&self) -> parry::math::Real {
fn ccd_angular_thickness(&self) -> Scalar {
crate::math::PI
}

Expand All @@ -108,7 +120,7 @@ impl RayCast for EllipseWrapper {
fn cast_local_ray_and_get_normal(
&self,
ray: &parry::query::Ray,
max_toi: parry::math::Real,
max_toi: Scalar,
solid: bool,
) -> Option<parry::query::RayIntersection> {
local_ray_intersection_with_support_map_with_params(
Expand All @@ -124,15 +136,15 @@ impl RayCast for EllipseWrapper {
impl PointQuery for EllipseWrapper {
fn project_local_point(
&self,
pt: &parry::math::Point<parry::math::Real>,
pt: &parry::math::Point<Scalar>,
solid: bool,
) -> parry::query::PointProjection {
local_point_projection_on_support_map(self, &mut VoronoiSimplex::new(), pt, solid)
}

fn project_local_point_and_get_feature(
&self,
pt: &parry::math::Point<parry::math::Real>,
pt: &parry::math::Point<Scalar>,
) -> (parry::query::PointProjection, parry::shape::FeatureId) {
(self.project_local_point(pt, false), FeatureId::Unknown)
}
Expand Down Expand Up @@ -185,7 +197,10 @@ impl IntoCollider for Triangle2d {

impl IntoCollider for Rectangle {
fn collider(&self) -> Collider {
Collider::from(SharedShape::cuboid(self.half_size.x, self.half_size.y))
Collider::from(SharedShape::cuboid(
self.half_size.x.adjust_precision(),
self.half_size.y.adjust_precision(),
))
}
}

Expand Down Expand Up @@ -220,6 +235,9 @@ impl IntoCollider for RegularPolygon {

impl IntoCollider for Capsule2d {
fn collider(&self) -> Collider {
Collider::capsule(2.0 * self.half_length, self.radius)
Collider::capsule(
2.0 * self.half_length.adjust_precision(),
self.radius.adjust_precision(),
)
}
}
11 changes: 8 additions & 3 deletions src/plugins/debug/gizmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ impl<'w, 's> PhysicsGizmoExt for Gizmos<'w, 's, PhysicsGizmos> {
#[cfg(feature = "2d")]
{
self.arrow_2d(a.f32(), b.f32(), color)
.with_tip_length(head_length);
.with_tip_length(head_length as f32);
}

#[cfg(feature = "3d")]
{
self.arrow(a.f32(), b.f32(), color)
.with_tip_length(head_length);
.with_tip_length(head_length as f32);
}
}

Expand Down Expand Up @@ -413,7 +413,12 @@ impl<'w, 's> PhysicsGizmoExt for Gizmos<'w, 's, PhysicsGizmos> {
#[cfg(feature = "2d")]
if _id == 1 {
if let Some(ellipse) = collider.shape_scaled().as_shape::<EllipseWrapper>() {
self.primitive_2d(ellipse.0, position.f32(), rotation.as_radians(), color);
self.primitive_2d(
ellipse.0,
position.f32(),
rotation.as_radians() as f32,
color,
);
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/plugins/debug/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,15 @@ fn debug_render_axes(
gizmos.circle_2d(
global_com.f32(),
// Scale dot size based on axis lengths
(lengths.x + lengths.y) / 20.0,
(lengths.x + lengths.y) as f32 / 20.0,
center_color,
);
#[cfg(feature = "3d")]
gizmos.sphere(
global_com.f32(),
rot.f32(),
// Scale dot size based on axis lengths
(lengths.x + lengths.y + lengths.z) / 30.0,
(lengths.x + lengths.y + lengths.z) as f32 / 30.0,
center_color,
);
}
Expand Down Expand Up @@ -304,10 +304,10 @@ fn debug_render_contacts(

for manifold in contacts.manifolds.iter() {
for contact in manifold.contacts.iter() {
let p1 = contact.global_point1(position1, rotation1).f32();
let p2 = contact.global_point2(position2, rotation2).f32();
let normal1 = contact.global_normal1(rotation1).f32();
let normal2 = contact.global_normal2(rotation2).f32();
let p1 = contact.global_point1(position1, rotation1);
let p2 = contact.global_point2(position2, rotation2);
let normal1 = contact.global_normal1(rotation1);
let normal2 = contact.global_normal2(rotation2);

// Don't render contacts that aren't penetrating
if contact.penetration <= Scalar::EPSILON {
Expand All @@ -318,13 +318,13 @@ fn debug_render_contacts(
if let Some(color) = config.contact_point_color {
#[cfg(feature = "2d")]
{
gizmos.circle_2d(p1, 3.0, color);
gizmos.circle_2d(p2, 3.0, color);
gizmos.circle_2d(p1.f32(), 3.0, color);
gizmos.circle_2d(p2.f32(), 3.0, color);
}
#[cfg(feature = "3d")]
{
gizmos.sphere(p1, default(), 0.025, color);
gizmos.sphere(p2, default(), 0.025, color);
gizmos.sphere(p1.f32(), default(), 0.025, color);
gizmos.sphere(p2.f32(), default(), 0.025, color);
}
}

Expand Down

0 comments on commit 0c79119

Please sign in to comment.