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
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
//! Collision detection implementation.

use std::hash::BuildHasherDefault;

use indexmap::IndexMap;

use rapier::Vector;
use rapier2d::geometry::InteractionGroups;
pub use rapier2d::prelude as rapier;
pub use shape::*;

pub mod filtering;
mod shape;

use crate::collisions::filtering::CollisionGroup;
use crate::collisions::filtering::SolverGroup;
use crate::impl_system_param;
use crate::prelude::*;

/// Resource containing the data structures needed for rapier collision detection.
#[derive(HasSchema, Default)]
pub struct RapierContext {
    pub collision_pipeline: rapier::CollisionPipeline,
    pub broad_phase: rapier::DefaultBroadPhase,
    pub narrow_phase: rapier::NarrowPhase,
    pub query_pipeline: rapier::QueryPipeline,
    pub collider_set: rapier::ColliderSet,
    pub rigid_body_set: rapier::RigidBodySet,
    pub collider_shape_cache: ColliderShapeCache,
    pub collision_cache: CollisionCache,
    pub physics_hooks: PhysicsHooks,
    pub physics_pipeline: rapier::PhysicsPipeline,
    pub islands: rapier::IslandManager,
    pub impulse_joints: rapier::ImpulseJointSet,
    pub ccd_solver: rapier::CCDSolver,
    pub multibody_joints: rapier::MultibodyJointSet,
    pub integration_params: rapier::IntegrationParameters,
}

impl Clone for RapierContext {
    fn clone(&self) -> Self {
        Self {
            // The collision pipeline is just a cache and while we can't clone it, creating a new one is
            // perfectly valid.
            collision_pipeline: default(),
            broad_phase: self.broad_phase.clone(),
            narrow_phase: self.narrow_phase.clone(),
            query_pipeline: self.query_pipeline.clone(),
            collider_set: self.collider_set.clone(),
            rigid_body_set: self.rigid_body_set.clone(),
            collider_shape_cache: self.collider_shape_cache.clone(),
            collision_cache: self.collision_cache.clone(),
            physics_hooks: self.physics_hooks.clone(),
            // Probably should keep this around, it's safe to drop data as only temp buffers,
            // but re-creating may hurt performance.
            physics_pipeline: rapier::PhysicsPipeline::default(),
            islands: self.islands.clone(),
            impulse_joints: self.impulse_joints.clone(),
            ccd_solver: self.ccd_solver.clone(),
            multibody_joints: self.multibody_joints.clone(),
            integration_params: self.integration_params,
        }
    }
}

/// A cache containing a map of entities, to the list of entities that each entity is currently
/// intersecting with.
pub struct CollisionCache {
    /// The collisions in the cache.
    pub collisions: Arc<AtomicCell<IndexMap<Entity, Vec<Entity>, EntityBuildHasher>>>,

    /// Map removed collider handles to entities. When Rapier gives collision event for removal,
    /// the collider is no longer in [`rapier::ColliderSet`], thus we cannot retrieve our user data
    /// and determine entity to be removed.
    ///
    /// Colliders should be added here on removal for event processing, this is cleared each frame.
    ///
    /// TODO: Consider a safer way to handle this that doesn't involve remembering to update this on
    /// removal.
    removed_colliders: IndexMap<rapier::ColliderHandle, Entity>,
}

impl Default for CollisionCache {
    fn default() -> Self {
        Self {
            collisions: Arc::new(AtomicCell::new(IndexMap::default())),
            removed_colliders: Default::default(),
        }
    }
}

/// Pass-through hasher for entities to reduce hashing cost when using them as keys in a hash map.
#[derive(Default)]
pub struct EntityHasher {
    bytes_so_far: usize,
    data: [u8; 8],
}
type EntityBuildHasher = BuildHasherDefault<EntityHasher>;

impl std::hash::Hasher for EntityHasher {
    fn finish(&self) -> u64 {
        u64::from_ne_bytes(self.data)
    }
    fn write(&mut self, bytes: &[u8]) {
        if self.bytes_so_far + bytes.len() <= 8 {
            self.data[self.bytes_so_far..(self.bytes_so_far + bytes.len())].copy_from_slice(bytes);
            self.bytes_so_far += bytes.len()
        } else {
            panic!("Too much data for `EntityHasher`. Will only accept 64 bits.")
        }
    }
}

impl CollisionCache {
    /// Get the set of entities that the given `entity` is intersecting.
    pub fn get(&self, entity: Entity) -> RefMut<'_, Vec<Entity>> {
        RefMut::map(self.collisions.borrow_mut(), |x| {
            x.entry(entity).or_default()
        })
    }

    /// Notify cache of removal of collider to correctly handle stop event on removed collider.
    /// see `Self::removed_colliders_userdata` field comment for details.
    pub fn collider_removed(&mut self, entity: Entity, collider_handle: rapier::ColliderHandle) {
        self.removed_colliders.insert(collider_handle, entity);
    }

    /// Clear tracked data for removed colliders this frame (call after rapier update)
    pub fn clear_removed_colliders(&mut self) {
        self.removed_colliders.clear();
    }
}

impl Clone for CollisionCache {
    fn clone(&self) -> Self {
        Self {
            collisions: Arc::new(AtomicCell::new((*self.collisions.borrow()).clone())),
            removed_colliders: self.removed_colliders.clone(),
        }
    }
}

/// Update the collision cache with rapier collision events.
impl rapier::EventHandler for &mut CollisionCache {
    fn handle_collision_event(
        &self,
        _bodies: &rapier::RigidBodySet,
        colliders: &rapier::ColliderSet,
        event: rapier::CollisionEvent,
        _contact_pair: Option<&rapier::ContactPair>,
    ) {
        match event {
            rapier::CollisionEvent::Started(a, b, _) => {
                let a_ent = RapierUserData::entity(colliders.get(a).unwrap().user_data);
                let b_ent = RapierUserData::entity(colliders.get(b).unwrap().user_data);

                self.collisions
                    .borrow_mut()
                    .entry(a_ent)
                    .or_default()
                    .push(b_ent);
                self.collisions
                    .borrow_mut()
                    .entry(b_ent)
                    .or_default()
                    .push(a_ent);
            }
            rapier::CollisionEvent::Stopped(a, b, _) => {
                let a_ent = match colliders.get(a) {
                    Some(a) => RapierUserData::entity(a.user_data),
                    None => {
                        if let Some(a_ent) = self.removed_colliders.get(&a) {
                            *a_ent
                        } else {
                            return;
                        }
                    }
                };
                let b_ent = match colliders.get(b) {
                    Some(b) => RapierUserData::entity(b.user_data),
                    None => {
                        if let Some(b_ent) = self.removed_colliders.get(&b) {
                            *b_ent
                        } else {
                            return;
                        }
                    }
                };

                self.collisions
                    .borrow_mut()
                    .entry(a_ent)
                    .or_default()
                    .retain(|e| e != &b_ent);
                self.collisions
                    .borrow_mut()
                    .entry(b_ent)
                    .or_default()
                    .retain(|e| e != &a_ent);
            }
        }
    }

