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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
use cgmath::{BaseFloat, EuclideanSpace, Transform, VectorSpace, Zero};

use super::PartialCrossProduct;

/// Force accumulator for a physical entity.
///
/// Will be consumed when doing force integration for the next frame.
///
/// ### Type parameters:
///
/// - `F`: Force type, usually `Vector2` or `Vector3`
/// - `T`: Torque force, usually `Scalar` or `Vector3`
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ForceAccumulator<F, T> {
    force: F,
    torque: T,
}

impl<F, T> Default for ForceAccumulator<F, T>
where
    F: VectorSpace + Zero,
    T: Zero + Copy + Clone,
{
    fn default() -> Self {
        ForceAccumulator::new()
    }
}

impl<F, T> ForceAccumulator<F, T>
where
    F: VectorSpace + Zero,
    T: Zero + Copy + Clone,
{
    /// Create a new force accumulator
    pub fn new() -> Self {
        Self {
            force: F::zero(),
            torque: T::zero(),
        }
    }

    /// Add a force vector to the accumulator
    pub fn add_force(&mut self, force: F) {
        self.force = self.force + force;
    }

    /// Add a torque vector to the accumulator
    pub fn add_torque(&mut self, torque: T) {
        self.torque = self.torque + torque;
    }

    /// Add a force on a given point on the body
    ///
    /// If the force vector does not pass directly through the origin of the body, as expressed by
    /// the pose, torque will occur.
    /// Note that no validation is made on the given position to make sure it's actually contained
    /// in the shape of the body.
    ///
    /// ### Parameters:
    ///
    /// - `force`: Force to apply
    /// - `position`: Position on the body to apply the force at.
    /// - `pose`: Current pose of the body, used to compute the world coordinates of the body center
    ///           of mass
    pub fn add_force_at_point<P, B>(&mut self, force: F, position: P, pose: &B)
    where
        P: EuclideanSpace<Scalar = F::Scalar, Diff = F>,
        P::Scalar: BaseFloat,
        B: Transform<P>,
        F: PartialCrossProduct<F, Output = T>,
    {
        let current_pos = pose.transform_point(P::origin());
        let r = position - current_pos;
        self.add_force(force);
        self.add_torque(r.cross(&force));
    }

    /// Consume the accumulated force
    ///
    /// Returns he current accumulated force. The force in the accumulator is reset.
    pub fn consume_force(&mut self) -> F {
        let v = self.force;
        self.force = F::zero();
        v
    }

    /// Consume the torque
    ///
    /// Returns the current accumulated torque. The torque in the accumulator is reset.
    pub fn consume_torque(&mut self) -> T {
        let v = self.torque;
        self.torque = T::zero();
        v
    }
}

#[cfg(test)]
mod tests_f32 {
    use cgmath::{Point2, Point3, Transform, Vector2, Vector3, Zero};

    use super::ForceAccumulator;
    use physics2d::BodyPose2;
    use physics3d::BodyPose3;

    #[test]
    fn test_add_force() {
        let mut forces = ForceAccumulator::<Vector2<f32>, f32>::new();
        forces.add_force(Vector2::new(0., 2.));
        forces.add_force(Vector2::new(1.4, 2.));
        assert_eq!(Vector2::new(1.4, 4.), forces.consume_force());
        assert_eq!(Vector2::zero(), forces.consume_force());
        assert_eq!(0., forces.consume_torque());

        let mut forces = ForceAccumulator::<Vector3<f32>, f32>::new();
        forces.add_force(Vector3::new(0., 2., -1.));
        forces.add_force(Vector3::new(1.4, 2., -1.));
        assert_eq!(Vector3::new(1.4, 4., -2.), forces.consume_force());
        assert_eq!(Vector3::zero(), forces.consume_force());
        assert_eq!(0., forces.consume_torque());
    }

    #[test]
    fn test_add_torque() {
        let mut forces = ForceAccumulator::<Vector2<f32>, f32>::new();
        forces.add_torque(0.2);
        forces.add_torque(1.4);
        assert_ulps_eq!(1.6, forces.consume_torque());
        assert_eq!(Vector2::zero(), forces.consume_force());
        assert_eq!(0., forces.consume_torque());

        let mut forces = ForceAccumulator::<Vector3<f32>, f32>::new();
        forces.add_torque(0.2);
        forces.add_torque(1.4);
        assert_ulps_eq!(1.6, forces.consume_torque());
        assert_eq!(Vector3::zero(), forces.consume_force());
        assert_eq!(0., forces.consume_torque());
    }

    #[test]
    fn test_add_force_at_point_2d() {
        let mut forces = ForceAccumulator::<Vector2<f32>, f32>::new();
        // add at origin -> no torque
        forces.add_force_at_point(Vector2::new(1., 1.), Point2::new(0., 0.), &BodyPose2::one());
        assert_eq!(Vector2::new(1., 1.), forces.consume_force());
        assert_eq!(0., forces.consume_torque());
        // add pointed at origin -> no torque
        forces.add_force_at_point(
            Vector2::new(1., 1.),
            Point2::new(-1., -1.),
            &BodyPose2::one(),
        );
        assert_eq!(Vector2::new(1., 1.), forces.consume_force());
        assert_eq!(0., forces.consume_torque());
        // add outside with offset -> torque
        forces.add_force_at_point(
            Vector2::new(1., 1.),
            Point2::new(-1., 0.),
            &BodyPose2::one(),
        );
        assert_eq!(Vector2::new(1., 1.), forces.consume_force());
        assert_eq!(-1., forces.consume_torque());
    }

