physme 0.2.4

A simple 2d and 3d physics engine for bevy
Documentation
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
//! This module provides the primitives and systems for 2d physics simulation.
//!
//! For examples, see the root of the crate.

use std::cmp::Ordering;
use std::mem;

use bevy::math::*;
use bevy::prelude::*;
use hashbrown::HashSet;
use serde::{Deserialize, Serialize};
use smallvec::SmallVec;

use crate::broad::{self, BoundingBox, Collider};
use crate::common::*;

/// This is what you want to add to your `App` if you want to run 2d physics simulation.
pub struct Physics2dPlugin;

pub mod stage {
    #[doc(hidden)]
    pub use bevy::prelude::stage::*;

    pub const COLLIDING_JOINT: &str = "colliding_joint";
    pub const PHYSICS_STEP: &str = "physics_step";
    pub const BROAD_PHASE: &str = "broad_phase";
    pub const NARROW_PHASE: &str = "narrow_phase";
    pub const PHYSICS_SOLVE: &str = "physics_solve";
    pub const RIGID_JOINT: &str = "rigid_joint";
    pub const SYNC_TRANSFORM: &str = "sync_transform";
}

impl Plugin for Physics2dPlugin {
    fn build(&self, app: &mut AppBuilder) {
        app.add_resource(GlobalFriction::default())
            .add_resource(GlobalGravity::default())
            .add_resource(TranslationMode::default())
            .add_resource(RotationMode::default())
            .add_resource(GlobalStep::default())
            .add_resource(GlobalUp::default())
            .add_resource(AngularTolerance::default())
            .add_event::<Manifold>()
            .add_stage_before(stage::UPDATE, stage::PHYSICS_STEP)
            .add_stage_before(stage::PHYSICS_STEP, stage::COLLIDING_JOINT)
            .add_stage_after(stage::PHYSICS_STEP, stage::BROAD_PHASE)
            .add_stage_after(stage::BROAD_PHASE, stage::NARROW_PHASE)
            .add_stage_after(stage::NARROW_PHASE, stage::PHYSICS_SOLVE)
            .add_stage_after(stage::PHYSICS_SOLVE, stage::RIGID_JOINT)
            .add_stage_after(stage::RIGID_JOINT, stage::SYNC_TRANSFORM);
        let physics_step = PhysicsStep::default().system(app.resources_mut());
        app.add_system_to_stage(stage::PHYSICS_STEP, physics_step)
            .add_system_to_stage(stage::BROAD_PHASE, broad_phase_system.system())
            .add_system_to_stage(stage::NARROW_PHASE, narrow_phase_system.system());
        let solver = Solver::default().system(app.resources_mut());
        app.add_system_to_stage(stage::PHYSICS_SOLVE, solver)
            .add_system_to_stage(stage::SYNC_TRANSFORM, sync_transform_system.system())
            .add_system_to_stage(
                FixedJointBehaviour::STAGE,
                joint_system::<FixedJointBehaviour>.system(),
            )
            .add_system_to_stage(
                MechanicalJointBehaviour::STAGE,
                joint_system::<MechanicalJointBehaviour>.system(),
            )
            .add_system_to_stage(
                SpringJointBehaviour::STAGE,
                joint_system::<SpringJointBehaviour>.system(),
            );
    }
}

pub type BroadPhase = broad::BroadPhase<Obb>;

/// The global gravity that affects every `RigidBody` with the `Semikinematic` status.
#[derive(Default, Debug, Clone, Copy, PartialEq)]
pub struct GlobalGravity(pub Vec2);

/// The global step value, affects all semikinematic bodies.
#[derive(Default, Debug, Clone, Copy, PartialEq)]
pub struct GlobalStep(pub f32);

/// The global up vector, affects all semikinematic bodies.
#[derive(Default, Debug, Clone, Copy, PartialEq)]
pub struct GlobalUp(pub Vec2);

/// The global angular tolerance in radians, affects all semikinematic bodies.
///
/// This is used for step calculation and for push dynamics.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct AngularTolerance(pub f32);

impl Default for AngularTolerance {
    fn default() -> Self {
        Self(30.0_f32.to_radians())
    }
}

#[doc(hidden)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Obb {
    status: Status,
    body: Entity,
    position: Vec2,
    rotation: Mat2,
    vertices: [Vec2; 4],
    normals: [Vec2; 4],
}

impl Obb {
    fn new(
        status: Status,
        body: Entity,
        rotation: Mat2,
        position: Vec2,
        v0: Vec2,
        v1: Vec2,
        v2: Vec2,
        v3: Vec2,
        n0: Vec2,
        n1: Vec2,
    ) -> Self {
        Self {
            status,
            body,
            rotation,
            position,
            vertices: [v0, v1, v2, v3],
            normals: [-n1, n0, n1, -n0],
        }
    }

    pub fn v0(&self) -> Vec2 {
        self.rotation * self.vertices[0] + self.position
    }

    pub fn v1(&self) -> Vec2 {
        self.rotation * self.vertices[1] + self.position
    }

    pub fn v2(&self) -> Vec2 {
        self.rotation * self.vertices[2] + self.position
    }

    pub fn v3(&self) -> Vec2 {
        self.rotation * self.vertices[3] + self.position
    }

