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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
use std::fmt::Debug;
use std::ops::{Add, Mul, Sub};

use cgmath::{BaseFloat, Basis2, EuclideanSpace, InnerSpace, Matrix3, Point2, Point3, Quaternion,
             Rotation, Transform, Vector3, Zero};
use collision::{Bound, ComputeBound, Contains, Discrete, HasBound, SurfaceArea, Union};
use collision::dbvt::TreeValue;
use core::{ApplyAngular, BroadPhase, GetId, Inertia, NarrowPhase, PartialCrossProduct,
           PhysicsTime, Pose, Primitive};
use specs::prelude::{Component, DispatcherBuilder, Entity, Tracked};

/// Create systems and add to a `Dispatcher` graph.
///
/// ### Parameters
///
/// - `dispatcher`: The dispatcher to add the systems to.
/// - `broad_phase`: Broad phase to use
/// - `narrow_phase`: Narrow phase to use
/// - `spatial`: If spatial or basic collision detection should be used
///
/// ### Type parameters:
///
/// - `P`: Shape primitive
/// - `T`: Pose (transform)
/// - `B`: Bounding volume
/// - `D`: Broad phase data, usually `TreeValueWrapped`.
/// - `Y`: Collider
/// - `V`: Broad phase algorithm
/// - `N`: Narrow phase algorithm
/// - `R`: Rotational quantity, `Basis2` or `Quaternion`
/// - `A`: Angular velocity, `Scalar` or `Vector3`
/// - `I`: Inertia, `Scalar` or `Matrix3`
/// - `DT`: Time quantity, usually `DeltaTime`
/// - `O`: Internal type used to abstract cross product for 2D vs 3D, `Scalar` or `Vector3`
pub fn setup_dispatch<'a, 'b, P, T, B, D, Y, V, N, R, A, I, DT, O>(
    dispatcher: &mut DispatcherBuilder<'a, 'b>,
    broad_phase: V,
    narrow_phase: N,
    spatial: bool,
) where
    V: BroadPhase<D> + BroadPhase<(usize, D)> + 'static,
    N: NarrowPhase<P, T, B, Y> + 'static,
    P: Primitive + ComputeBound<B> + Send + Sync + 'static,
    P::Point: Debug + Send + Sync + 'static,
    <P::Point as EuclideanSpace>::Scalar: BaseFloat + Send + Sync + 'static,
    <P::Point as EuclideanSpace>::Diff: InnerSpace
        + PartialCrossProduct<<P::Point as EuclideanSpace>::Diff, Output = O>
        + Debug
        + Send
        + Sync
        + 'static,
    T: Debug + Component + Pose<P::Point, R> + Transform<P::Point> + Send + Sync + Clone + 'static,
    T::Storage: Tracked,
    Y: Default + Send + Sync + 'static,
    B: Bound<Point = P::Point>
        + Send
        + Sync
        + 'static
        + Union<B, Output = B>
        + Clone
        + Contains<B>
        + SurfaceArea<Scalar = <P::Point as EuclideanSpace>::Scalar>
        + Discrete<B>
        + Debug,
    (usize, D): HasBound<Bound = B>,
    D: TreeValue<Bound = B>
        + HasBound<Bound = B>
        + From<(Entity, B)>
        + GetId<Entity>
        + Send
        + Sync
        + 'static,
    R: Rotation<P::Point>
        + ApplyAngular<<P::Point as EuclideanSpace>::Scalar, A>
        + Send
        + Sync
        + 'static,
    I: Inertia<Orientation = R> + Mul<A, Output = A> + Mul<O, Output = O> + Send + Sync + 'static,
    A: Mul<<P::Point as EuclideanSpace>::Scalar, Output = A>
        + PartialCrossProduct<
        <P::Point as EuclideanSpace>::Diff,
        Output = <P::Point as EuclideanSpace>::Diff,
    >
        + Zero
        + Clone
        + Copy
        + Send
        + Sync
        + 'static,
    DT: PhysicsTime<<P::Point as EuclideanSpace>::Scalar> + Default + Send + Sync + 'static,
    O: PartialCrossProduct<
        <P::Point as EuclideanSpace>::Diff,
        Output = <P::Point as EuclideanSpace>::Diff,
    >
        + Send
        + Sync
        + 'static,
    for<'c> &'c A: Sub<O, Output = A> + Add<O, Output = A>,
{
    use {BasicCollisionSystem, ContactResolutionSystem, CurrentFrameUpdateSystem,
         NextFrameSetupSystem, SpatialCollisionSystem, SpatialSortingSystem};
    dispatcher.add(
        CurrentFrameUpdateSystem::<P::Point, R, A, T>::new(),
        "physics_solver_system",
        &[],
    );
    dispatcher.add(
        NextFrameSetupSystem::<P::Point, R, I, A, T, DT>::new(),
        "next_frame_setup",
        &["physics_solver_system"],
    );
    if spatial {
        dispatcher.add(
            SpatialSortingSystem::<P, T, D, B, Y>::new(),
            "spatial_sorting_system",
            &["next_frame_setup"],
        );
        dispatcher.add(
            SpatialCollisionSystem::<P, T, (usize, D), B, Y>::new()
                .with_broad_phase(broad_phase)
                .with_narrow_phase(narrow_phase),
            "collision_system",
            &["spatial_sorting_system"],
        );
    } else {
        dispatcher.add(
            BasicCollisionSystem::<P, T, D, B, Y>::new()
                .with_broad_phase(broad_phase)
                .with_narrow_phase(narrow_phase),
            "collision_system",
            &["next_frame_setup"],
        );
    }
    dispatcher.add(
        ContactResolutionSystem::<P::Point, R, I, A, O, T>::new(),
        "contact_resolution",
        &["collision_system"],
    );
}