    fn handle_contact_force_event(
        &self,
        _dt: rapier::Real,
        _bodies: &rapier::RigidBodySet,
        _colliders: &rapier::ColliderSet,
        _contact_pair: &rapier::ContactPair,
        _total_force_magnitude: rapier::Real,
    ) {
    }
}

/// Errors produced from physics system
#[derive(thiserror::Error, Debug)]
pub enum PhysicsError {
    #[error("Physics body not initialized: {0}")]
    BodyNotInitialized(String),
}

#[derive(Default, Clone)]
pub struct PhysicsHooks;

impl rapier::PhysicsHooks for PhysicsHooks {
    fn filter_contact_pair(
        &self,
        _context: &rapier::PairFilterContext,
    ) -> Option<rapier::SolverFlags> {
        // No contact pair filtering hook currently implemented
        Some(rapier::SolverFlags::COMPUTE_IMPULSES)
    }

    fn filter_intersection_pair(&self, _context: &rapier::PairFilterContext) -> bool {
        // No intersection pair hook currently implemented
        true
    }

    fn modify_solver_contacts(&self, context: &mut rapier::ContactModificationContext) {
        // Determine if jump through modifiation is needed:
        // (If one body is a player, and other is jump through tile.)

        let collider1 = context.colliders.get(context.collider1).unwrap();
        let body1 = context.bodies.get(context.rigid_body1.unwrap()).unwrap();
        let collider2 = context.colliders.get(context.collider2).unwrap();
        let body2 = context.bodies.get(context.rigid_body2.unwrap()).unwrap();

        let mut jump_through_body: Option<&rapier::RigidBody> = None;
        let mut other_body: Option<&rapier::RigidBody> = None;

        // Determine which body is jump through collider, if any.
        if collider1
            .solver_groups()
            .memberships
            .intersects(SolverGroup::JUMP_THROUGH.bits().into())
        {
            jump_through_body = Some(body1);
            other_body = Some(body2);
        } else if collider2
            .solver_groups()
            .memberships
            .intersects(SolverGroup::JUMP_THROUGH.bits().into())
        {
            jump_through_body = Some(body2);
            other_body = Some(body1);
        }

        if jump_through_body.is_some() {
            let other_body = other_body.unwrap();

            if other_body.linvel().y > 0.0 {
                context.solver_contacts.clear();
            }
        }
    }
}

impl_system_param! {
    pub struct CollisionWorld<'a> {
        entities: Res<'a, Entities>,

        /// The rapier context.
        ctx: ResMutInit<'a, RapierContext>,

        /// Actors are things like players that move around and detect collisions, but don't collide
        /// with other actors.
        actors: CompMut<'a, Actor>,
        /// Solids are things like walls and platforms, that aren't tiles, that have solid
        /// collisions.
        solids: CompMut<'a, Solid>,
        /// A collider is anything that can detect collisions in the world other than tiles, and
        /// must either be an [`Actor`] or [`Solid`] to participate in collision detection.
        colliders: CompMut<'a, Collider>,
        /// Contains the rapier collider handles for each map tile.
        tile_rapier_handles: CompMut<'a, TileRapierHandle>,

        tile_layers: Comp<'a, TileLayer>,
        tile_collision_kinds: Comp<'a, TileCollisionKind>,
        tile_dynamic_colliders: Comp<'a, TileDynamicCollider>,
        spawned_map_layer_metas: Comp<'a, SpawnedMapLayerMeta>,
    }
}

impl<'a> CollisionWorld<'a> {
    /// Update shape of actor's [`Collider`]. Warns if entity does not have an [`Actor`] component.
    ///
    /// Updates shape on `Collider` and rebuilds rapier's collider on rigidbody.
    ///
    /// Use [`Self::set_actor_shape_from_builder`] for more control over new collider's settings.
    pub fn set_actor_shape(&mut self, entity: Entity, shape: ColliderShape) {
        let shared_shape = self.ctx.collider_shape_cache.shared_shape(shape);
        let new_collider = build_actor_rapier_collider(entity, shared_shape.clone());
        self.set_actor_shape_from_builder(entity, new_collider, shape);
    }

    /// Update shape of actor's [`Collider`]. Warns if entity does not have an [`Actor`] component.
    ///
    /// Updates shape on `Collider` and rebuilds rapier's collider on rigidbody.
    ///
    /// Accepts a [`rapier::ColliderBuilder`] (can get one with [`build_actor_rapier_collider`]) so
    /// other settings may be configured on new collider.
    pub fn set_actor_shape_from_builder(
        &mut self,
        entity: Entity,
        mut collider_builder: rapier::ColliderBuilder,
        shape: ColliderShape,
    ) {
        if !self.actors.contains(entity) {
            // This doesn't technically need be restricted, however we use default settings of collider for Actor,
            // and function would need to be updated to do this correctly for Solids, Tiles, or other classes of body.
            warn!("CollisionWorld::set_actor_shape called on entity that is not an Actor.");
            return;
        }

        if let Some(collider) = self.colliders.get_mut(entity) {
            collider.shape = shape;

            if let Some(handle) = collider.rapier_handle {
                let RapierContext {
                    rigid_body_set,
                    collision_cache,
                    collider_set,
                    islands,
                    ..
                } = &mut *self.ctx;

                let rapier_body = rigid_body_set.get_mut(handle).unwrap();

                {
                    let collider_handle = rapier_body.colliders()[0];
                    let current_rapier_collider = collider_set.get(collider_handle).unwrap();

                    // Update new collider with any settings that need to be synchronized
                    collider_builder = collider_builder.sensor(current_rapier_collider.is_sensor());

                    // Remove body's current collider
                    let wake_up = true;
                    collider_set.remove(collider_handle, islands, rigid_body_set, wake_up);

                    // Notify collision event cache handle was removed
                    //
                    // We may get a stop/start even while changing collider. This is required to make sure
                    // collision event is properly handled by rapier.
                    collision_cache.collider_removed(entity, collider_handle);
                }

                // Insert body's new collider
                collider_set.insert_with_parent(collider_builder, handle, rigid_body_set);
            } else {
                // We have an existing Collider but no rapier body yet, shape was updated on Collider
                // and will be used when body is created.
            }
        } else {
            // No existing collider, insert new one with shape.
            // Not really expecting this case to be called, but might as well handle it.
            // rapier body will be created on next call to `sync_colliders`.
            self.colliders.insert(
                entity,
                Collider {
                    shape,
                    ..Default::default()
                },
            );
        }
    }

