aboutsummaryrefslogtreecommitdiff
path: root/src/geometry/collider.rs
diff options
context:
space:
mode:
authorSébastien Crozet <developer@crozet.re>2020-10-06 16:53:54 +0200
committerGitHub <noreply@github.com>2020-10-06 16:53:54 +0200
commit24a25f8ae7a62c5c5afa24825b063fbb1b603922 (patch)
tree5302f5282fe963b72dbd9e94f422994b6ab11eca /src/geometry/collider.rs
parent99f28ba4b4a14254b4160a191cbeb15211cdd2d2 (diff)
parent25b8486ebf8bdfa0d165300a30877293e9e40c51 (diff)
downloadrapier-24a25f8ae7a62c5c5afa24825b063fbb1b603922.tar.gz
rapier-24a25f8ae7a62c5c5afa24825b063fbb1b603922.tar.bz2
rapier-24a25f8ae7a62c5c5afa24825b063fbb1b603922.zip
Merge pull request #28 from dimforge/raycast
Add the QueryPipeline for ray-casting and other geometrical queries in the future
Diffstat (limited to 'src/geometry/collider.rs')
-rw-r--r--src/geometry/collider.rs52
1 files changed, 51 insertions, 1 deletions
diff --git a/src/geometry/collider.rs b/src/geometry/collider.rs
index 2d55857..7c293b6 100644
--- a/src/geometry/collider.rs
+++ b/src/geometry/collider.rs
@@ -1,11 +1,12 @@
use crate::dynamics::{MassProperties, RigidBodyHandle, RigidBodySet};
use crate::geometry::{
Ball, Capsule, ColliderGraphIndex, Contact, Cuboid, HeightField, InteractionGraph, Polygon,
- Proximity, Triangle, Trimesh,
+ Proximity, Ray, RayIntersection, Triangle, Trimesh,
};
use crate::math::{AngVector, Isometry, Point, Rotation, Vector};
use na::Point3;
use ncollide::bounding_volume::{HasBoundingVolume, AABB};
+use ncollide::query::RayCast;
use num::Zero;
#[derive(Clone)]
@@ -97,6 +98,49 @@ impl Shape {
Shape::HeightField(heightfield) => heightfield.bounding_volume(position),
}
}
+
+ /// Computes the first intersection point between a ray in this collider.
+ ///
+ /// Some shapes are not supported yet and will always return `None`.
+ ///
+ /// # Parameters
+ /// - `position`: the position of this shape.
+ /// - `ray`: the ray to cast.
+ /// - `max_toi`: the maximum time-of-impact that can be reported by this cast. This effectively
+ /// limits the length of the ray to `ray.dir.norm() * max_toi`. Use `f32::MAX` for an unbounded ray.
+ pub fn cast_ray(
+ &self,
+ position: &Isometry<f32>,
+ ray: &Ray,
+ max_toi: f32,
+ ) -> Option<RayIntersection> {
+ match self {
+ Shape::Ball(ball) => ball.toi_and_normal_with_ray(position, ray, max_toi, true),
+ Shape::Polygon(_poly) => None,
+ Shape::Capsule(caps) => {
+ let pos = position * caps.transform_wrt_y();
+ let caps = ncollide::shape::Capsule::new(caps.half_height(), caps.radius);
+ caps.toi_and_normal_with_ray(&pos, ray, max_toi, true)
+ }
+ Shape::Cuboid(cuboid) => cuboid.toi_and_normal_with_ray(position, ray, max_toi, true),
+ #[cfg(feature = "dim2")]
+ Shape::Triangle(_) | Shape::Trimesh(_) => {
+ // This is not implemented yet in 2D.
+ None
+ }
+ #[cfg(feature = "dim3")]
+ Shape::Triangle(triangle) => {
+ triangle.toi_and_normal_with_ray(position, ray, max_toi, true)
+ }
+ #[cfg(feature = "dim3")]
+ Shape::Trimesh(trimesh) => {
+ trimesh.toi_and_normal_with_ray(position, ray, max_toi, true)
+ }
+ Shape::HeightField(heightfield) => {
+ heightfield.toi_and_normal_with_ray(position, ray, max_toi, true)
+ }
+ }
+ }
}
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
@@ -353,6 +397,12 @@ impl ColliderBuilder {
self
}
+ /// Sets the restitution coefficient of the collider this builder will build.
+ pub fn restitution(mut self, restitution: f32) -> Self {
+ self.restitution = restitution;
+ self
+ }
+
/// Sets the density of the collider this builder will build.
pub fn density(mut self, density: f32) -> Self {
self.density = Some(density);