/// Create systems for 2D and add to a `Dispatcher` graph.
///
/// ### Parameters
///
/// - `dispatcher`: The dispatcher to add the systems to.
/// - `broad_phase`: Broad phase to use
/// - `narrow_phase`: Narrow phase to use
/// - `spatial`: If spatial or basic collision detection should be used
///
/// ### Type parameters:
///
/// - `S`: Scalar type, `f32` or `f64`
/// - `P`: Shape primitive
/// - `T`: Pose (transform)
/// - `B`: Bounding volume
/// - `D`: Broad phase data, usually `TreeValueWrapped`.
/// - `Y`: Collider
/// - `V`: Broad phase algorithm
/// - `N`: Narrow phase algorithm
/// - `DT`: Time quantity, usually `DeltaTime`
pub fn setup_dispatch_2d<'a, 'b, S, P, T, B, D, Y, V, N, DT>(
    dispatcher: &mut DispatcherBuilder<'a, 'b>,
    broad_phase: V,
    narrow_phase: N,
    spatial: bool,
) where
    V: BroadPhase<D> + BroadPhase<(usize, D)> + 'static,
    N: NarrowPhase<P, T, B, Y> + 'static,
    P: Primitive<Point = Point2<S>> + ComputeBound<B> + Send + Sync + 'static,
    S: Inertia<Orientation = Basis2<S>> + BaseFloat + Send + Sync + 'static,
    T: Component
        + Pose<Point2<S>, Basis2<S>>
        + Debug
        + Transform<Point2<S>>
        + Send
        + Sync
        + Clone
        + 'static,
    T::Storage: Tracked,
    Y: Default + Send + Sync + 'static,
    B: Bound<Point = Point2<S>>
        + Send
        + Sync
        + 'static
        + Union<B, Output = B>
        + Clone
        + Contains<B>
        + SurfaceArea<Scalar = S>
        + Discrete<B>
        + Debug,
    (usize, D): HasBound<Bound = B>,
    D: TreeValue<Bound = B>
        + HasBound<Bound = B>
        + From<(Entity, B)>
        + GetId<Entity>
        + Send
        + Sync
        + 'static,
    DT: PhysicsTime<S> + Default + Send + Sync + 'static,
    for<'c> &'c S: Sub<S, Output = S> + Add<S, Output = S>,
{
    setup_dispatch::<P, T, B, D, Y, V, N, Basis2<S>, S, S, DT, S>(
        dispatcher,
        broad_phase,
        narrow_phase,
        spatial,
    );
}