    /// Call closure with mutable reference to [`rapier::RigidBody`] for entity.
    ///
    /// # Errors
    /// - [`PhysicsError::BodyNotInitialized`] when called before physics is updated to initialize body after
    ///   a [`Collider`] component is newly added. (If missing collider or rapier handle does not map to body).
    pub fn mutate_rigidbody(
        &mut self,
        entity: Entity,
        command: impl FnOnce(&mut rapier::RigidBody),
    ) -> Result<(), PhysicsError> {
        if let Some(collider) = self.colliders.get(entity) {
            if let Some(handle) = collider.rapier_handle {
                if let Some(body) = self.ctx.rigid_body_set.get_mut(handle) {
                    command(body);
                    Ok(())
                } else {
                    Err(PhysicsError::BodyNotInitialized(
                        "Rigidbody handle found but not in rigid body set.".to_string(),
                    ))
                }
            } else {
                Err(PhysicsError::BodyNotInitialized(
                    "Entity has collider that is missing rapier handle.".to_string(),
                ))
            }
        } else {
            Err(PhysicsError::BodyNotInitialized(
                "Entity does not have a Collider component.".to_string(),
            ))
        }
    }
}

/// Helper function for configuring ColliderBuilder for actors.
pub fn build_actor_rapier_collider(
    entity: Entity,
    shared_shape: rapier::SharedShape,
) -> rapier::ColliderBuilder {
    // Do not filter collision pairs, get all collision events
    let collision_membership = CollisionGroup::DEFAULT;
    let collision_filter = CollisionGroup::ALL;
    // Only generate contact forces (when simulating) with solids/tiles, not other dynamics.
    // This is not relevant if only Kinematic, only relevant if DynamicBody is added and switched to simulating.
    let simulation_membership = SolverGroup::DYNAMIC;
    let simulation_filter = SolverGroup::SOLID_WORLD | SolverGroup::JUMP_THROUGH;

    rapier::ColliderBuilder::new(shared_shape)
        .active_events(rapier::ActiveEvents::COLLISION_EVENTS)
        .active_collision_types(rapier::ActiveCollisionTypes::all())
        .collision_groups(rapier::InteractionGroups::new(
            collision_membership.bits().into(),
            collision_filter.bits().into(),
        ))
        .solver_groups(rapier::InteractionGroups::new(
            simulation_membership.bits().into(),
            simulation_filter.bits().into(),
        ))
        .sensor(true)
        .user_data(RapierUserData::from(entity))
}

/// An actor in the physics simulation.
#[derive(Default, Clone, Copy, Debug, HasSchema)]
#[repr(C)]
pub struct Actor;

/// A solid in the physics simulation.
#[derive(Default, Clone, Copy, Debug, HasSchema)]
#[repr(C)]
pub struct Solid {
    pub disabled: bool,
    pub pos: Vec2,
    pub size: Vec2,
    #[schema(opaque)]
    pub rapier_handle: Option<rapier::RigidBodyHandle>,
}

/// A collider body in the physics simulation.
///
/// This is only used for actors in the simulation, not for tiles or solids.
#[derive(Default, Clone, Debug, HasSchema)]
#[repr(C)]
pub struct Collider {
    // TODO: We used to have an offset here in the `Collider` struct, but I think maybe that should
    // become a part of the collision shape, not part of the collider. So if you need an offset
    // collider, maybe that means a compound collider shape with an offset collider in it.
    //
    // When we have a separate offset here, we have to remember and correctly apply the offset,
    // every time we check a collision between this colliders shape, at the colliders transform. It
    // kept causing bugs in colliders with offsets. That may still be the best option, and we just
    // have to deal with it, but we should consider the offset being included in the collider shape.
    pub shape: ColliderShape,
    // Whether or not the collider is disabled.
    pub disabled: bool,
    /// Whether or not the collider wants to drop through jump-through platforms.
    pub descent: bool,
    /// Whether or not the collider is in the process of going through a jump-through platform.
    pub seen_wood: bool,
    /// The handle to the Rapier rigid body associated to this collider, if one has been spawned as
    /// of yet.
    #[schema(opaque)]
    pub rapier_handle: Option<rapier::RigidBodyHandle>,
}

/// Component added to tiles that have been given corresponding rapier colliders.
#[derive(Default, Clone, Debug, HasSchema, Deref, DerefMut)]
pub struct TileRapierHandle(pub rapier::RigidBodyHandle);

/// Component added to tiles that have an additional collider used for interaction with
/// dynamic bodies that simulate physics.
///
/// This collider is added to rapier body stored in [`TileRapierHandle`]. If present,
/// default collider will not interact with dynamics, and this one will.
///
/// This is mostly useful for Jump through tiles.
#[derive(Default, Clone, HasSchema)]
pub struct TileDynamicCollider {
    /// Shape of collider, should be contained within tile.
    pub shape: ColliderShape,

    /// Offset of collider from center of tile.
    pub offset: Vec2,
}

/// Namespace struct for converting rapier collider user data to/from [`Entity`].
pub struct RapierUserData;
impl RapierUserData {
    /// Create rapier user data value from the entity `e`.
    pub fn from(e: Entity) -> u128 {
        let mut out = 0u128;

        out |= e.index() as u128;
        out |= (e.generation() as u128) << 32;

        out
    }

    /// Get an [`Entity`] from the given Rapier user data ( assuming the user data was created with
    /// [`RapierUserData::from`] ).
    pub fn entity(user_data: u128) -> Entity {
        let index = (u32::MAX as u128) & user_data;
        let generation = (u32::MAX as u128) & (user_data >> 32);
        Entity::new(index as u32, generation as u32)
    }
}

