1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//! # Rhusics physics library
//!
//! A physics library, primarily built to be used with
//! [`specs`](https://github.com/slide-rs/specs/).
//! Uses [`cgmath`](https://github.com/brendanzab/cgmath/) for all computation.
//!
//! Features:
//!
//! * Two different broad phase collision detection implementations:
//!   * Brute force
//!   * Sweep and Prune
//! * Narrow phase collision detection using GJK, and optionally EPA for full contact information
//! * [`specs::System`](https://docs.rs/specs/0.9.5/specs/trait.System.html) for collision
//!    detection working on user supplied transform, and
//!    [`CollisionShape`](collide/struct.CollisionShape.html) components.
//!    Can optionally use broad and/or narrow phase detection.
//!    Library supplies a transform implementation [`BodyPose`](struct.BodyPose.html) for
//!    convenience.
//! * Uses single precision as default, can be changed to double precision with the `double`
//!   feature.
//! * Has support for doing spatial sort/collision detection using the collision-rs DBVT.
//! * Support for doing broad phase using the collision-rs DBVT.
//! * Has support for all primitives in collision-rs
//!
//! # Examples
//!
//! See the `examples/` directory for examples.

#![deny(missing_docs, trivial_casts, unsafe_code, unstable_features, unused_import_braces,
        unused_qualifications)]

extern crate cgmath;
extern crate collision;
#[cfg(feature = "ecs")]
extern crate shrev;
#[cfg(feature = "ecs")]
extern crate specs;

#[cfg(test)]
#[macro_use]
extern crate approx;

#[cfg(feature = "eders")]
#[macro_use]
extern crate serde;

pub mod collide;
pub mod physics;
#[cfg(feature = "ecs")]
pub mod ecs;

use cgmath::BaseFloat;
use cgmath::prelude::*;
use collision::prelude::*;

/// Wrapper for data computed for the next frame
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct NextFrame<T> {
    /// Wrapped value
    pub value: T,
}

/// Transform component used throughout the library
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "eders", derive(Serialize, Deserialize))]
pub struct BodyPose<P, R> {
    dirty: bool,
    position: P,
    rotation: R,
    inverse_rotation: R,
}

impl<P, R> BodyPose<P, R>
where
    P: EuclideanSpace,
    P::Scalar: BaseFloat,
    R: Rotation<P>,
{
    /// Create a new [`BodyPose`](struct.BodyPose.html) with initial state given by the supplied
    /// position and rotation.
    pub fn new(position: P, rotation: R) -> Self {
        Self {
            dirty: true,
            position,
            inverse_rotation: rotation.invert(),
            rotation,
        }
    }

    /// Set the rotation. Will also compute the inverse rotation. Sets the dirty flag.
    pub fn set_rotation(&mut self, rotation: R) {
        self.rotation = rotation;
        self.inverse_rotation = self.rotation.invert();
        self.dirty = true;
    }

    /// Set the position. Sets the dirty flag.
    pub fn set_position(&mut self, position: P) {
        self.position = position;
        self.dirty = true;
    }

    /// Borrows the position attribute
    pub fn position(&self) -> &P {
        &self.position
    }

    /// Borrows the rotation attribute
    pub fn rotation(&self) -> &R {
        &self.rotation
    }

    /// Clear the dirty flag
    pub fn clear(&mut self) {
        self.dirty = false;
    }
}

impl<P, R> Transform<P> for BodyPose<P, R>
where
    P: EuclideanSpace,
    P::Scalar: BaseFloat,
    R: Rotation<P>,
{
    fn one() -> Self {
        Self::new(P::origin(), R::one())
    }

    fn look_at(eye: P, center: P, up: P::Diff) -> Self {
        let rot = R::look_at(center - eye, up);
        let disp = rot.rotate_vector(P::origin() - eye);
        Self::new(P::from_vec(disp), rot)
    }

    fn transform_vector(&self, vec: P::Diff) -> P::Diff {
        self.rotation.rotate_vector(vec)
    }

    fn transform_point(&self, point: P) -> P {
        self.rotation.rotate_point(point) + self.position.to_vec()
    }

    fn concat(&self, other: &Self) -> Self {
        Self::new(
            self.position + self.rotation.rotate_point(other.position).to_vec(),
            self.rotation * other.rotation,
        )
    }

    fn inverse_transform(&self) -> Option<Self> {
        Some(Self::new(
            self.rotation.rotate_point(self.position) * -P::Scalar::one(),
            self.inverse_rotation,
        ))
    }

    fn inverse_transform_vector(&self, vec: P::Diff) -> Option<P::Diff> {
        Some(self.inverse_rotation.rotate_vector(vec))
    }
}

impl<P, R> TranslationInterpolate<P::Scalar> for BodyPose<P, R>
where
    P: EuclideanSpace,
    P::Scalar: BaseFloat,
    P::Diff: VectorSpace + InnerSpace,
    R: Rotation<P> + Clone,
{
    fn translation_interpolate(&self, other: &Self, amount: P::Scalar) -> Self {
        BodyPose::new(
            P::from_vec(self.position.to_vec().lerp(other.position.to_vec(), amount)),
            other.rotation.clone(),
        )
    }
}

impl<P, R> Interpolate<P::Scalar> for BodyPose<P, R>
where
    P: EuclideanSpace,
    P::Scalar: BaseFloat,
    P::Diff: VectorSpace + InnerSpace,
    R: Rotation<P> + Interpolate<P::Scalar>,
{
    fn interpolate(&self, other: &Self, amount: P::Scalar) -> Self {
        BodyPose::new(
            P::from_vec(self.position.to_vec().lerp(other.position.to_vec(), amount)),
            self.rotation.interpolate(&other.rotation, amount),
        )
    }
}