    pub fn min(&self) -> Vec2 {
        let min_x = self
            .v0()
            .x()
            .min(self.v1().x())
            .min(self.v2().x())
            .min(self.v3().x());
        let min_y = self
            .v0()
            .y()
            .min(self.v1().y())
            .min(self.v2().y())
            .min(self.v3().y());
        Vec2::new(min_x, min_y)
    }

    pub fn max(&self) -> Vec2 {
        let max_x = self
            .v0()
            .x()
            .max(self.v1().x())
            .max(self.v2().x())
            .max(self.v3().x());
        let max_y = self
            .v0()
            .y()
            .max(self.v1().y())
            .max(self.v2().y())
            .max(self.v3().y());
        Vec2::new(max_x, max_y)
    }

    pub fn get_support(&self, dir: Vec2) -> Vec2 {
        let mut best_projection = f32::MIN;
        let mut best_vertex = Vec2::zero();

        for i in 0..4 {
            let v = self.vertices[i];
            let proj = v.dot(dir);

            if proj > best_projection {
                best_vertex = v;
                best_projection = proj;
            }
        }

        best_vertex
    }
}

impl Collider for Obb {
    type Point = Vec2;

    fn bounding_box(&self) -> BoundingBox<Self::Point> {
        BoundingBox::new(self.min(), self.max())
    }

    fn status(&self) -> Status {
        self.status
    }
}

/// The two dimensional size of a `Shape`
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Property)]
pub struct Size2 {
    pub width: f32,
    pub height: f32,
}

impl Size2 {
    /// Returns a new 2d size.
    pub fn new(width: f32, height: f32) -> Self {
        Self { width, height }
    }
}

/// The shape of a rigid body.
///
/// Contains a rotation/translation offset and a size.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Properties)]
pub struct Shape {
    offset: Vec2,
    size: Size2,
}

impl Shape {
    /// Return a new `Shape` with a zero offset and a size.
    pub fn new(size: Size2) -> Self {
        let offset = Vec2::zero();
        Self { offset, size }
    }

    /// Return a new `Shape` with an offset and a size.
    pub fn with_offset(mut self, offset: Vec2) -> Self {
        self.offset = offset;
        self
    }
}

impl From<Size2> for Shape {
    fn from(size: Size2) -> Self {
        let x = size.width * 0.5;
        let y = size.height * 0.5;
        let offset = Vec2::new(-x, -y);
        Self { offset, size }
    }
}

#[doc(hidden)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct InnerJoint {
    body1: Entity,
    body2: Entity,
    offset: Vec2,
    angle: f32,
}

impl InnerJoint {
    pub fn new(body1: Entity, body2: Entity) -> Self {
        Self {
            body1,
            body2,
            offset: Vec2::zero(),
            angle: 0.0,
        }
    }

    pub fn with_offset(mut self, offset: Vec2) -> Self {
        self.offset = offset;
        self
    }

    pub fn with_angle(mut self, angle: f32) -> Self {
        self.angle = angle;
        self
    }
}

/// Defines a set of behaviours on how joints should move the anchored body relative to the anchor.
pub trait JointBehaviour: Send + Sync + 'static {
    const STAGE: &'static str = stage::COLLIDING_JOINT;

    /// Returns a new position for target based on `self` and `anchor`.
    fn position(
        &mut self,
        _offset: Vec2,
        _anchor: &RigidBody,
        _target: &RigidBody,
    ) -> Option<Vec2> {
        None
    }

    /// Returns a new rotation for target based on `self` and `anchor`.
    fn rotation(&mut self, _angle: f32, _anchor: &RigidBody, _target: &RigidBody) -> Option<f32> {
        None
    }

    /// Returns a new linear velocity for target based on `self` and `anchor`.
    fn linear_velocity(
        &mut self,
        _offset: Vec2,
        _anchor: &RigidBody,
        _target: &RigidBody,
    ) -> Option<Vec2> {
        None
    }

    /// Returns a new angular velocity for target based on `self` and `anchor`.
    fn angular_velocity(
        &mut self,
        _angle: f32,
        _anchor: &RigidBody,
        _target: &RigidBody,
    ) -> Option<f32> {
        None
    }

    /// Returns a linear impulse to apply to target based on `self` and `anchor`.
    fn linear_impulse(
        &mut self,
        _offset: Vec2,
        _anchor: &RigidBody,
        _target: &RigidBody,
    ) -> Option<Vec2> {
        None
    }

    /// Returns an angular impulse to apply to target based on `self` and `anchor`.
    fn angular_impulse(
        &mut self,
        _angle: f32,
        _anchor: &RigidBody,
        _target: &RigidBody,
    ) -> Option<f32> {
        None
    }
}

/// A joint behaviour that causes the anchored body to be rigidly fixed at an offset and an angle.
#[derive(Default, Debug, Clone, Copy, PartialEq)]
pub struct FixedJointBehaviour;

impl JointBehaviour for FixedJointBehaviour {
    const STAGE: &'static str = stage::RIGID_JOINT;

    fn position(&mut self, offset: Vec2, anchor: &RigidBody, _target: &RigidBody) -> Option<Vec2> {
        Some(anchor.position + offset)
    }