/// The kind of collision that a map tile has.
#[derive(Default, PartialEq, Eq, Clone, Copy, Debug, HasSchema, Serialize, Deserialize)]
#[repr(u8)]
#[derive_type_data(SchemaDeserialize)]
pub enum TileCollisionKind {
    #[default]
    Empty,
    Solid,
    JumpThrough,
}

impl TileCollisionKind {
    /// Get the solver group for tile, this is collision filtering group
    /// used against dynamic bodies that simulate physical collision.
    /// Does not impact event filtering.
    pub fn simulation_group_membership(&self) -> SolverGroup {
        match self {
            TileCollisionKind::Empty => SolverGroup::NONE,
            TileCollisionKind::Solid => SolverGroup::SOLID_WORLD,
            TileCollisionKind::JumpThrough => SolverGroup::JUMP_THROUGH,
        }
    }
}

/// Parameters for physics step
pub struct PhysicsParams {
    /// Gravity (positive value is downward force)
    pub gravity: f32,

    /// Terminal velocity (effectively min velocity on y axis, body will not fall faster than this).
    pub terminal_velocity: Option<f32>,
}

impl<'a> CollisionWorld<'a> {
    /// Updates the collision world with the entity's actual transforms.
    /// Advance physics, synchronize position of dynamic bodies.
    ///
    /// If the transform of an entity is changed without calling `update()`, then collision queries
    /// will be out-of-date with the actual entity positions.
    ///
    /// > **⚠️ Warning:** This does **not** update the map tile collisions. To do that, call
    /// > [`update_tiles()`][Self::update_tiles] instead.
    pub fn update(
        &mut self,
        dt: f32,
        physics_params: PhysicsParams,
        transforms: &mut CompMut<Transform>,
        dynamic_bodies: &mut CompMut<DynamicBody>,
    ) {
        puffin::profile_function!();

        self.sync_bodies(&*transforms, dynamic_bodies);
        self.apply_simulation_commands(&mut *dynamic_bodies);

        let RapierContext {
            broad_phase,
            collider_set,
            query_pipeline,
            collision_cache,
            rigid_body_set,
            narrow_phase,
            physics_hooks,
            physics_pipeline,
            islands,
            impulse_joints,
            ccd_solver,
            multibody_joints,
            integration_params,
            ..
        } = &mut *self.ctx;

        // Delete any bodies that don't have alive entities
        let mut to_delete = Vec::new();
        for (handle, body) in rigid_body_set.iter() {
            let entity = RapierUserData::entity(body.user_data);

            if !self.entities.is_alive(entity) {
                // Remove any collisions with the killed entity from the collision cache.
                let mut collisions = collision_cache.collisions.borrow_mut();
                let colliding_with = collisions.swap_remove(&entity);
                if let Some(colliding_with) = colliding_with {
                    for other_entity in colliding_with {
                        if let Some(collisions) = collisions.get_mut(&other_entity) {
                            collisions.retain(|e| e != &entity);
                        }
                    }
                }

                // Delete the rigid body
                to_delete.push(handle);
            }
        }

        for body_handle in to_delete {
            rigid_body_set.remove(
                body_handle,
                islands,
                collider_set,
                impulse_joints,
                multibody_joints,
                true,
            );
        }

        // Step physics pipeline, also steps collision pipeline and updates collision cache.
        integration_params.dt = dt;
        physics_pipeline.step(
            &Vector::new(0.0, -physics_params.gravity),
            integration_params,
            islands,
            broad_phase,
            narrow_phase,
            rigid_body_set,
            collider_set,
            impulse_joints,
            multibody_joints,
            ccd_solver,
            Some(query_pipeline),
            physics_hooks,
            &collision_cache,
        );

        // Force a full rebuild of broadphase
        // PhysicsPipeline step should incrementally update it, but due to an issue with
        // incrementally rebalancing BVH in parry2d, the tree becomes unbalanced and query perf tanks.
        // Rebuilding it every frame is quite fast for our game, and avoids this issue.
        // https://github.com/fishfolk/jumpy/issues/961
        query_pipeline.update(rigid_body_set, collider_set);

        // Iter on each dynamic rigid-bodies that moved.
        for rigid_body_handle in islands.active_dynamic_bodies() {
            let rigid_body = rigid_body_set.get_mut(*rigid_body_handle).unwrap();
            let entity = RapierUserData::entity(rigid_body.user_data);
            if let Some(dynamic_body) = dynamic_bodies.get_mut(entity) {
                if dynamic_body.is_dynamic {
                    let transform = transforms.get_mut(entity).unwrap();
                    let rotation = Quat::from_rotation_z(rigid_body.rotation().angle());
                    // Get translation from physics and preserve Z offset of transform
                    let translation: Vec3 = rigid_body
                        .translation()
                        .xy()
                        .push(transform.translation.z)
                        .into();
                    transform.translation = translation;
                    transform.rotation = rotation;

                    // Apply terminal velocity. Without this kinematic and dynamic objects may
                    // fall at different speeds even if same graviy applied. This is used to mirror
                    // kinematic motion of gravity + terminal vel limit.
                    if let Some(terminal_vel) = physics_params.terminal_velocity {
                        let mut vel = *rigid_body.linvel();
                        if vel.y < -terminal_vel {
                            vel.y = -terminal_vel;
                            rigid_body.set_linvel(vel, true);
                        }
                    }

                    // Update simulation output for tracking if transform is modified outside of rapier
                    // next frame.
                    dynamic_body.update_last_rapier_synced_transform(translation, rotation)
                } else {
                    warn!("Active dynamic bodies contained DynamicBody that is not simulating");
                }
            } else {
                warn!("Active dynamic bodies contained entity that does not have a DynamicBody.");
            }
        }

        // Reset tracking of removed colliders for this frame
        collision_cache.clear_removed_colliders();
    }

