ncollide3d/shape/
cone.rs

1//! Support mapping based Cone shape.
2
3use crate::math::{Point, Vector};
4use crate::shape::SupportMap;
5use na::{self, RealField};
6
7/// SupportMap description of a cylinder shape with its principal axis aligned with the `y` axis.
8#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9#[derive(PartialEq, Debug, Copy, Clone)]
10pub struct Cone<N> {
11    /// The half-height of the cone.
12    pub half_height: N,
13    /// The base radius of the cone.
14    pub radius: N,
15}
16
17impl<N: RealField + Copy> Cone<N> {
18    /// Creates a new cone.
19    ///
20    /// # Arguments:
21    /// * `half_height` - the half length of the cone along the `y` axis.
22    /// * `radius` - the length of the cone along all other axis.
23    pub fn new(half_height: N, radius: N) -> Cone<N> {
24        Cone {
25            half_height,
26            radius,
27        }
28    }
29
30    /// The cone half length along the `y` axis.
31    #[inline]
32    #[deprecated(note = "use the `self.half_height` public field directly.")]
33    pub fn half_height(&self) -> N {
34        self.half_height
35    }
36
37    /// The radius of the cone along all but the `y` axis.
38    #[inline]
39    #[deprecated(note = "use the `self.radius` public field directly.")]
40    pub fn radius(&self) -> N {
41        self.radius
42    }
43}
44
45impl<N: RealField + Copy> SupportMap<N> for Cone<N> {
46    #[inline]
47    fn local_support_point(&self, dir: &Vector<N>) -> Point<N> {
48        let mut vres = *dir;
49
50        vres[1] = na::zero();
51
52        if vres.normalize_mut().is_zero() {
53            vres = na::zero();
54            vres[1] = self.half_height.copysign(dir[1]);
55        } else {
56            vres = vres * self.radius;
57            vres[1] = -self.half_height;
58
59            if dir.dot(&vres) < dir[1] * self.half_height {
60                vres = na::zero();
61                vres[1] = self.half_height
62            }
63        }
64
65        Point::from(vres)
66    }
67}