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
use prelude::*;
use ami::*;
use constants;
pub struct RigidBody {
#[allow(unused)] mass: Mass,
force: Vector, spin: Rotation, bbox: BBox,
position: Vector, rotation: Rotation, }
impl Collider for RigidBody {
fn bbox(&self) -> BBox {
self.bbox + self.position
}
}
impl RigidBody {
pub fn new(mass: Mass, bbox: BBox, position: Vector,
rotation: Rotation) -> Self
{
RigidBody {
mass, force: vector!(), bbox, position,
spin: Rotation::identity(), rotation,
}
}
pub fn apply_gravity(&mut self) {
self.apply_force(Vector{ x:0.0, y:constants::GRAVITY, z:0.0 });
}
pub fn apply_force(&mut self, force: Vector) {
self.force += force;
}
pub fn apply_spin(&mut self, spin: Rotation) {
self.spin = self.spin.then(spin);
}
pub fn stop_force(&mut self) {
self.force = vector!();
}
pub fn stop_spin(&mut self) {
self.spin = Rotation::identity();
}
pub fn update(&mut self, time: f32) -> Matrix {
self.position += self.force * time;
self.rotation.then(self.spin * time);
matrix!().rt(self.rotation,self.position)
}
}