    /// Sync the transforms and attributes ( like `disabled` ) of the colliders.
    /// Creates rapier bodies for any object with collision.
    ///
    /// Handle [`DynamicBody`] toggling between simulation and kinematic mode.
    pub fn sync_bodies<'b, Tq>(&mut self, transforms: Tq, dynamic_bodies: &mut CompMut<DynamicBody>)
    where
        Tq: QueryItem,
        Tq::Iter: Iterator<Item = &'b Transform>,
    {
        puffin::profile_function!();

        let RapierContext {
            rigid_body_set,
            collider_set,
            collider_shape_cache,
            ..
        } = &mut *self.ctx;
        for (ent, (transform, collider, dynamic_body)) in self.entities.iter_with((
            transforms,
            &mut self.colliders,
            &mut OptionalMut(dynamic_bodies),
        )) {
            // Get the rapier shape.
            //
            // TODO: Evaluate whether or not caching the colliders like this actually improves
            // performance.
            let shared_shape = collider_shape_cache.shared_shape(collider.shape);

            let is_dynamic = match dynamic_body.as_ref() {
                Some(dynamic_body) => dynamic_body.is_dynamic,
                None => false,
            };

            // Get the handle to the rapier collider, creating it if it doesn't exist.
            let rapier_handle = collider.rapier_handle.get_or_insert_with(|| {
                // Initialize body
                let body_handle = rigid_body_set.insert(if is_dynamic {
                    rapier::RigidBodyBuilder::dynamic().user_data(RapierUserData::from(ent))
                } else if KINEMATIC_MODE == rapier::RigidBodyType::KinematicPositionBased {
                    rapier::RigidBodyBuilder::kinematic_position_based()
                        .user_data(RapierUserData::from(ent))
                } else {
                    rapier::RigidBodyBuilder::kinematic_velocity_based()
                        .user_data(RapierUserData::from(ent))
                });

                collider_set.insert_with_parent(
                    build_actor_rapier_collider(ent, shared_shape.clone()),
                    body_handle,
                    rigid_body_set,
                );
                body_handle
            });
            let rapier_body = rigid_body_set.get_mut(*rapier_handle).unwrap();
            let rapier_collider = collider_set.get_mut(rapier_body.colliders()[0]).unwrap();

            if let Some(dynamic_body) = dynamic_body {
                // Handle changes in is_dynamic
                let was_dynamic = matches!(rapier_body.body_type(), rapier::RigidBodyType::Dynamic);
                if !was_dynamic && is_dynamic {
                    rapier_body.set_body_type(rapier::RigidBodyType::Dynamic, true);

                    // Clear any velocity that may be left from previously simulating body.
                    // If Dynamic is newly initialized or user wants to apply velocity changes before next step,
                    // `DynamicBody::push_simulation_command` may be used which is called after this operation.
                    rapier_body.set_linvel(Vector::zeros(), true);
                    rapier_body.set_angvel(0.0, true);

                    // TODO: We may want to synchronize kinematic body's gravity, mass, and other properties.

                    rapier_collider.set_sensor(false);

                    // Enable contact modification for all bodies to handle stuff like jump through.
                    rapier_collider.set_active_hooks(rapier::ActiveHooks::MODIFY_SOLVER_CONTACTS);
                } else if was_dynamic && !is_dynamic {
                    rapier_collider.set_sensor(true);
                    rapier_collider.set_active_hooks(rapier::ActiveHooks::empty());

                    rapier_body.set_body_type(KINEMATIC_MODE, true);
                }

                // This function still calls for position update if is_dynamic = false
                if dynamic_body.simulation_transform_needs_update(transform) {
                    rapier_body.set_position(
                        rapier::Isometry::new(
                            transform.translation.truncate().to_array().into(),
                            transform.rotation.to_euler(EulerRot::XYZ).2,
                        ),
                        true,
                    )
                }
            } else {
                // update position of kinematics
                //
                // TODO: we may want to use rapier::RigidBody::set_next_kinematic_position
                // so rapier computes velocity of kinematics for better interaction with dynamics,
                // however we don't currently have any kinematic <-> dynamic interaction.
                rapier_body.set_position(
                    rapier::Isometry::new(
                        transform.translation.truncate().to_array().into(),
                        transform.rotation.to_euler(EulerRot::XYZ).2,
                    ),
                    true,
                );
            }
            rapier_collider.set_enabled(!collider.disabled);
        }

        for (solid_ent, solid) in self.entities.iter_with(&mut self.solids) {
            let bones_shape = ColliderShape::Rectangle { size: solid.size };
            let shared_shape = collider_shape_cache.shared_shape(bones_shape);

            // Get or create a collider for the solid
            let handle = solid.rapier_handle.get_or_insert_with(|| {
                let body_handle = rigid_body_set.insert(
                    rapier::RigidBodyBuilder::fixed().user_data(RapierUserData::from(solid_ent)),
                );
                // Membership default, does not filter out collision events.
                let collision_membership = CollisionGroup::DEFAULT;
                let collision_filter = CollisionGroup::ALL;
                // Solids do not filter contact forces with other bodies.
                let simulation_membership = SolverGroup::SOLID_WORLD;
                let simulation_filter = SolverGroup::ALL;
                collider_set.insert_with_parent(
                    rapier::ColliderBuilder::new(shared_shape.clone())
                        .active_events(rapier::ActiveEvents::COLLISION_EVENTS)
                        .active_collision_types(rapier::ActiveCollisionTypes::all())
                        .collision_groups(InteractionGroups::new(
                            collision_membership.bits().into(),
                            collision_filter.bits().into(),
                        ))
                        .solver_groups(InteractionGroups::new(
                            simulation_membership.bits().into(),
                            simulation_filter.bits().into(),
                        ))
                        .user_data(RapierUserData::from(solid_ent)),
                    body_handle,
                    rigid_body_set,
                );
                body_handle
            });
            let solid_body = rigid_body_set.get_mut(*handle).unwrap();

            // Update the solid position
            solid_body.set_translation(rapier::Vector::new(solid.pos.x, solid.pos.y), false);

            let rapier_collider = collider_set.get_mut(solid_body.colliders()[0]).unwrap();
            rapier_collider.set_enabled(!solid.disabled);
            rapier_collider.set_position_wrt_parent(rapier::Isometry::new(default(), 0.0));
            rapier_collider.set_shape(shared_shape.clone());
        }
    }