    fn rotation(&mut self, angle: f32, anchor: &RigidBody, _target: &RigidBody) -> Option<f32> {
        Some(anchor.rotation + angle)
    }
}

/// A joint behaviour that causes the anchored body to be accurately positioned with an offset and an angle.
#[derive(Default, Debug, Clone, Copy, PartialEq)]
pub struct MechanicalJointBehaviour;

impl JointBehaviour for MechanicalJointBehaviour {
    const STAGE: &'static str = stage::COLLIDING_JOINT;

    fn position(&mut self, offset: Vec2, anchor: &RigidBody, _target: &RigidBody) -> Option<Vec2> {
        Some(anchor.position + offset)
    }

    fn rotation(&mut self, angle: f32, anchor: &RigidBody, _target: &RigidBody) -> Option<f32> {
        Some(anchor.rotation + angle)
    }

    fn linear_velocity(
        &mut self,
        _offset: Vec2,
        _anchor: &RigidBody,
        _target: &RigidBody,
    ) -> Option<Vec2> {
        Some(Vec2::zero())
    }

    fn angular_velocity(
        &mut self,
        _angle: f32,
        _anchor: &RigidBody,
        _target: &RigidBody,
    ) -> Option<f32> {
        Some(0.0)
    }
}

/// A joint behaviour that will move the anchored body into a position and angle relative to the anchor over time.
#[derive(Default, Debug, Clone, Copy, PartialEq)]
pub struct SpringJointBehaviour {
    rigidness: f32,
}

impl SpringJointBehaviour {
    /// Create a new SpringJointBehaviour with an exact rigidness value.
    ///
    /// Rigidness describes how "snappy" the spring joint is. When it's at 0.0,
    /// the anchored body will "jump" into position softly over one second.
    /// When it's at 1.0, the anchored body will "jump" into position almost instantaenously.
    pub fn new(rigidness: f32) -> Option<SpringJointBehaviour> {
        if rigidness < 0.0 || rigidness >= 1.0 {
            None
        } else {
            Some(Self { rigidness })
        }
    }

    pub fn new_lossy(rigidness: f32) -> SpringJointBehaviour {
        Self {
            rigidness: rigidness.max(0.0).min(1.0),
        }
    }
}

impl JointBehaviour for SpringJointBehaviour {
    const STAGE: &'static str = stage::COLLIDING_JOINT;

    fn linear_velocity(
        &mut self,
        _offset: Vec2,
        _anchor: &RigidBody,
        _target: &RigidBody,
    ) -> Option<Vec2> {
        Some(Vec2::zero())
    }

    fn angular_velocity(
        &mut self,
        _angle: f32,
        _anchor: &RigidBody,
        _target: &RigidBody,
    ) -> Option<f32> {
        Some(0.0)
    }

    fn linear_impulse(
        &mut self,
        offset: Vec2,
        anchor: &RigidBody,
        target: &RigidBody,
    ) -> Option<Vec2> {
        // the minimum time to "jump" into position
        const EPSILON: f32 = 0.1;
        // the maximum time to "jump" into position
        const T: f32 = 1.0;
        let springiness = 1.0 - self.rigidness;
        let position = anchor.position + offset;
        let d = position - target.position;
        let scale = (T - EPSILON) * springiness + EPSILON;
        let impulse = d * target.mass / scale;
        Some(impulse)
    }

    fn angular_impulse(
        &mut self,
        angle: f32,
        anchor: &RigidBody,
        target: &RigidBody,
    ) -> Option<f32> {
        // the minimum time to "jump" into position
        const EPSILON: f32 = 0.1;
        // the maximum time to "jump" into position
        const T: f32 = 1.0;
        let springiness = 1.0 - self.rigidness;
        let rotation = anchor.rotation + angle;
        let d = rotation - target.rotation;
        let scale = (T - EPSILON) * springiness + EPSILON;
        let impulse = d * target.mass / scale;
        Some(impulse)
    }
}

/// Allows one `RigidBody` to be anchored at another one
/// in a fixed way, along with a local offset and angle.
pub type FixedJoint = Joint<FixedJointBehaviour>;

/// Allows one `RigidBody` to be anchored at another one
/// in a physically accurate way, along with a local offset and angle.
pub type MechanicalJoint = Joint<MechanicalJointBehaviour>;

/// Allows one `RigidBody` to be anchored at another one
/// in a spring-y way, along with a local offset and angle.
pub type SpringJoint = Joint<SpringJointBehaviour>;

impl SpringJoint {
    /// Add a rigidness value to an owned `Joint`.
    pub fn with_rigidness(mut self, rigidness: f32) -> Self {
        self.behaviour.rigidness = rigidness;
        self
    }
}

/// Allows one `RigidBody` to be anchored at another one
/// in a pre-defined way, along with a local offset and angle.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Joint<B: JointBehaviour> {
    inner: InnerJoint,
    behaviour: B,
}

impl<B: JointBehaviour + Default> Joint<B> {
    /// Create a new joint, where the second body shall be anchored at the first body.
    pub fn new(body1: Entity, body2: Entity) -> Self {
        Self::with_behaviour(body1, body2, B::default())
    }
}