    #[test]
    fn test_add_force_at_point_3d() {
        let mut forces = ForceAccumulator::<Vector3<f32>, Vector3<f32>>::new();
        // add at origin -> no torque
        forces.add_force_at_point(
            Vector3::new(1., 1., 1.),
            Point3::new(0., 0., 0.),
            &BodyPose3::one(),
        );
        assert_eq!(Vector3::new(1., 1., 1.), forces.consume_force());
        assert_eq!(Vector3::zero(), forces.consume_torque());
        // add pointed at origin -> no torque
        forces.add_force_at_point(
            Vector3::new(1., 1., 1.),
            Point3::new(-1., -1., -1.),
            &BodyPose3::one(),
        );
        assert_eq!(Vector3::new(1., 1., 1.), forces.consume_force());
        assert_eq!(Vector3::zero(), forces.consume_torque());
        // add outside with offset -> torque
        forces.add_force_at_point(
            Vector3::new(1., 1., 1.),
            Point3::new(-1., 0., 0.),
            &BodyPose3::one(),
        );
        assert_eq!(Vector3::new(1., 1., 1.), forces.consume_force());
        assert_eq!(Vector3::new(0., 1., -1.), forces.consume_torque());
    }
}

#[cfg(test)]
mod tests_f64 {
    use cgmath::{Point2, Point3, Transform, Vector2, Vector3, Zero};

    use super::ForceAccumulator;
    use physics2d::BodyPose2;
    use physics3d::BodyPose3;

    #[test]
    fn test_add_force() {
        let mut forces = ForceAccumulator::<Vector2<f64>, f64>::new();
        forces.add_force(Vector2::new(0., 2.));
        forces.add_force(Vector2::new(1.4, 2.));
        assert_eq!(Vector2::new(1.4, 4.), forces.consume_force());
        assert_eq!(Vector2::zero(), forces.consume_force());
        assert_eq!(0., forces.consume_torque());

        let mut forces = ForceAccumulator::<Vector3<f64>, f64>::new();
        forces.add_force(Vector3::new(0., 2., -1.));
        forces.add_force(Vector3::new(1.4, 2., -1.));
        assert_eq!(Vector3::new(1.4, 4., -2.), forces.consume_force());
        assert_eq!(Vector3::zero(), forces.consume_force());
        assert_eq!(0., forces.consume_torque());
    }

    #[test]
    fn test_add_torque() {
        let mut forces = ForceAccumulator::<Vector2<f64>, f64>::new();
        forces.add_torque(0.2);
        forces.add_torque(1.4);
        assert_ulps_eq!(1.6, forces.consume_torque());
        assert_eq!(Vector2::zero(), forces.consume_force());
        assert_eq!(0., forces.consume_torque());

        let mut forces = ForceAccumulator::<Vector3<f64>, f64>::new();
        forces.add_torque(0.2);
        forces.add_torque(1.4);
        assert_ulps_eq!(1.6, forces.consume_torque());
        assert_eq!(Vector3::zero(), forces.consume_force());
        assert_eq!(0., forces.consume_torque());
    }

    #[test]
    fn test_add_force_at_point_2d() {
        let mut forces = ForceAccumulator::<Vector2<f64>, f64>::new();
        // add at origin -> no torque
        forces.add_force_at_point(Vector2::new(1., 1.), Point2::new(0., 0.), &BodyPose2::one());
        assert_eq!(Vector2::new(1., 1.), forces.consume_force());
        assert_eq!(0., forces.consume_torque());
        // add pointed at origin -> no torque
        forces.add_force_at_point(
            Vector2::new(1., 1.),
            Point2::new(-1., -1.),
            &BodyPose2::one(),
        );
        assert_eq!(Vector2::new(1., 1.), forces.consume_force());
        assert_eq!(0., forces.consume_torque());
        // add outside with offset -> torque
        forces.add_force_at_point(
            Vector2::new(1., 1.),
            Point2::new(-1., 0.),
            &BodyPose2::one(),
        );
        assert_eq!(Vector2::new(1., 1.), forces.consume_force());
        assert_eq!(-1., forces.consume_torque());
    }

    #[test]
    fn test_add_force_at_point_3d() {
        let mut forces = ForceAccumulator::<Vector3<f64>, Vector3<f64>>::new();
        // add at origin -> no torque
        forces.add_force_at_point(
            Vector3::new(1., 1., 1.),
            Point3::new(0., 0., 0.),
            &BodyPose3::one(),
        );
        assert_eq!(Vector3::new(1., 1., 1.), forces.consume_force());
        assert_eq!(Vector3::zero(), forces.consume_torque());
        // add pointed at origin -> no torque
        forces.add_force_at_point(
            Vector3::new(1., 1., 1.),
            Point3::new(-1., -1., -1.),
            &BodyPose3::one(),
        );
        assert_eq!(Vector3::new(1., 1., 1.), forces.consume_force());
        assert_eq!(Vector3::zero(), forces.consume_torque());
        // add outside with offset -> torque
        forces.add_force_at_point(
            Vector3::new(1., 1., 1.),
            Point3::new(-1., 0., 0.),
            &BodyPose3::one(),
        );
        assert_eq!(Vector3::new(1., 1., 1.), forces.consume_force());
        assert_eq!(Vector3::new(0., 1., -1.), forces.consume_torque());
    }
}