From 2b628f9580a826722346983ef42672d4e8dd8053 Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Mon, 26 Oct 2020 15:58:30 +0100 Subject: Redefine capsules as a segment with a radius, allowing us to reuse the pfm_pfm_contact generator for it. --- src/geometry/collider.rs | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) (limited to 'src/geometry/collider.rs') diff --git a/src/geometry/collider.rs b/src/geometry/collider.rs index 838dda2..ae5cc73 100644 --- a/src/geometry/collider.rs +++ b/src/geometry/collider.rs @@ -1,7 +1,7 @@ use crate::dynamics::{MassProperties, RigidBodyHandle, RigidBodySet}; use crate::geometry::{ Ball, Capsule, ColliderGraphIndex, Contact, Cuboid, HeightField, InteractionGraph, Proximity, - Shape, ShapeType, Triangle, Trimesh, + Segment, Shape, ShapeType, Triangle, Trimesh, }; #[cfg(feature = "dim3")] use crate::geometry::{Cone, Cylinder, PolygonalFeatureMap, Rounded}; @@ -58,9 +58,14 @@ impl ColliderShape { ColliderShape(Arc::new(Cuboid::new(half_extents))) } - /// Initialize a capsule shape aligned with the `y` axis. - pub fn capsule(half_height: f32, radius: f32) -> Self { - ColliderShape(Arc::new(Capsule::new(half_height, radius))) + /// Initialize a capsule shape from its endpoints and radius. + pub fn capsule(a: Point, b: Point, radius: f32) -> Self { + ColliderShape(Arc::new(Capsule::new(a, b, radius))) + } + + /// Initialize a segment shape from its endpoints. + pub fn segment(a: Point, b: Point) -> Self { + ColliderShape(Arc::new(Segment::new(a, b))) } /// Initializes a triangle shape. @@ -157,6 +162,7 @@ impl<'de> serde::Deserialize<'de> for ColliderShape { Some(ShapeType::Cuboid) => deser::(&mut seq)?, Some(ShapeType::Capsule) => deser::(&mut seq)?, Some(ShapeType::Triangle) => deser::(&mut seq)?, + Some(ShapeType::Segment) => deser::(&mut seq)?, Some(ShapeType::Trimesh) => deser::(&mut seq)?, Some(ShapeType::HeightField) => deser::(&mut seq)?, #[cfg(feature = "dim3")] @@ -349,25 +355,21 @@ impl ColliderBuilder { /// Initialize a new collider builder with a capsule shape aligned with the `x` axis. pub fn capsule_x(half_height: f32, radius: f32) -> Self { - #[cfg(feature = "dim2")] - let rot = -std::f32::consts::FRAC_PI_2; - #[cfg(feature = "dim3")] - let rot = Vector::z() * -std::f32::consts::FRAC_PI_2; - Self::new(ColliderShape::capsule(half_height, radius)) - .position(Isometry::new(na::zero(), rot)) + let p = Point::from(Vector::x() * half_height); + Self::new(ColliderShape::capsule(-p, p, radius)) } /// Initialize a new collider builder with a capsule shape aligned with the `y` axis. pub fn capsule_y(half_height: f32, radius: f32) -> Self { - Self::new(ColliderShape::capsule(half_height, radius)) + let p = Point::from(Vector::y() * half_height); + Self::new(ColliderShape::capsule(-p, p, radius)) } /// Initialize a new collider builder with a capsule shape aligned with the `z` axis. #[cfg(feature = "dim3")] pub fn capsule_z(half_height: f32, radius: f32) -> Self { - let rot = Vector::x() * std::f32::consts::FRAC_PI_2; - Self::new(ColliderShape::capsule(half_height, radius)) - .position(Isometry::new(na::zero(), rot)) + let p = Point::from(Vector::z() * half_height); + Self::new(ColliderShape::capsule(-p, p, radius)) } /// Initialize a new collider builder with a cuboid shape defined by its half-extents. @@ -377,11 +379,8 @@ impl ColliderBuilder { } /// Initializes a collider builder with a segment shape. - /// - /// A segment shape is modeled by a capsule with a 0 radius. pub fn segment(a: Point, b: Point) -> Self { - let (pos, half_height) = crate::utils::segment_to_capsule(&a, &b); - Self::new(ColliderShape::capsule(half_height, 0.0)).position(pos) + Self::new(ColliderShape::segment(a, b)) } /// Initializes a collider builder with a triangle shape. -- cgit