impl<B: JointBehaviour> Joint<B> {
    /// Create a new joint, where the second body shall be anchored at the first body.
    pub fn with_behaviour(body1: Entity, body2: Entity, behaviour: B) -> Self {
        Self {
            inner: InnerJoint::new(body1, body2),
            behaviour,
        }
    }

    /// Add an offset to an owned `Joint`.
    pub fn with_offset(self, offset: Vec2) -> Self {
        Self {
            inner: self.inner.with_offset(offset),
            behaviour: self.behaviour,
        }
    }

    /// Add an angle to an owned `Joint`.
    pub fn with_angle(self, angle: f32) -> Self {
        Self {
            inner: self.inner.with_angle(angle),
            behaviour: self.behaviour,
        }
    }
}

/// The rigid body.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Properties)]
pub struct RigidBody {
    /// Current position of this rigid body.
    pub position: Vec2,
    lowest_position: Vec2,
    /// Current rotation of this rigid body.
    ///
    /// NOTE: collisions checks may or may not be broken if this is not a multiple of 90 degrees.
    pub rotation: f32,
    /// Current linear velocity of this rigid body.
    pub linvel: Vec2,
    prev_linvel: Vec2,
    /// The terminal linear velocity of a semikinematic body.
    ///
    /// Defaults to `f32::INFINITY`.
    pub terminal: Vec2,
    accumulator: Vec2,
    dynamic_acc: Vec2,
    /// Current angular velocity of this rigid body.
    pub angvel: f32,
    prev_angvel: f32,
    /// The terminal angular velocity of a semikinematic body.
    ///
    /// Defaults to `f32::INFINITY`.
    pub ang_term: f32,
    /// The status, i.e. static or semikinematic.
    ///
    /// Affects how forces and collisions affect this rigid body.
    pub status: Status,
    mass: f32,
    inv_mass: f32,
    active: bool,
    sensor: bool,
}

impl RigidBody {
    /// Returns a new `RigidBody` with just a mass and all other components set to their defaults.
    pub fn new(mass: Mass) -> Self {
        Self {
            position: Vec2::zero(),
            lowest_position: Vec2::zero(),
            rotation: 0.0,
            linvel: Vec2::zero(),
            prev_linvel: Vec2::zero(),
            terminal: Vec2::new(f32::INFINITY, f32::INFINITY),
            accumulator: Vec2::zero(),
            dynamic_acc: Vec2::zero(),
            angvel: 0.0,
            prev_angvel: 0.0,
            ang_term: f32::INFINITY,
            status: Status::Semikinematic,
            mass: mass.scalar(),
            inv_mass: mass.inverse(),
            active: true,
            sensor: false,
        }
    }

    /// Returns a `RigidBody` identical to this one, but with the position set to a new one.
    pub fn with_position(mut self, position: Vec2) -> Self {
        self.position = position;
        self
    }

    /// Returns a `RigidBody` identical to this one, but with the rotation set to a new one.
    pub fn with_rotation(mut self, rotation: f32) -> Self {
        self.rotation = rotation;
        self
    }

    /// Returns a `RigidBody` identical to this one, but with the linear velocity set to a new one.
    pub fn with_linear_velocity(mut self, linvel: Vec2) -> Self {
        self.linvel = linvel;
        self
    }

    /// Returns a `RigidBody` identical to this one, but with the linear velocity set to a new one.
    pub fn with_angular_velocity(mut self, angvel: f32) -> Self {
        self.angvel = angvel;
        self
    }

    /// Returns a `RigidBody` identical to this one, but with the terminal linear velocity set to a new one.
    pub fn with_terminal(mut self, terminal: Vec2) -> Self {
        self.terminal = terminal;
        self
    }

    /// Returns a `RigidBody` identical to this one, but with the terminal linear velocity set to a new one.
    pub fn with_angular_terminal(mut self, terminal: f32) -> Self {
        self.ang_term = terminal;
        self
    }

    /// Returns a `RigidBody` identical to this one, but with the acceleration set to a new one.
    pub fn with_acceleration(mut self, acceleration: Vec2) -> Self {
        self.accumulator = acceleration;
        self
    }

    /// Returns a `RigidBody` identical to this one, but with the status set to a new one.
    pub fn with_status(mut self, status: Status) -> Self {
        self.status = status;
        self
    }

    /// Returns a `RigidBody` identical to this one, but with the active flag set to a new one.
    pub fn with_active(mut self, active: bool) -> Self {
        self.active = active;
        self
    }

    /// Returns a `RigidBody` identical to this one, but with the sensor flag set to a new one.
    pub fn with_sensor(mut self, sensor: bool) -> Self {
        self.sensor = sensor;
        self
    }

    /// Applies an impulse to the `RigidBody`s linear velocity.
    pub fn apply_linear_impulse(&mut self, impulse: Vec2) {
        self.linvel += impulse * self.inv_mass;
    }

    /// Applies an impulse to the `RigidBody`s linear velocity.
    pub fn apply_angular_impulse(&mut self, impulse: f32) {
        self.angvel += impulse * self.inv_mass;
    }

    /// Applies a force to the `RigidBody`s acceleration accumulator.
    pub fn apply_force(&mut self, force: Vec2) {
        self.accumulator += force * self.inv_mass;
    }

    /// Gets the active flag.
    pub fn is_active(&self) -> bool {
        self.active
    }