    /// Apply simulation commands to dynamic bodies.
    ///
    /// # Panics
    ///
    /// This should be called after bodies are initialized, [`DynamicBody`] must have
    /// a [`Collider`] with valid `rapier::RigidBodyHandle` otherwise will panic.
    fn apply_simulation_commands<'b, Dq>(&mut self, dynamic_bodies: Dq)
    where
        Dq: QueryItem,
        Dq::Iter: Iterator<Item = &'b mut DynamicBody>,
    {
        for (ent, dynamic_body) in self.entities.iter_with(dynamic_bodies) {
            // This will consume commands even if body is_dynamic is false.
            let commands = dynamic_body.simulation_commands();
            if dynamic_body.is_dynamic {
                let collider = self.colliders.get(ent).unwrap();
                let rapier_handle = collider.rapier_handle.unwrap();
                let rapier_body = self.ctx.rigid_body_set.get_mut(rapier_handle).unwrap();
                for command in commands {
                    command(rapier_body);
                }
            }
        }
    }

    /// Update all of the map tile collisions.
    ///
    /// You should only need to call this when spawning or otherwise completely rebuilding the map
    /// layout.
    pub fn update_tiles(&mut self) {
        self.update_tiles_with_filter(|_, _| true);
    }

    /// Update the collision for the tile with the given layer index and map grid position.
    pub fn update_tile(&mut self, layer_idx: u32, pos: UVec2) {
        self.update_tiles_with_filter(|idx, p| layer_idx == idx && pos == p);
    }

    /// Update the collisions for map tiles that pass the given filter.
    ///
    /// The filter is a function that takes the layer index and the tile position as an argument.
    pub fn update_tiles_with_filter<F>(&mut self, mut filter: F)
    where
        F: FnMut(u32, UVec2) -> bool,
    {
        let RapierContext {
            rigid_body_set,
            collider_set,
            collider_shape_cache,
            ..
        } = &mut *self.ctx;
        for (_, (layer, meta)) in self
            .entities
            .iter_with((&self.tile_layers, &self.spawned_map_layer_metas))
        {
            let tile_shared_shape = collider_shape_cache
                .shared_shape(ColliderShape::Rectangle {
                    size: layer.tile_size,
                })
                .clone();
            for x in 0..layer.grid_size.x {
                for y in 0..layer.grid_size.y {
                    let pos = uvec2(x, y);
                    if !filter(meta.layer_idx, pos) {
                        continue;
                    };

                    let Some(tile_ent) = layer.get(pos) else {
                        continue;
                    };
                    let collider_x = x as f32 * layer.tile_size.x + layer.tile_size.x / 2.0;
                    let collider_y = y as f32 * layer.tile_size.y + layer.tile_size.y / 2.0;

                    // Get dynamic collider if we have one
                    let dynamic_collider = self.tile_dynamic_colliders.get(tile_ent);

                    // Get or create a collider for the tile
                    let handle = self
                        .tile_rapier_handles
                        .get(tile_ent)
                        .map(|x| **x)
                        .unwrap_or_else(|| {
                            let body_handle = rigid_body_set.insert(
                                rapier::RigidBodyBuilder::fixed()
                                    .user_data(RapierUserData::from(tile_ent)),
                            );

                            // Set SolverGroup based on collision kind so dynamic bodies
                            // know if they should generate contact forces with tile or not.
                            let mut simulation_membership = SolverGroup::NONE;
                            if let Some(collision_kind) = self.tile_collision_kinds.get(tile_ent) {
                                simulation_membership =
                                    collision_kind.simulation_group_membership();
                            }
                            let simulation_filter = SolverGroup::ALL;

                            // Sim group for default tile collider. This is not used for collision
                            // (only used for events) if an additional "dynamic" collider is present
                            // to be used for collision response.
                            let mut default_collider_sim_membership = simulation_membership;
                            if dynamic_collider.is_some() {
                                default_collider_sim_membership = SolverGroup::NONE;
                            }

                            // Insert default collider
                            collider_set.insert_with_parent(
                                rapier::ColliderBuilder::new(tile_shared_shape.clone())
                                    .active_events(rapier::ActiveEvents::COLLISION_EVENTS)
                                    .active_collision_types(rapier::ActiveCollisionTypes::all())
                                    .solver_groups(InteractionGroups::new(
                                        default_collider_sim_membership.bits().into(),
                                        simulation_filter.bits().into(),
                                    ))
                                    .user_data(RapierUserData::from(tile_ent)),
                                body_handle,
                                rigid_body_set,
                            );

                            // Insert dynamic collider if we have one
                            if let Some(dynamic_collider) = dynamic_collider {
                                let shared_shape =
                                    collider_shape_cache.shared_shape(dynamic_collider.shape);
                                collider_set.insert_with_parent(
                                    rapier::ColliderBuilder::new(shared_shape.clone())
                                        // Don't generate events for this collider
                                        .active_events(rapier::ActiveEvents::empty())
                                        // Only needs to collide with dynamics
                                        .active_collision_types(
                                            rapier::ActiveCollisionTypes::DYNAMIC_FIXED,
                                        )
                                        .solver_groups(InteractionGroups::new(
                                            simulation_membership.bits().into(),
                                            simulation_filter.bits().into(),
                                        ))
                                        .position(dynamic_collider.offset.into())
                                        .user_data(RapierUserData::from(tile_ent)),
                                    body_handle,
                                    rigid_body_set,
                                );
                            }
                            self.tile_rapier_handles
                                .insert(tile_ent, TileRapierHandle(body_handle));
                            body_handle
                        });
                    let tile_body = rigid_body_set.get_mut(handle).unwrap();

                    // Update the collider position
                    tile_body.set_translation(rapier::Vector::new(collider_x, collider_y), false);
                }
            }
        }
    }

    /// When spawning or teleporting an entity, this should be called to make sure the entity
    /// doesn't get stuck in semi-solid platforms, and properly falls out of them if it happens to
    /// be colliding with one when spawned.
    //
    // TODO: I believe we can make this method unnecessary by correctly detecting when a body is
    // stuck in a wood platform, with no ground below it.
    pub fn handle_teleport(&mut self, entity: Entity) {
        if self
            .ctx
            .collision_cache
            .get(entity)
            .iter()
            .any(|x| self.tile_collision_kinds.get(*x) == Some(&TileCollisionKind::JumpThrough))
        {
            let collider = self.colliders.get_mut(entity).unwrap();
            collider.descent = true;
            collider.seen_wood = true;
        }
    }

    /// Returns the collisions that one actor has with any other actors.
    pub fn actor_collisions(&self, entity: Entity) -> Vec<Entity> {
        if !self.actors.contains(entity) {
            return default();
        }
        if !self.colliders.contains(entity) {
            return default();
        };

        self.ctx
            .collision_cache
            .get(entity)
            .iter()
            .filter(|x| self.actors.contains(**x))
            .copied()
            .collect()
    }

