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
use std::fmt::Debug;
use cgmath::prelude::*;
use collision::prelude::*;
use shrev::EventChannel;
use specs::prelude::{Component, Entities, Entity, Join, ReadStorage, System, Write, WriteStorage};
use core::{basic_collide, BroadPhase, CollisionData, CollisionShape, ContactEvent, GetId,
NarrowPhase, NextFrame, Primitive};
pub struct BasicCollisionSystem<P, T, D, B, Y = ()>
where
P: Primitive,
B: Bound,
{
narrow: Option<Box<NarrowPhase<P, T, B, Y>>>,
broad: Option<Box<BroadPhase<D>>>,
}
impl<P, T, D, B, Y> BasicCollisionSystem<P, T, D, B, Y>
where
P: Primitive + ComputeBound<B> + Send + Sync + 'static,
P::Point: Debug + Send + Sync + 'static,
<P::Point as EuclideanSpace>::Scalar: Send + Sync + 'static,
<P::Point as EuclideanSpace>::Diff: Debug + Send + Sync + 'static,
T: Component + Transform<P::Point> + Send + Sync + Clone + 'static,
Y: Default + Send + Sync + 'static,
B: Bound<Point = P::Point> + Send + Sync + 'static + Union<B, Output = B> + Clone,
D: HasBound<Bound = B> + From<(Entity, B)> + GetId<Entity>,
{
pub fn new() -> Self {
Self {
narrow: None,
broad: None,
}
}
pub fn with_narrow_phase<N: NarrowPhase<P, T, B, Y> + 'static>(mut self, narrow: N) -> Self {
self.narrow = Some(Box::new(narrow));
self
}
pub fn with_broad_phase<V: BroadPhase<D> + 'static>(mut self, broad: V) -> Self {
self.broad = Some(Box::new(broad));
self
}
}
impl<'a, P, T, Y, D, B> System<'a> for BasicCollisionSystem<P, T, D, B, Y>
where
P: Primitive + ComputeBound<B> + Send + Sync + 'static,
P::Point: Debug + Send + Sync + 'static,
<P::Point as EuclideanSpace>::Scalar: Send + Sync + 'static,
<P::Point as EuclideanSpace>::Diff: Debug + Send + Sync + 'static,
T: Component + Transform<P::Point> + Send + Sync + Clone + 'static,
Y: Default + Send + Sync + 'static,
B: Bound<Point = P::Point> + Send + Sync + 'static + Union<B, Output = B> + Clone,
D: HasBound<Bound = B> + From<(Entity, B)> + GetId<Entity>,
{
type SystemData = (
Entities<'a>,
ReadStorage<'a, T>,
ReadStorage<'a, NextFrame<T>>,
WriteStorage<'a, CollisionShape<P, T, B, Y>>,
Write<'a, EventChannel<ContactEvent<Entity, P::Point>>>,
);
fn run(&mut self, system_data: Self::SystemData) {
let (entities, poses, next_poses, mut shapes, mut event_channel) = system_data;
if let Some(ref mut broad) = self.broad {
for (entity, pose, shape) in (&*entities, &poses, &mut shapes).join() {
shape.update(pose, next_poses.get(entity).map(|p| &p.value));
}
event_channel.iter_write(basic_collide(
&BasicCollisionData {
poses,
shapes,
next_poses,
entities,
},
broad,
&self.narrow,
));
}
}
}
struct BasicCollisionData<'a, P, T, B, Y>
where
P: Primitive + ComputeBound<B> + Send + Sync + 'static,
P::Point: Debug + Send + Sync + 'static,
<P::Point as EuclideanSpace>::Scalar: Send + Sync + 'static,
<P::Point as EuclideanSpace>::Diff: Debug + Send + Sync + 'static,
T: Component + Transform<P::Point> + Send + Sync + Clone + 'static,
Y: Default + Send + Sync + 'static,
B: Bound<Point = P::Point> + Send + Sync + 'static + Union<B, Output = B> + Clone,
{
pub shapes: WriteStorage<'a, CollisionShape<P, T, B, Y>>,
pub poses: ReadStorage<'a, T>,
pub next_poses: ReadStorage<'a, NextFrame<T>>,
pub entities: Entities<'a>,
}
impl<'a, P, T, B, Y, D> CollisionData<Entity, P, T, B, Y, D> for BasicCollisionData<'a, P, T, B, Y>
where
P: Primitive + ComputeBound<B> + Send + Sync + 'static,
P::Point: Debug + Send + Sync + 'static,
<P::Point as EuclideanSpace>::Scalar: Send + Sync + 'static,
<P::Point as EuclideanSpace>::Diff: Debug + Send + Sync + 'static,
T: Component + Transform<P::Point> + Send + Sync + Clone + 'static,
Y: Default + Send + Sync + 'static,
B: Bound<Point = P::Point> + Send + Sync + 'static + Union<B, Output = B> + Clone,
D: HasBound<Bound = B> + From<(Entity, B)> + GetId<Entity>,
{
fn get_broad_data(&self) -> Vec<D> {
(&*self.entities, &self.shapes)
.join()
.map(|(entity, shape)| (entity, shape.bound().clone()).into())
.collect::<Vec<_>>()
}
fn get_shape(&self, id: Entity) -> Option<&CollisionShape<P, T, B, Y>> {
self.shapes.get(id)
}
fn get_pose(&self, id: Entity) -> Option<&T> {
self.poses.get(id)
}
fn get_next_pose(&self, id: Entity) -> Option<&T> {
self.next_poses.get(id).as_ref().map(|p| &p.value)
}
}