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
use nalgebra::RealField;
use nphysics::object::BodyHandle;
pub use nphysics::object::BodyStatus;
use specs::{Component, DenseVecStorage, FlaggedStorage};
use crate::math::{Matrix3, Point3, Vector3};
pub trait Position<N: RealField> {
fn position(&self) -> (N, N, N);
fn set_position(&mut self, x: N, y: N, z: N);
}
#[derive(Clone, Copy, Debug)]
pub struct PhysicsBody<N: RealField> {
pub(crate) handle: Option<BodyHandle>,
pub gravity_enabled: bool,
pub body_status: BodyStatus,
pub velocity: Vector3<N>,
pub angular_inertia: Matrix3<N>,
pub mass: N,
pub local_center_of_mass: Point3<N>,
}
impl<N: RealField> Component for PhysicsBody<N> {
type Storage = FlaggedStorage<Self, DenseVecStorage<Self>>;
}
pub struct PhysicsBodyBuilder<N: RealField> {
gravity_enabled: bool,
body_status: BodyStatus,
velocity: Vector3<N>,
angular_inertia: Matrix3<N>,
mass: N,
local_center_of_mass: Point3<N>,
}
impl<N: RealField> From<BodyStatus> for PhysicsBodyBuilder<N> {
fn from(body_status: BodyStatus) -> Self {
Self {
gravity_enabled: false,
body_status,
velocity: Vector3::repeat(N::zero()),
angular_inertia: Matrix3::zeros(),
mass: N::from_f32(1.2).unwrap(),
local_center_of_mass: Point3::new(N::zero(), N::zero(), N::zero()),
}
}
}
impl<N: RealField> PhysicsBodyBuilder<N> {
pub fn gravity_enabled(mut self, gravity_enabled: bool) -> Self {
self.gravity_enabled = gravity_enabled;
self
}
pub fn velocity(mut self, velocity: Vector3<N>) -> Self {
self.velocity = velocity;
self
}
pub fn angular_inertia(mut self, angular_inertia: Matrix3<N>) -> Self {
self.angular_inertia = angular_inertia;
self
}
pub fn mass(mut self, mass: N) -> Self {
self.mass = mass;
self
}
pub fn local_center_of_mass(mut self, local_center_of_mass: Point3<N>) -> Self {
self.local_center_of_mass = local_center_of_mass;
self
}
pub fn build(self) -> PhysicsBody<N> {
PhysicsBody {
handle: None,
gravity_enabled: self.gravity_enabled,
body_status: self.body_status,
velocity: self.velocity,
angular_inertia: self.angular_inertia,
mass: self.mass,
local_center_of_mass: self.local_center_of_mass,
}
}
}