    /// Returns the collisions that one actor has with any other actors filtered by the given Fn
    pub fn actor_collisions_filtered(
        &self,
        entity: Entity,
        filter: impl Fn(Entity) -> bool,
    ) -> Vec<Entity> {
        if !self.actors.contains(entity) {
            return default();
        }
        if !self.colliders.contains(entity) {
            return default();
        };

        self.ctx
            .collision_cache
            .get(entity)
            .iter()
            .filter(|x| self.actors.contains(**x) && filter(**x))
            .copied()
            .collect()
    }

    /// Put the entity's collider into descent mode so that it will fall through jump-through
    /// platforms.
    pub fn descent(&mut self, entity: Entity) {
        if self.actors.contains(entity) {
            let collider = self.colliders.get_mut(entity).unwrap();
            collider.descent = true;
        }
    }

    /// Attempt to move a body vertically. This will return `true` if an obstacle was run into that
    /// caused the movement to stop short.
    pub fn move_vertical(
        &mut self,
        transforms: &mut CompMut<Transform>,
        entity: Entity,
        mut dy: f32,
    ) -> bool {
        puffin::profile_function!();

        let RapierContext {
            query_pipeline,
            collider_set,
            rigid_body_set,
            collider_shape_cache,
            ..
        } = &mut *self.ctx;
        assert!(self.actors.contains(entity));
        if dy == 0.0 {
            return false;
        }

        // Get the shape and position info for the given entity
        let collider = self.colliders.get_mut(entity).unwrap();
        let transform = *transforms.get(entity).unwrap();
        let mut position = rapier::Isometry::new(
            transform.translation.truncate().into(),
            transform.rotation.to_euler(EulerRot::XYZ).2,
        );
        let shape = collider_shape_cache.shared_shape(collider.shape);

        let mut movement = 0.0;
        let collided = loop {
            // Do a shape cast in the direction of movement
            let velocity = rapier::Vector::new(0.0, dy);
            let collision = query_pipeline.cast_shape(
                rigid_body_set,
                collider_set,
                &position,
                &velocity,
                &**shape,
                1.0,
                true,
                rapier::QueryFilter::new().predicate(&|_handle, rapier_collider| {
                    let ent = RapierUserData::entity(rapier_collider.user_data);

                    if self.solids.contains(ent) {
                        // Include all solid collisions
                        return true;
                    }

                    let Some(tile_kind) = self.tile_collision_kinds.get(ent) else {
                        // Ignore non-tile collisions
                        return false;
                    };

                    // Ignore jump-through tiles if we have already seen wood
                    !(collider.seen_wood && *tile_kind == TileCollisionKind::JumpThrough)
                }),
            );

            if let Some((handle, toi)) = collision {
                let ent = RapierUserData::entity(collider_set.get(handle).unwrap().user_data);

                // Move up to the point of collision
                let diff = dy * toi.toi;
                movement += diff;
                position.translation.y += diff;

                // Subtract from the remaining attempted movement
                dy -= diff;

                if self.solids.contains(ent) {
                    break true;
                }

                let tile_kind = *self.tile_collision_kinds.get(ent).unwrap();

                // collider wants to go down and collided with jumpthrough tile
                if tile_kind == TileCollisionKind::JumpThrough && collider.descent {
                    collider.seen_wood = true;
                }
                // collider wants to go up and encoutered jumpthrough obstace
                if tile_kind == TileCollisionKind::JumpThrough && dy > 0.0 {
                    collider.seen_wood = true;
                    collider.descent = true;
                }

                // If we hit a solid block, or a jumpthrough tile that we aren't falling through
                if !(tile_kind == TileCollisionKind::JumpThrough
                    && (collider.descent || dy > 0.0 || collider.seen_wood))
                {
                    // Indicate we ran into something and stop processing
                    break true;
                }

            // If there is no collision
            } else {
                movement += dy;
                // Indicate we didn't run into anything and stop processing
                break false;
            }
        };

        // Move the entity
        let transform = transforms.get_mut(entity).unwrap();
        transform.translation.y += movement - if collided { 0.1 * dy.signum() } else { 0.0 };

        // Final check, if we are out of woods after the move - reset wood flags
        {
            puffin::profile_scope!("out of woods check");
            let is_in_jump_through = query_pipeline
                .intersection_with_shape(
                    rigid_body_set,
                    collider_set,
                    &(
                        transform.translation.truncate(),
                        transform.rotation.to_euler(EulerRot::XYZ).2,
                    )
                        .into(),
                    &**shape,
                    rapier::QueryFilter::new().predicate(&|_handle, collider| {
                        let ent = RapierUserData::entity(collider.user_data);
                        self.tile_collision_kinds.get(ent) == Some(&TileCollisionKind::JumpThrough)
                    }),
                )
                .is_some();

            if !is_in_jump_through {
                collider.seen_wood = false;
                collider.descent = false;
            }
        }

        collided
    }