    /// Gets the sensor flag.
    pub fn is_sensor(&self) -> bool {
        self.sensor
    }

    /// Gets the mass
    pub fn mass(&self) -> f32 {
        self.mass
    }

    /// Gets the mass
    pub fn inverse_mass(&self) -> f32 {
        self.inv_mass
    }

    /// Sets the active flag.
    pub fn set_active(&mut self, active: bool) {
        self.active = active;
    }

    /// Sets the sensor flag.
    pub fn set_sensor(&mut self, sensor: bool) {
        self.sensor = sensor;
    }

    /// Sets the mass.
    pub fn set_mass(&mut self, mass: Mass) {
        self.mass = mass.scalar();
        self.inv_mass = mass.inverse();
    }
}

/// The manifold, representing detailed data on a collision between two `RigidBody`s.
///
/// Usable as an event.
#[derive(Debug, Clone)]
pub struct Manifold {
    /// The first entity.
    pub body1: Entity,
    /// The second entity.
    pub body2: Entity,
    /// The penetration, relative to the second entity.
    pub penetration: f32,
    /// The normal, relative to the second entity.
    pub normal: Vec2,
    /// The contact points of this manifold.
    pub contacts: SmallVec<[Vec2; 4]>,
}

pub fn broad_phase_system(
    mut commands: Commands,
    mut query: Query<(Entity, &RigidBody, &Children)>,
    query2: Query<&Shape>,
) {
    let mut colliders = Vec::new();
    for (entity, body, children) in &mut query.iter() {
        for &e in children.iter() {
            if let Ok(shape) = query2.get::<Shape>(e) {
                let v0 = shape.offset;
                let v1 = shape.offset + Vec2::new(shape.size.width, 0.0);
                let v2 = shape.offset + Vec2::new(shape.size.width, shape.size.height);
                let v3 = shape.offset + Vec2::new(0.0, shape.size.height);
                let rotation = Mat2::from_angle(body.rotation);
                let position = body.position;
                let n0 = Vec2::new(1.0, 0.0);
                let n1 = Vec2::new(0.0, 1.0);
                let collider = Obb::new(
                    body.status,
                    entity,
                    rotation,
                    position,
                    v0,
                    v1,
                    v2,
                    v3,
                    n0,
                    n1,
                );
                colliders.push(collider);
            }
        }
    }
    let broad = BroadPhase::with_colliders(colliders);
    commands.insert_resource(broad);
}

#[derive(Default)]
pub struct NarrowPhase {
    set: HashSet<[Entity; 2]>,
}

impl NarrowPhase {
    pub fn system(self, res: &mut Resources) -> Box<dyn System> {
        let system = narrow_phase_system.system();
        res.insert_local(system.id(), self);
        system
    }
}

fn bias_greater_than(a: f32, b: f32) -> bool {
    const BIAS_RELATIVE: f32 = 0.95;
    const BIAS_ABSOLUTE: f32 = 0.01;
    a >= b * BIAS_RELATIVE + a * BIAS_ABSOLUTE
}

fn find_axis_of_least_penetration(a: &Obb, b: &Obb) -> (f32, usize) {
    let mut best_distance = f32::MIN;
    let mut best_index = 0;

    for i in 0..4 {
        let n = a.normals[i];
        let nw = a.rotation * n;

        let brt = b.rotation.transpose();
        let n = brt * nw;

        let s = b.get_support(-n);

        let mut v = a.vertices[i];
        v = a.rotation * v + a.position;
        v -= b.position;
        v = brt * v;

        let d = n.dot(s - v);

        if d > best_distance {
            best_distance = d;
            best_index = i;
        }
    }

    (best_distance, best_index)
}

fn find_incident_face(ref_poly: &Obb, inc_poly: &Obb, idx: usize) -> [Vec2; 2] {
    let mut ref_normal = ref_poly.normals[idx];

    ref_normal = ref_poly.rotation * ref_normal;
    ref_normal = inc_poly.rotation.transpose() * ref_normal;

    let mut incident_face = 0;
    let mut min_dot = f32::MAX;

    for i in 0..4 {
        let dot = ref_normal.dot(inc_poly.normals[i]);
        if dot < min_dot {
            min_dot = dot;
            incident_face = i;
        }
    }

    let v0 = inc_poly.rotation * inc_poly.vertices[incident_face] + inc_poly.position;
    incident_face = (incident_face + 1) & 0x3;
    let v1 = inc_poly.rotation * inc_poly.vertices[incident_face] + inc_poly.position;
    [v0, v1]
}

fn clip(n: Vec2, c: f32, face: &mut [Vec2]) -> usize {
    let mut sp = 0;
    let mut out = [face[0], face[1]];

    let d1 = n.dot(face[0]) - c;
    let d2 = n.dot(face[1]) - c;

    if d1 <= 0.0 {
        out[sp] = face[0];
        sp += 1;
    }

    if d2 <= 0.0 {
        out[sp] = face[1];
        sp += 1;
    }

    if d1 * d2 < 0.0 {
        let alpha = d1 / (d1 - d2);
        out[sp] = face[0] + alpha * (face[1] - face[0]);
        sp += 1;
    }

    face[0] = out[0];
    face[1] = out[1];

    debug_assert_ne!(sp, 3);

    sp
}

