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
183
184
use na::storage::Storage;
use na::{self, Isometry2, RealField, Rotation2, Vector, Vector1, Vector2, Vector3, U3};
use std::mem;
use std::ops::{Add, AddAssign, Mul, Sub, SubAssign};
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct Velocity2<N: RealField> {
pub linear: Vector2<N>,
pub angular: N,
}
impl<N: RealField> Velocity2<N> {
#[inline]
pub fn new(linear: Vector2<N>, angular: N) -> Self {
Velocity2 { linear, angular }
}
#[inline]
pub fn from_vectors(linear: Vector2<N>, angular: Vector1<N>) -> Self {
Self::new(linear, angular.x)
}
#[inline]
pub fn angular(w: N) -> Self {
Velocity2::new(na::zero(), w)
}
#[inline]
pub fn linear(vx: N, vy: N) -> Self {
Velocity2::new(Vector2::new(vx, vy), N::zero())
}
#[inline]
pub fn zero() -> Self {
Self::new(na::zero(), N::zero())
}
pub fn between_positions(start: &Isometry2<N>, end: &Isometry2<N>, time: N) -> Self {
let delta = end / start;
let linear = delta.translation.vector / time;
let angular = delta.rotation.angle() / time;
Self::new(linear, angular)
}
#[inline]
pub fn angular_vector(&self) -> Vector1<N> {
Vector1::new(self.angular)
}
pub fn integrate(&self, dt: N) -> Isometry2<N> {
(*self * dt).to_transform()
}
pub fn to_transform(&self) -> Isometry2<N> {
Isometry2::new(self.linear, self.angular)
}
#[inline]
pub fn as_slice(&self) -> &[N] {
self.as_vector().as_slice()
}
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [N] {
self.as_vector_mut().as_mut_slice()
}
#[inline]
pub fn as_vector(&self) -> &Vector3<N> {
unsafe { mem::transmute(self) }
}
#[inline]
pub fn as_vector_mut(&mut self) -> &mut Vector3<N> {
unsafe { mem::transmute(self) }
}
#[inline]
pub fn from_vector<S: Storage<N, U3>>(data: &Vector<N, U3, S>) -> Self {
Self::new(Vector2::new(data[0], data[1]), data[2])
}
#[inline]
pub fn from_slice(data: &[N]) -> Self {
Self::new(Vector2::new(data[0], data[1]), data[2])
}
#[inline]
pub fn shift(&self, shift: &Vector2<N>) -> Self {
Self::new(
self.linear + Vector2::new(-shift.y, shift.x) * self.angular,
self.angular,
)
}
#[inline]
pub fn rotated(&self, rot: &Rotation2<N>) -> Self {
Self::new(rot * self.linear, self.angular)
}
#[inline]
pub fn transformed(&self, iso: &Isometry2<N>) -> Self {
Self::new(iso * self.linear, self.angular)
}
}
impl<N: RealField> Add<Velocity2<N>> for Velocity2<N> {
type Output = Self;
#[inline]
fn add(self, rhs: Self) -> Self {
Velocity2::new(self.linear + rhs.linear, self.angular + rhs.angular)
}
}
impl<N: RealField> AddAssign<Velocity2<N>> for Velocity2<N> {
#[inline]
fn add_assign(&mut self, rhs: Self) {
self.linear += rhs.linear;
self.angular += rhs.angular;
}
}
impl<N: RealField> Sub<Velocity2<N>> for Velocity2<N> {
type Output = Self;
#[inline]
fn sub(self, rhs: Self) -> Self {
Velocity2::new(self.linear - rhs.linear, self.angular - rhs.angular)
}
}
impl<N: RealField> SubAssign<Velocity2<N>> for Velocity2<N> {
#[inline]
fn sub_assign(&mut self, rhs: Self) {
self.linear -= rhs.linear;
self.angular -= rhs.angular;
}
}
impl<N: RealField> Mul<N> for Velocity2<N> {
type Output = Self;
#[inline]
fn mul(self, rhs: N) -> Self {
Velocity2::new(self.linear * rhs, self.angular * rhs)
}
}