    /// Attempt to move a body horizontally. This will return `true` if an obstacle was run into
    /// that caused the movement to stop short.
    pub fn move_horizontal(
        &mut self,
        transforms: &mut CompMut<Transform>,
        entity: Entity,
        mut dx: f32,
    ) -> bool {
        puffin::profile_function!();

        let RapierContext {
            query_pipeline,
            collider_set,
            rigid_body_set,
            collider_shape_cache,
            ..
        } = &mut *self.ctx;
        assert!(self.actors.contains(entity));
        if dx == 0.0 {
            return false;
        }

        // Get the shape and position info for the given entity
        let collider = self.colliders.get_mut(entity).unwrap();
        let transform = *transforms.get(entity).unwrap();
        let mut position = (
            transform.translation.truncate(),
            transform.rotation.to_euler(EulerRot::XYZ).2,
        )
            .into();
        let shape = collider_shape_cache.shared_shape(collider.shape);

        let mut movement = 0.0;
        let collided = 'collision: loop {
            // Do a shape cast in the direction of movement
            let velocity = rapier::Vector::new(dx, 0.0);
            let collision = {
                puffin::profile_scope!("cast shape");
                query_pipeline.cast_shape(
                    rigid_body_set,
                    collider_set,
                    &position,
                    &velocity,
                    &**shape,
                    1.0,
                    true,
                    rapier::QueryFilter::new().predicate(&|_handle, rapier_collider| {
                        let ent = RapierUserData::entity(rapier_collider.user_data);

                        if self.solids.contains(ent) {
                            // Include all solid collisions
                            return true;
                        }

                        let Some(tile_kind) = self.tile_collision_kinds.get(ent) else {
                            // Ignore non-tile collisions
                            return false;
                        };

                        // Ignore jump-through tiles if we have already seen wood.
                        !(collider.seen_wood && *tile_kind == TileCollisionKind::JumpThrough)
                    }),
                )
            };

            if let Some((handle, toi)) = collision {
                let ent = RapierUserData::entity(collider_set.get(handle).unwrap().user_data);

                // Move up to the point of collision
                let diff = dx * toi.toi;
                movement += diff;
                position.translation.x += diff;

                // Subtract from the remaining attempted movement
                dx -= diff;

                if self.solids.contains(ent) {
                    break true;
                }

                let tile_kind = *self.tile_collision_kinds.get(ent).unwrap();

                // If we ran into a jump-through tile, go through it and continue casting
                if tile_kind == TileCollisionKind::JumpThrough {
                    collider.seen_wood = true;
                    collider.descent = true;

                // If we ran into any other kind of tile
                } else {
                    // Indicate we ran into something and stop processing
                    break 'collision true;
                }

            // If there is no collision
            } else {
                movement += dx;
                // Indicate we didn't run into anything and stop processing
                break 'collision false;
            }
        };

        // Move the entity
        let transform = transforms.get_mut(entity).unwrap();
        transform.translation.x += movement - if collided { 0.1 * dx.signum() } else { 0.0 };

        // Final check, if we are out of woods after the move - reset wood flags
        {
            puffin::profile_scope!("out of woods check");
            let is_in_jump_through = query_pipeline
                .intersection_with_shape(
                    rigid_body_set,
                    collider_set,
                    &(
                        transform.translation.truncate(),
                        transform.rotation.to_euler(EulerRot::XYZ).2,
                    )
                        .into(),
                    &**shape,
                    rapier::QueryFilter::new().predicate(&|_handle, collider| {
                        let ent = RapierUserData::entity(collider.user_data);
                        self.tile_collision_kinds.get(ent) == Some(&TileCollisionKind::JumpThrough)
                    }),
                )
                .is_some();

            if !is_in_jump_through {
                collider.seen_wood = false;
                collider.descent = false;
            }
        }

        collided
    }

    /// Returns whether or not there is a tile or solid at the given position.
    ///
    /// > ⚠️ **Warning:** There is a slight difference to how `tile_collision_point` and
    /// > [`tile_collision`][Self::tile_collision] reports collisions.
    /// >
    /// > [`tile_collision`][Self::tile_collision] will report a collision if the collider shape is
    /// > perfectly lined up along the edge of a tile, but `tile_collision_point` won't.
    #[allow(unused)]
    pub fn solid_at(&self, pos: Vec2) -> bool {
        self.solid_collision_point(pos)
            || self.tile_collision_point(pos) == TileCollisionKind::Solid
    }

    pub fn solid_collision_point(&self, pos: Vec2) -> bool {
        for (_, (solid, collider)) in self.entities.iter_with((&self.solids, &self.colliders)) {
            let bbox = collider
                .shape
                .bounding_box(Transform::from_translation(solid.pos.extend(0.0)));
            if bbox.contains(pos) {
                return true;
            }
        }

        false
    }

    /// Returns the tile collision at the given point.
    ///
    /// > ⚠️ **Warning:** There is a slight difference to how `tile_collision_point` and
    /// > [`tile_collision`][Self::tile_collision] reports collisions.
    /// >
    /// > [`tile_collision`][Self::tile_collision] will report a collision if the collider shape is
    /// > perfectly lined up along the edge of a tile, but `tile_collision_point` won't.
    #[allow(unused)]
    pub fn tile_collision_point(&self, pos: Vec2) -> TileCollisionKind {
        for (entity, tile_layer) in self.entities.iter_with(&self.tile_layers) {
            let TileLayer { tile_size, .. } = tile_layer;

            let x = (pos.x / tile_size.y).floor() as u32;
            let y = (pos.y / tile_size.x).floor() as u32;
            let tile_entity = tile_layer.get(UVec2::new(x, y));
            if let Some(tile_entity) = tile_entity {
                return self
                    .tile_collision_kinds
                    .get(tile_entity)
                    .copied()
                    .unwrap_or_default();
            }
        }

        TileCollisionKind::Empty
    }

    /// Get the [`TileCollisionKind`] of the first tile detected colliding with the `shape` at the
    /// given `transform`.
    pub fn tile_collision(&self, transform: Transform, shape: ColliderShape) -> TileCollisionKind {
        self.tile_collision_filtered(transform, shape, |_| true)
    }

    pub fn tile_collision_filtered(
        &self,
        transform: Transform,
        shape: ColliderShape,
        filter: impl Fn(Entity) -> bool,
    ) -> TileCollisionKind {
        self.ctx
            .query_pipeline
            .intersection_with_shape(
                &self.ctx.rigid_body_set,
                &self.ctx.collider_set,
                &(
                    transform.translation.truncate(),
                    transform.rotation.to_euler(EulerRot::XYZ).2,
                )
                    .into(),
                &*shape.shared_shape(),
                rapier::QueryFilter::new().predicate(&|_handle, collider| {
                    let ent = RapierUserData::entity(collider.user_data);
                    (self.solids.contains(ent) || self.tile_collision_kinds.contains(ent))
                        && filter(ent)
                }),
            )
            .map(|x| RapierUserData::entity(self.ctx.collider_set.get(x).unwrap().user_data))
            .and_then(|ent| {
                if self.solids.contains(ent) {
                    return Some(TileCollisionKind::Solid);
                }
                self.tile_collision_kinds.get(ent).copied()
            })
            .unwrap_or_default()
    }

    /// Get the collider for the given entity.
    pub fn get_collider(&self, actor: Entity) -> &Collider {
        assert!(self.actors.contains(actor));
        self.colliders.get(actor).unwrap()
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn convert_entity_to_from_user_data() {
        let e1 = Entity::new(102395950, 10394875);
        let bits = RapierUserData::from(e1);
        let e2 = RapierUserData::entity(bits);
        assert_eq!(e1, e2);
    }
}