fn narrow_phase_system(
    mut state: Local<NarrowPhase>,
    mut manifolds: ResMut<Events<Manifold>>,
    broad: Res<BroadPhase>,
) {
    state.set.clear();
    let narrow = broad.iter();
    for (collider1, collider2) in narrow {
        if collider1.body == collider2.body {
            continue;
        }

        if state.set.contains(&[collider2.body, collider1.body]) {
            continue;
        }
        state.set.insert([collider1.body, collider2.body]);

        let (penetration_a, face_a) = find_axis_of_least_penetration(&collider1, &collider2);
        if penetration_a >= 0.0 {
            continue;
        }

        let (penetration_b, face_b) = find_axis_of_least_penetration(&collider2, &collider1);
        if penetration_b >= 0.0 {
            continue;
        }

        let mut ref_index;
        let flip;
        let ref_poly;
        let inc_poly;

        if bias_greater_than(penetration_a, penetration_b) {
            ref_poly = &collider1;
            inc_poly = &collider2;
            ref_index = face_a;
            flip = false;
        } else {
            ref_poly = &collider2;
            inc_poly = &collider1;
            ref_index = face_b;
            flip = true;
        }

        let mut incident_face = find_incident_face(ref_poly, inc_poly, ref_index);

        let mut v1 = ref_poly.vertices[ref_index];
        ref_index = (ref_index + 1) & 0x3;
        let mut v2 = ref_poly.vertices[ref_index];

        v1 = ref_poly.rotation * v1 + ref_poly.position;
        v2 = ref_poly.rotation * v2 + ref_poly.position;

        let side_plane_normal = (v2 - v1).normalize();

        let ref_face_normal = Vec2::new(side_plane_normal.y(), -side_plane_normal.x());

        let refc = ref_face_normal.dot(v1);
        let negside = -side_plane_normal.dot(v1);
        let posside = side_plane_normal.dot(v2);

        if clip(-side_plane_normal, negside, &mut incident_face) < 2 {
            continue;
        }

        if clip(side_plane_normal, posside, &mut incident_face) < 2 {
            continue;
        }

        let normal = if flip {
            -ref_face_normal
        } else {
            ref_face_normal
        };
        let mut penetration = 0.0;

        let mut contacts = SmallVec::new();

        let mut cp = 0;
        let sep = ref_face_normal.dot(incident_face[0]) - refc;
        if sep <= 0.0 {
            contacts.push(incident_face[0]);
            penetration = -sep;
            cp += 1;
        }

        let sep = ref_face_normal.dot(incident_face[1]) - refc;
        if sep <= 0.0 {
            contacts.push(incident_face[1]);
            penetration += -sep;
            cp += 1;
            penetration /= cp as f32;
        }

        let manifold = Manifold {
            body1: collider1.body,
            body2: collider2.body,
            penetration,
            normal,
            contacts,
        };
        manifolds.send(manifold);
    }
}

#[derive(Default)]
pub struct Solver {
    reader: EventReader<Manifold>,
}

impl Solver {
    pub fn system(self, res: &mut Resources) -> Box<dyn System> {
        let system = solve_system.system();
        res.insert_local(system.id(), self);
        system
    }
}