/// Create systems for 3sD and add to a `Dispatcher` graph.
///
/// ### Parameters
///
/// - `dispatcher`: The dispatcher to add the systems to.
/// - `broad_phase`: Broad phase to use
/// - `narrow_phase`: Narrow phase to use
/// - `spatial`: If spatial or basic collision detection should be used
///
/// ### Type parameters:
///
/// - `S`: Scalar type, `f32` or `f64`
/// - `P`: Shape primitive
/// - `T`: Pose (transform)
/// - `B`: Bounding volume
/// - `D`: Broad phase data, usually `TreeValueWrapped`.
/// - `Y`: Collider
/// - `V`: Broad phase algorithm
/// - `N`: Narrow phase algorithm
/// - `DT`: Time quantity, usually `DeltaTime`
pub fn setup_dispatch_3d<'a, 'b, S, P, T, B, D, Y, V, N, DT>(
    dispatcher: &mut DispatcherBuilder<'a, 'b>,
    broad_phase: V,
    narrow_phase: N,
    spatial: bool,
) where
    V: BroadPhase<D> + BroadPhase<(usize, D)> + 'static,
    N: NarrowPhase<P, T, B, Y> + 'static,
    P: Primitive<Point = Point3<S>> + ComputeBound<B> + Send + Sync + 'static,
    S: BaseFloat + Send + Sync + 'static,
    T: Component
        + Pose<Point3<S>, Quaternion<S>>
        + Transform<Point3<S>>
        + Debug
        + Send
        + Sync
        + Clone
        + 'static,
    T::Storage: Tracked,
    Y: Default + Send + Sync + 'static,
    B: Bound<Point = Point3<S>>
        + Send
        + Sync
        + 'static
        + Union<B, Output = B>
        + Clone
        + Contains<B>
        + SurfaceArea<Scalar = S>
        + Discrete<B>
        + Debug,
    (usize, D): HasBound<Bound = B>,
    D: TreeValue<Bound = B>
        + HasBound<Bound = B>
        + From<(Entity, B)>
        + GetId<Entity>
        + Send
        + Sync
        + 'static,
    DT: PhysicsTime<S> + Default + Send + Sync + 'static,
{
    setup_dispatch::<P, T, B, D, Y, V, N, Quaternion<S>, Vector3<S>, Matrix3<S>, DT, Vector3<S>>(
        dispatcher,
        broad_phase,
        narrow_phase,
        spatial,
    );
}

#[cfg(test)]
mod tests {

    use super::*;
    use DeltaTime;
    use collide2d::{BodyPose2, GJK2, SweepAndPrune2};
    use collide3d::{BodyPose3, GJK3, SweepAndPrune3};
    use collision::{Aabb2, Aabb3};
    use collision::dbvt::TreeValueWrapped;
    use collision::primitive::{Primitive2, Primitive3};

    #[test]
    fn test_dispatch() {
        let mut builder = DispatcherBuilder::new();
        setup_dispatch::<
            Primitive2<f32>,
            BodyPose2<f32>,
            Aabb2<f32>,
            TreeValueWrapped<Entity, Aabb2<f32>>,
            (),
            _,
            _,
            _,
            _,
            f32,
            DeltaTime<f32>,
            _,
        >(&mut builder, SweepAndPrune2::new(), GJK2::new(), false);
    }

    #[test]
    fn test_dispatch_2d() {
        let mut builder = DispatcherBuilder::new();
        setup_dispatch_2d::<
            _,
            Primitive2<f32>,
            BodyPose2<f32>,
            Aabb2<f32>,
            TreeValueWrapped<Entity, Aabb2<f32>>,
            (),
            _,
            _,
            DeltaTime<f32>,
        >(&mut builder, SweepAndPrune2::new(), GJK2::new(), false);
    }

    #[test]
    fn test_dispatch_3d() {
        let mut builder = DispatcherBuilder::new();
        setup_dispatch_3d::<
            _,
            Primitive3<f32>,
            BodyPose3<f32>,
            Aabb3<f32>,
            TreeValueWrapped<Entity, Aabb3<f32>>,
            (),
            _,
            _,
            DeltaTime<f32>,
        >(&mut builder, SweepAndPrune3::new(), GJK3::new(), false);
    }
}