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
use crate::data::arena::Arena;
use crate::dynamics::{RigidBodyHandle, RigidBodySet};
use crate::geometry::Collider;
use std::ops::{Index, IndexMut};
pub type ColliderHandle = crate::data::arena::Index;
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct ColliderSet {
pub(crate) colliders: Arena<Collider>,
}
impl ColliderSet {
pub fn new() -> Self {
ColliderSet {
colliders: Arena::new(),
}
}
pub fn invalid_handle() -> ColliderHandle {
ColliderHandle::from_raw_parts(crate::INVALID_USIZE, crate::INVALID_U64)
}
pub fn iter(&self) -> impl Iterator<Item = (ColliderHandle, &Collider)> {
self.colliders.iter()
}
pub fn len(&self) -> usize {
self.colliders.len()
}
pub fn contains(&self, handle: ColliderHandle) -> bool {
self.colliders.contains(handle)
}
pub fn insert(
&mut self,
mut coll: Collider,
parent_handle: RigidBodyHandle,
bodies: &mut RigidBodySet,
) -> ColliderHandle {
coll.parent = parent_handle;
let parent = bodies
.get_mut_internal(parent_handle)
.expect("Parent rigid body not found.");
coll.position = parent.position * coll.delta;
coll.predicted_position = parent.predicted_position * coll.delta;
let handle = self.colliders.insert(coll);
let coll = self.colliders.get(handle).unwrap();
parent.add_collider_internal(handle, &coll);
bodies.activate(parent_handle);
handle
}
pub(crate) fn remove_internal(&mut self, handle: ColliderHandle) -> Option<Collider> {
self.colliders.remove(handle)
}
pub fn get_unknown_gen(&self, i: usize) -> Option<(&Collider, ColliderHandle)> {
self.colliders.get_unknown_gen(i)
}
pub fn get_unknown_gen_mut(&mut self, i: usize) -> Option<(&mut Collider, ColliderHandle)> {
self.colliders.get_unknown_gen_mut(i)
}
pub fn get(&self, handle: ColliderHandle) -> Option<&Collider> {
self.colliders.get(handle)
}
pub fn get_mut(&mut self, handle: ColliderHandle) -> Option<&mut Collider> {
self.colliders.get_mut(handle)
}
pub(crate) fn get2_mut_internal(
&mut self,
h1: ColliderHandle,
h2: ColliderHandle,
) -> (Option<&mut Collider>, Option<&mut Collider>) {
self.colliders.get2_mut(h1, h2)
}
}
impl Index<ColliderHandle> for ColliderSet {
type Output = Collider;
fn index(&self, index: ColliderHandle) -> &Collider {
&self.colliders[index]
}
}
impl IndexMut<ColliderHandle> for ColliderSet {
fn index_mut(&mut self, index: ColliderHandle) -> &mut Collider {
&mut self.colliders[index]
}
}