fn solve_system(
    mut solver: Local<Solver>,
    time: Res<Time>,
    manifolds: Res<Events<Manifold>>,
    step: Res<GlobalStep>,
    up: Res<GlobalUp>,
    ang_tol: Res<AngularTolerance>,
    query: Query<Mut<RigidBody>>,
) {
    let delta_time = time.delta.as_secs_f32();

    for manifold in solver.reader.iter(&manifolds) {
        let a = query.get::<RigidBody>(manifold.body1).unwrap();
        let b = query.get::<RigidBody>(manifold.body2).unwrap();

        if a.sensor || b.sensor {
            continue;
        }

        let dynamics = if a.status == Status::Semikinematic && b.status == Status::Semikinematic {
            let push_angle = up.0.abs().dot(manifold.normal.abs()).acos();
            if push_angle > ang_tol.0 {
                let sum_recip = (a.mass + b.mass).recip();
                let br = b.linvel * b.mass;
                let ar = a.linvel * a.mass;
                let rv = br * sum_recip - ar * sum_recip;

                let impulse = -rv * manifold.normal.abs();

                let a = a.linvel - impulse;
                let b = b.linvel + impulse;
                Some((a, b))
            } else {
                None
            }
        } else {
            None
        };
        mem::drop(a);
        mem::drop(b);

        let mut a = query.get_mut::<RigidBody>(manifold.body1).unwrap();
        match a.status {
            Status::Static => {}
            Status::Semikinematic => {
                if let Some((impulse, _)) = dynamics {
                    a.dynamic_acc += impulse;

                    let d = -manifold.normal * manifold.penetration;
                    let v = a.linvel * delta_time;
                    if v.sign() == d.sign() {
                        // nothing
                    } else {
                        a.linvel *= Vec2::new(manifold.normal.y().abs(), manifold.normal.x().abs());
                        a.position += d;
                    }
                } else {
                    let mut solve = true;
                    let step_angle = up.0.abs().dot(manifold.normal.abs()).acos();
                    if step_angle > ang_tol.0 {
                        let up_vector = up.0;
                        if up_vector.length_squared() != 0.0 {
                            for &point in &manifold.contacts {
                                let d = point - a.lowest_position;
                                let s = d.dot(up_vector);
                                if s < step.0 {
                                    let diff = a.position - a.lowest_position;
                                    a.lowest_position += up_vector * s;
                                    a.position = a.lowest_position + diff;
                                    solve = false;
                                }
                            }
                        }
                    }

                    if solve {
                        let d = -manifold.normal * manifold.penetration;
                        let v = a.linvel * delta_time;
                        if v.sign() == d.sign() {
                            // nothing
                        } else {
                            a.linvel *=
                                Vec2::new(manifold.normal.y().abs(), manifold.normal.x().abs());
                            a.position += d;
                        }
                    }
                }
            }
        }
        mem::drop(a);

        let mut b = query.get_mut::<RigidBody>(manifold.body2).unwrap();
        match b.status {
            Status::Static => {}
            Status::Semikinematic => {
                if let Some((_, impulse)) = dynamics {
                    b.dynamic_acc += impulse;

                    let d = manifold.normal * manifold.penetration;
                    let v = b.linvel * delta_time;
                    if v.sign() == d.sign() {
                        // nothing
                    } else {
                        b.linvel *= Vec2::new(manifold.normal.y().abs(), manifold.normal.x().abs());
                        b.position += d;
                    }
                } else {
                    let mut solve = true;
                    let step_angle = up.0.abs().dot(manifold.normal.abs()).acos();
                    if step_angle > ang_tol.0 {
                        let up_vector = up.0;
                        if up_vector.length_squared() != 0.0 {
                            for &point in &manifold.contacts {
                                let d = point - b.lowest_position;
                                let s = d.dot(up_vector);
                                if s < step.0 {
                                    let diff = b.position - b.lowest_position;
                                    b.lowest_position += up_vector * s;
                                    b.position = b.lowest_position + diff;
                                    solve = false;
                                }
                            }
                        }
                    }

                    if solve {
                        let d = manifold.normal * manifold.penetration;
                        let v = b.linvel * delta_time;
                        if v.sign() == d.sign() {
                            // nothing
                        } else {
                            b.linvel *=
                                Vec2::new(manifold.normal.y().abs(), manifold.normal.x().abs());
                            b.position += d;
                        }
                    }
                }
            }
        }
        mem::drop(b);
    }
}

pub struct PhysicsStep {
    skip: usize,
}

impl PhysicsStep {
    pub fn system(self, res: &mut Resources) -> Box<dyn System> {
        let system = physics_step_system.system();
        res.insert_local(system.id(), self);
        system
    }
}

impl Default for PhysicsStep {
    fn default() -> Self {
        PhysicsStep { skip: 3 }
    }
}

fn physics_step_system(
    mut state: Local<PhysicsStep>,
    time: Res<Time>,
    friction: Res<GlobalFriction>,
    gravity: Res<GlobalGravity>,
    up: Res<GlobalUp>,
    mut query: Query<(Mut<RigidBody>, &Children)>,
    shapes: Query<&Shape>,
) {
    if state.skip > 0 {
        state.skip -= 1;
        return;
    }

    let delta_time = time.delta.as_secs_f32();

    for (mut body, children) in &mut query.iter() {
        if !body.active {
            continue;
        }

        if !matches!(body.status, Status::Static) {
            body.accumulator += gravity.0;
        }

        let linvel = body.linvel + body.accumulator * delta_time;
        let linvel = linvel + body.dynamic_acc;
        body.linvel = linvel;
        body.accumulator = Vec2::zero();
        body.dynamic_acc = Vec2::zero();

        if matches!(body.status, Status::Semikinematic) {
            let vel = body.linvel;
            let limit = body.terminal;
            match vel.x().partial_cmp(&0.0) {
                Some(Ordering::Less) => *body.linvel.x_mut() = vel.x().max(-limit.x()),
                Some(Ordering::Greater) => *body.linvel.x_mut() = vel.x().min(limit.x()),
                Some(Ordering::Equal) => {}
                None => *body.linvel.x_mut() = 0.0,
            }
            match vel.y().partial_cmp(&0.0) {
                Some(Ordering::Less) => *body.linvel.y_mut() = vel.y().max(-limit.y()),
                Some(Ordering::Greater) => *body.linvel.y_mut() = vel.y().min(limit.y()),
                Some(Ordering::Equal) => {}
                None => *body.linvel.y_mut() = 0.0,
            }
            let vel = body.angvel;
            let limit = body.ang_term;
            match vel.partial_cmp(&0.0) {
                Some(Ordering::Less) => body.angvel = vel.max(-limit),
                Some(Ordering::Greater) => body.angvel = vel.min(limit),
                Some(Ordering::Equal) => {}
                None => body.angvel = 0.0,
            }
        }

        let position = body.position + body.linvel * delta_time;
        body.position = position;

        let rotation = body.rotation + body.angvel * delta_time;
        body.rotation = rotation;

        match body.status {
            Status::Semikinematic => {
                if body.linvel.x().abs() <= body.prev_linvel.x().abs() {
                    *body.linvel.x_mut() *= friction.0
                }
                if body.linvel.y().abs() <= body.prev_linvel.y().abs() {
                    *body.linvel.y_mut() *= friction.0
                }
                if body.angvel.abs() <= body.prev_angvel.abs() {
                    body.angvel *= friction.0
                }
            }
            Status::Static => {}
        }
        body.prev_linvel = body.linvel;
        body.prev_angvel = body.angvel;

        for &child in children.iter() {
            if let Ok(shape) = shapes.get::<Shape>(child) {
                let v0 = shape.offset;
                let v1 = shape.offset + Vec2::new(shape.size.width, 0.0);
                let v2 = shape.offset + Vec2::new(shape.size.width, shape.size.height);
                let v3 = shape.offset + Vec2::new(0.0, shape.size.height);
                let rotation = Mat2::from_angle(body.rotation);
                let position = body.position;
                let v0 = rotation * v0;
                let v1 = rotation * v1;
                let v2 = rotation * v2;
                let v3 = rotation * v3;
                let s0 = v0.dot(up.0);
                let s1 = v1.dot(up.0);
                let s2 = v2.dot(up.0);
                let s3 = v3.dot(up.0);
                let v = [v0, v1, v2, v3];
                let s = [s0, s1, s2, s3];
                let min = s0.min(s1).min(s2).min(s3);
                let mut lowest_point = Vec2::zero();
                let mut count = 0;
                for (&v, &s) in v.iter().zip(&s) {
                    if s == min {
                        lowest_point += v;
                        count += 1;
                    }
                }
                body.lowest_position = position + lowest_point / count as f32;
            }
        }
    }
}

