/
body_integrator.rs
155 lines (137 loc) · 5.33 KB
/
body_integrator.rs
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
use std::ptr;
use std::num::One;
use nalgebra::mat::{Translation, Rotation, Rotate, Transformation, Transform, Inv};
use nalgebra::vec::Vec;
use ncollide::util::hash_map::HashMap;
use ncollide::util::hash::UintTWHash;
use object::{RB, SB};
use object::Body;
use object::volumetric::InertiaTensor;
use integration::Integrator;
use integration::euler;
use signal::signal::SignalEmiter;
pub struct BodyExpEulerIntegrator<N, LV, AV, M, II> {
priv objects: HashMap<uint, @mut Body<N, LV, AV, M, II>, UintTWHash>,
}
impl<N: 'static + Clone,
M: 'static + Clone + Inv + Mul<M, M> + Rotation<AV> + Rotate<LV> + Translation<LV> +
Transform<LV> + One + ToStr,
LV: 'static + Clone + Vec<N> + ToStr,
AV: 'static + Clone + Vec<N> + ToStr,
II: 'static + Clone + Mul<II, II> + InertiaTensor<N, LV, M> + Inv>
BodyExpEulerIntegrator<N, LV, AV, M, II> {
#[inline]
pub fn new<C>(events: &mut SignalEmiter<N, Body<N, LV, AV, M, II>, C>)
-> @mut BodyExpEulerIntegrator<N, LV, AV, M, II> {
let res = @mut BodyExpEulerIntegrator {
objects: HashMap::new(UintTWHash::new())
};
events.add_body_activated_handler(ptr::to_mut_unsafe_ptr(res) as uint, |o, _| res.add(o));
events.add_body_deactivated_handler(ptr::to_mut_unsafe_ptr(res) as uint, |o| res.remove(o));
res
}
}
impl<N: Clone,
M: Clone + Inv + Mul<M, M> + Rotation<AV> + Rotate<LV> + Translation<LV> + Transform<LV> + One + ToStr,
LV: Clone + Vec<N> + ToStr,
AV: Clone + Vec<N> + ToStr,
II: Clone + Mul<II, II> + InertiaTensor<N, LV, M> + Inv>
Integrator<N, Body<N, LV, AV, M, II>> for BodyExpEulerIntegrator<N, LV, AV, M, II> {
#[inline]
fn add(&mut self, o: @mut Body<N, LV, AV, M, II>) {
self.objects.insert(ptr::to_mut_unsafe_ptr(o) as uint, o);
}
#[inline]
fn remove(&mut self, o: @mut Body<N, LV, AV, M, II>) {
self.objects.remove(&(ptr::to_mut_unsafe_ptr(o) as uint));
}
#[inline]
fn update(&mut self, dt: N) {
for o in self.objects.elements().iter() {
match *o.value {
RB(ref mut rb) => {
if rb.can_move() {
let (t, lv, av) = euler::explicit_integrate(
dt.clone(),
rb.transform_ref(),
rb.center_of_mass(),
&rb.lin_vel(),
&rb.ang_vel(),
&rb.lin_acc(),
&rb.ang_acc());
rb.transform_by(&t);
rb.set_lin_vel(lv);
rb.set_ang_vel(av);
}
},
SB(_) => {
fail!("Not yet implemented.")
}
}
}
}
#[inline]
fn priority(&self) -> f64 { 50.0 }
}
pub struct BodySmpEulerIntegrator<N, LV, AV, M, II> {
priv objects: HashMap<uint, @mut Body<N, LV, AV, M, II>, UintTWHash>,
}
impl<N: 'static + Clone,
M: 'static + Clone + Inv + Mul<M, M> + Rotation<AV> + Transform<LV> + Translation<LV> +
Rotate<LV> + One + ToStr,
LV: 'static + Clone + Vec<N> + ToStr,
AV: 'static + Clone + Vec<N> + ToStr,
II: 'static + Clone + Mul<II, II> + Inv + InertiaTensor<N, LV, M>>
BodySmpEulerIntegrator<N, LV, AV, M, II> {
#[inline]
pub fn new<C>(events: &mut SignalEmiter<N, Body<N, LV, AV, M, II>, C>)
-> @mut BodySmpEulerIntegrator<N, LV, AV, M, II> {
let res = @mut BodySmpEulerIntegrator {
objects: HashMap::new(UintTWHash::new())
};
events.add_body_activated_handler(ptr::to_mut_unsafe_ptr(res) as uint, |o, _| res.add(o));
events.add_body_deactivated_handler(ptr::to_mut_unsafe_ptr(res) as uint, |o| res.remove(o));
res
}
}
impl<N: Clone,
M: Clone + Inv + Mul<M, M> + Rotation<AV> + Transform<LV> + Translation<LV> + Rotate<LV> + One +
ToStr,
LV: Clone + Vec<N> + ToStr,
AV: Clone + Vec<N> + ToStr,
II: Clone + Mul<II, II> + Inv + InertiaTensor<N, LV, M>>
Integrator<N, Body<N, LV, AV, M, II>> for BodySmpEulerIntegrator<N, LV, AV, M, II> {
#[inline]
fn add(&mut self, o: @mut Body<N, LV, AV, M, II>) {
self.objects.insert(ptr::to_mut_unsafe_ptr(o) as uint, o);
}
#[inline]
fn remove(&mut self, o: @mut Body<N, LV, AV, M, II>) {
self.objects.remove(&(ptr::to_mut_unsafe_ptr(o) as uint));
}
#[inline]
fn update(&mut self, dt: N) {
for o in self.objects.elements().iter() {
match *o.value {
RB(ref mut rb) => {
if rb.can_move() {
let (t, lv, av) = euler::semi_implicit_integrate(
dt.clone(),
rb.transform_ref(),
rb.center_of_mass(),
&rb.lin_vel(),
&rb.ang_vel(),
&rb.lin_acc(),
&rb.ang_acc());
rb.transform_by(&t);
rb.set_lin_vel(lv);
rb.set_ang_vel(av);
}
},
SB(_) => fail!("Not yet implemented.")
}
}
}
#[inline]
fn priority(&self) -> f64 { 50.0 }
}