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
use std::mem;
use std::any::Any;
use std::rc::Rc;
use std::cell::RefCell;
use na;
use ncollide::math::Scalar;
use ncollide::inspection::Repr;
use ncollide::shape::ShapeHandle;
use math::{Point, Matrix};
use object::{RigidBodyHandle, SensorCollisionGroups};
pub type SensorHandle<N> = Rc<RefCell<Sensor<N>>>;
pub struct Sensor<N: Scalar> {
parent: Option<RigidBodyHandle<N>>,
relative_position: Matrix<N>,
shape: ShapeHandle<Point<N>, Matrix<N>>,
margin: N,
collision_groups: SensorCollisionGroups,
parent_prox: bool,
user_data: Option<Box<Any>>,
interfering_bodies: Vec<RigidBodyHandle<N>>,
interfering_sensors: Vec<SensorHandle<N>>
}
impl<N: Scalar> Sensor<N> {
pub fn new<G>(shape: G, parent: Option<RigidBodyHandle<N>>) -> Sensor<N>
where G: Send + Sync + Repr<Point<N>, Matrix<N>> {
Sensor::new_with_shared_shape(ShapeHandle::new(shape), parent)
}
pub fn new_with_shared_shape(shape: ShapeHandle<Point<N>, Matrix<N>>,
parent: Option<RigidBodyHandle<N>>)
-> Sensor<N> {
Sensor {
parent: parent,
relative_position: na::one(),
shape: shape,
margin: na::zero(),
collision_groups: SensorCollisionGroups::new(),
parent_prox: false,
user_data: None,
interfering_bodies: Vec::new(),
interfering_sensors: Vec::new()
}
}
#[inline]
pub fn user_data(&self) -> Option<&Box<Any>> {
self.user_data.as_ref()
}
#[inline]
pub fn user_data_mut(&mut self) -> Option<&mut Box<Any>> {
self.user_data.as_mut()
}
pub fn set_user_data(&mut self, user_data: Option<Box<Any>>) -> Option<Box<Any>> {
mem::replace(&mut self.user_data, user_data)
}
#[inline]
pub fn interfering_bodies(&self) -> &[RigidBodyHandle<N>] {
&self.interfering_bodies[..]
}
#[inline]
pub fn interfering_sensors(&self) -> &[SensorHandle<N>] {
&self.interfering_sensors[..]
}
#[inline]
pub fn relative_position(&self) -> &Matrix<N> {
&self.relative_position
}
#[inline]
pub fn set_relative_position(&mut self, rel_pos: Matrix<N>) {
self.relative_position = rel_pos
}
#[inline]
pub fn position(&self) -> Matrix<N> {
match self.parent {
Some(ref rb) => *rb.borrow().position() * self.relative_position,
None => self.relative_position.clone()
}
}
#[inline]
pub fn set_position(&mut self, abs_pos: Matrix<N>) {
match self.parent {
Some(ref rb) => {
self.relative_position = na::inverse(rb.borrow().position()).unwrap() * abs_pos
}
None => self.relative_position = abs_pos
}
}
#[inline]
pub fn center(&self) -> Point<N> {
match self.parent {
Some(ref rb) => *rb.borrow().position() * na::translation(&self.relative_position).to_point(),
None => na::translation(&self.relative_position).to_point()
}
}
pub fn margin(&self) -> N {
self.margin
}
#[inline]
pub fn proximity_with_parent_enabled(&self) -> bool {
self.parent_prox
}
#[inline]
pub fn enable_proximity_with_parent(&mut self) {
self.parent_prox = true;
}
#[inline]
pub fn disable_proximity_with_parent(&mut self) {
self.parent_prox = false;
}
#[inline]
pub fn parent(&self) -> Option<&RigidBodyHandle<N>> {
self.parent.as_ref()
}
#[inline]
pub fn shape(&self) -> &ShapeHandle<Point<N>, Matrix<N>> {
&self.shape
}
#[inline]
pub fn collision_groups(&self) -> &SensorCollisionGroups {
&self.collision_groups
}
}