pub fn joint_system<B: JointBehaviour>(
    mut commands: Commands,
    mut query: Query<(Entity, Mut<Joint<B>>)>,
    bodies: Query<Mut<RigidBody>>,
) {
    for (e, mut joint) in &mut query.iter() {
        let anchor = if let Ok(anchor) = bodies.get::<RigidBody>(joint.inner.body1) {
            anchor
        } else {
            commands.despawn_recursive(e);
            continue;
        };
        let target = if let Ok(target) = bodies.get::<RigidBody>(joint.inner.body2) {
            target
        } else {
            commands.despawn_recursive(e);
            continue;
        };
        let offset = joint.inner.offset;
        let angle = joint.inner.angle;
        let position = joint.behaviour.position(offset, &anchor, &target);
        let rotation = joint.behaviour.rotation(angle, &anchor, &target);
        let linvel = joint.behaviour.linear_velocity(offset, &anchor, &target);
        let angvel = joint.behaviour.angular_velocity(angle, &anchor, &target);
        let linimp = joint.behaviour.linear_impulse(offset, &anchor, &target);
        let angimp = joint.behaviour.angular_impulse(angle, &anchor, &target);

        mem::drop(anchor);
        mem::drop(target);

        let mut target = bodies.get_mut::<RigidBody>(joint.inner.body2).unwrap();

        if let Some(position) = position {
            target.position = position;
        }

        if let Some(rotation) = rotation {
            target.rotation = rotation;
        }

        if let Some(linvel) = linvel {
            target.linvel = linvel;
        }

        if let Some(angvel) = angvel {
            target.angvel = angvel;
        }

        if let Some(linimp) = linimp {
            target.apply_linear_impulse(linimp);
        }

        if let Some(angimp) = angimp {
            target.apply_angular_impulse(angimp);
        }
    }
}

/// The plane on which to translate the 2d position into 3d coordinates.
#[derive(Debug, Clone, Copy)]
pub enum TranslationMode {
    AxesXY,
    AxesXZ,
    AxesYZ,
}

impl Default for TranslationMode {
    fn default() -> Self {
        Self::AxesXY
    }
}

/// The axis on which to rotate the 2d rotation into a 3d quaternion.
#[derive(Debug, Clone, Copy)]
pub enum RotationMode {
    AxisX,
    AxisY,
    AxisZ,
}

impl Default for RotationMode {
    fn default() -> Self {
        Self::AxisZ
    }
}

pub fn sync_transform_system(
    translation_mode: Res<TranslationMode>,
    rotation_mode: Res<RotationMode>,
    mut query: Query<(&RigidBody, Mut<Transform>)>,
) {
    for (body, mut transform) in &mut query.iter() {
        match *translation_mode {
            TranslationMode::AxesXY => {
                let x = body.position.x();
                let y = body.position.y();
                let z = 0.0;
                transform.set_translation(Vec3::new(x, y, z));
            }
            TranslationMode::AxesXZ => {
                let x = body.position.x();
                let y = 0.0;
                let z = body.position.y();
                transform.set_translation(Vec3::new(x, y, z));
            }
            TranslationMode::AxesYZ => {
                let x = 0.0;
                let y = body.position.x();
                let z = body.position.y();
                transform.set_translation(Vec3::new(x, y, z));
            }
        }
        match *rotation_mode {
            RotationMode::AxisX => {
                transform.set_rotation(Quat::from_rotation_x(body.rotation));
            }
            RotationMode::AxisY => {
                transform.set_rotation(Quat::from_rotation_y(body.rotation));
            }
            RotationMode::AxisZ => {
                transform.set_rotation(Quat::from_rotation_z(body.rotation));
            }
        }
    }
}