ponko 0.0.1

A fast 2D game engine for simulating millions of things: ECS on all cores, hot reload, an in-game inspector, AI-agent control, and a C++ API.
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
//! 3D physics (feature `physics3d`): bodies, colliders, heightfields,
//! queries and the character controller.
#![cfg(feature = "physics3d")]

use std::cell::Cell;
use std::rc::Rc;

use ponko::*;

const GRAVITY: Vec3 = Vec3::new(0.0, -9.81, 0.0);

fn position(world: &mut World, e: Entity) -> Vec3 {
    world.get::<Transform3D>(e).unwrap().position
}

/// Runs `setup`, steps physics `ticks` times, then calls `query` (in a sim
/// system, so it sees the physics world) and returns what it returned.
fn query_after<T: 'static>(
    ticks: u64,
    gravity: Vec3,
    setup: impl FnOnce(&mut Ctx) + 'static,
    mut query: impl FnMut(&mut Ctx) -> T + 'static,
) -> T {
    let out = Rc::new(std::cell::RefCell::new(None));
    let slot = out.clone();
    Game::new("t", 1, 1)
        .physics3d(gravity)
        .sim_system(move |ctx: &mut Ctx| {
            if ctx.sim_tick() == ticks {
                *slot.borrow_mut() = Some(query(ctx));
            }
        })
        .run_headless(ticks + 1, setup);
    out.take().expect("ran the query")
}

/// A 40 × 1 × 40 m slab whose top is at y = 0.
fn floor() -> (Transform3D, RigidBody3D, Collider3D) {
    (
        Transform3D::at(vec3(0.0, -0.5, 0.0)),
        RigidBody3D::fixed(),
        Collider3D::cuboid(vec3(40.0, 1.0, 40.0)),
    )
}

#[test]
fn physics3d_steps_at_50_hz_unless_the_game_chose_a_rate() {
    let mut dt = 0.0;
    Game::new("t", 1, 1)
        .physics3d(GRAVITY)
        .run_headless(1, |ctx| dt = ctx.fixed_dt());
    assert!((dt - 0.02).abs() < 1e-6, "{dt}");
    Game::new("t", 1, 1)
        .fixed_hz(120)
        .physics3d(GRAVITY)
        .run_headless(1, |ctx| dt = ctx.fixed_dt());
    assert!((dt - 1.0 / 120.0).abs() < 1e-6, "{dt}");
}

#[test]
fn a_falling_ball_comes_to_rest_on_a_box() {
    let mut ball = None;
    let mut world = Game::new("t", 1, 1).physics3d(GRAVITY).run_headless(150, |ctx| {
        ctx.world.spawn(floor());
        ball = Some(ctx.world.spawn((
            Transform3D::at(vec3(1.0, 4.0, 2.0)),
            RigidBody3D::dynamic(),
            Collider3D::sphere(0.5),
        )));
    });
    let p = position(&mut world, ball.unwrap());
    assert!((p.y - 0.5).abs() < 0.02, "rests at {p}");
    assert!(
        (p.x - 1.0).abs() < 0.01 && (p.z - 2.0).abs() < 0.01,
        "fell straight: {p}"
    );
}

#[test]
fn a_falling_box_turns_and_writes_its_rotation_back() {
    let mut cube = None;
    let world = Game::new("t", 1, 1).physics3d(GRAVITY).run_headless(150, |ctx| {
        ctx.world.spawn(floor());
        // Dropped on a corner: it tips over onto a face.
        let tilted = Quat::from_rotation_z(0.6) * Quat::from_rotation_x(0.5);
        cube = Some(ctx.world.spawn((
            Transform3D::at(vec3(0.0, 3.0, 0.0)).rotated(tilted),
            RigidBody3D::dynamic(),
            Collider3D::cuboid(Vec3::ONE),
        )));
    });
    let t = *world.get::<Transform3D>(cube.unwrap()).unwrap();
    assert!((t.position.y - 0.5).abs() < 0.03, "lies flat: {}", t.position);
    // A face down: one of its axes points straight up or down.
    let up = [Vec3::X, Vec3::Y, Vec3::Z].map(|a| (t.rotation * a).y.abs());
    assert!(up.iter().any(|&u| u > 0.99), "{up:?}");
}

/// Heights that differ along X and along Z, so a flipped or transposed
/// grid shows.
fn bumpy(n: usize) -> Vec<f32> {
    let mut h = Vec::with_capacity(n * n);
    for z in 0..n {
        for x in 0..n {
            let (x, z) = (x as f32, z as f32);
            h.push(0.4 * x + 0.05 * z * z + ((x * 1.3).sin() * (z * 0.7).cos()));
        }
    }
    h
}

#[test]
fn a_heightfield_collider_matches_the_drawn_ground() {
    let n = 9;
    let heights = bumpy(n);
    let size = 16.0;
    let corner = vec3(30.0, -2.0, -50.0);
    let ground = Heightfield::new(&heights, size);
    // Rays straight down, at points inside cells on both sides of each
    // cell's diagonal, compared with the CPU heights the renderer uses.
    let mut game_points = Vec::new();
    for i in 0..40 {
        let x = (i as f32 * 0.37) % size;
        let z = (i as f32 * 0.61 + 0.13) % size;
        game_points.push((x, z));
    }
    let points = game_points.clone();
    Game::new("t", 1, 1)
        .physics3d(Vec3::ZERO)
        .sim_system(move |ctx: &mut Ctx| {
            if ctx.sim_tick() != 1 {
                return;
            }
            let mut found = Vec::new();
            for &(x, z) in &points {
                let from = corner + vec3(x, 100.0, z);
                let hit = ctx
                    .physics3d
                    .cast_ray(from, Vec3::NEG_Y, 200.0, QueryFilter3D::default());
                found.push(hit.map(|h| h.point.y - corner.y));
            }
            let drawn: Vec<Option<f32>> = points.iter().map(|&(x, z)| ground.height_at(x, z)).collect();
            for ((x, z), (hit, want)) in points.iter().zip(found.iter().zip(&drawn)) {
                let (hit, want) = (hit.expect("hit the ground"), want.unwrap());
                assert!((hit - want).abs() < 1e-3, "at ({x}, {z}): collider {hit}, drawn {want}");
            }
        })
        .run_headless(2, |ctx| {
            let shape = ctx.physics3d.heightfield(&heights, size);
            ctx.world
                .spawn((Transform3D::at(corner), RigidBody3D::fixed(), Collider3D::mesh(shape)));
        });
}

#[test]
fn a_ball_rests_on_a_heightfield_and_falls_through_its_holes() {
    let n = 5;
    let mut heights = vec![3.0; n * n];
    // A hole at grid point (x 3, z 3): the four cells around it are open.
    heights[3 * n + 3] = f32::NAN;
    let (mut on, mut over_hole) = (None, None);
    let mut world = Game::new("t", 1, 1).physics3d(GRAVITY).run_headless(120, |ctx| {
        let shape = ctx.physics3d.heightfield(&heights, 8.0);
        ctx.world
            .spawn((Transform3D::default(), RigidBody3D::fixed(), Collider3D::mesh(shape)));
        on = Some(ctx.world.spawn((
            Transform3D::at(vec3(1.0, 6.0, 1.0)),
            RigidBody3D::dynamic(),
            Collider3D::sphere(0.25),
        )));
        // Grid points are 2 m apart: (6, 6) is the hole.
        over_hole = Some(ctx.world.spawn((
            Transform3D::at(vec3(6.0, 6.0, 6.0)),
            RigidBody3D::dynamic(),
            Collider3D::sphere(0.25),
        )));
    });
    let y = position(&mut world, on.unwrap()).y;
    assert!((y - 3.25).abs() < 0.02, "rests at {y}");
    let y = position(&mut world, over_hole.unwrap()).y;
    assert!(y < 0.0, "fell through the hole: {y}");
}

#[test]
#[should_panic(expected = "isn't a square grid")]
fn a_heightfield_needs_a_square_grid() {
    let mut physics = Physics3D::default();
    let _ = physics.heightfield(&[0.0; 6], 1.0);
}

#[test]
fn moving_the_transform_teleports_the_body() {
    let mut world = Game::new("t", 1, 1)
        .physics3d(Vec3::ZERO)
        .sim_system(|ctx: &mut Ctx| {
            if ctx.sim_tick() == 5 {
                ctx.world
                    .query::<&mut Transform3D>()
                    .with::<RigidBody3D>()
                    .for_each(|t| t.position = vec3(50.0, 7.0, -3.0));
            }
        })
        .run_headless(10, |ctx| {
            ctx.world
                .spawn((Transform3D::default(), RigidBody3D::dynamic(), Collider3D::sphere(1.0)));
        });
    let p = world.query::<&Transform3D>().iter().next().unwrap().position;
    assert_eq!(p, vec3(50.0, 7.0, -3.0));
}

#[test]
fn velocity_sets_and_reports_speed() {
    let mut world = Game::new("t", 1, 1).physics3d(Vec3::ZERO).run_headless(50, |ctx| {
        ctx.world.spawn((
            Transform3D::default(),
            RigidBody3D::dynamic(),
            Collider3D::sphere(0.5),
            Velocity3D::linear(vec3(0.0, 0.0, -3.0)),
        ));
    });
    let (t, v) = world
        .query::<(&Transform3D, &Velocity3D)>()
        .iter()
        .map(|(t, v)| (*t, *v))
        .next()
        .unwrap();
    assert!((t.position.z + 3.0).abs() < 0.01, "one second at 3 m/s: {}", t.position);
    assert!((v.linear.z + 3.0).abs() < 1e-3);
}

#[test]
fn bodies_leave_the_world_when_despawned() {
    let count = Rc::new(Cell::new(usize::MAX));
    let seen = count.clone();
    Game::new("t", 1, 1)
        .physics3d(GRAVITY)
        .sim_system(move |ctx: &mut Ctx| {
            if ctx.sim_tick() == 2 {
                let all: Vec<Entity> = ctx.world.query::<Entity>().with::<RigidBody3D>().iter().collect();
                for e in all {
                    ctx.world.despawn(e);
                }
            }
            seen.set(ctx.physics3d.body_count());
        })
        .run_headless(5, |ctx| {
            ctx.world.spawn(floor());
            ctx.world.spawn((
                Transform3D::at(Vec3::Y),
                RigidBody3D::dynamic(),
                Collider3D::sphere(0.5),
            ));
        });
    assert_eq!(count.get(), 0);
}

#[test]
fn collisions_are_events() {
    let started = Rc::new(Cell::new(0));
    let seen = started.clone();
    Game::new("t", 1, 1)
        .physics3d(GRAVITY)
        .sim_system(move |ctx: &mut Ctx| seen.set(seen.get() + ctx.events.read::<CollisionStarted>().len()))
        .run_headless(100, |ctx| {
            ctx.world.spawn(floor());
            ctx.world.spawn((
                Transform3D::at(vec3(0.0, 2.0, 0.0)),
                RigidBody3D::dynamic(),
                Collider3D::sphere(0.5),
            ));
        });
    assert_eq!(started.get(), 1);
}

#[test]
fn collision_groups_let_bodies_pass_through_each_other() {
    const GROUND: u32 = 1 << 0;
    const GHOST: u32 = 1 << 1;
    let (mut ghost, mut ball) = (None, None);
    let mut world = Game::new("t", 1, 1).physics3d(GRAVITY).run_headless(60, |ctx| {
        let (t, r, c) = floor();
        ctx.world
            .spawn((t, r, c.groups(CollisionGroups::new(GROUND, u32::MAX))));
        ghost = Some(ctx.world.spawn((
            Transform3D::at(vec3(0.0, 2.0, 0.0)),
            RigidBody3D::dynamic(),
            Collider3D::sphere(0.5).groups(CollisionGroups::new(GHOST, !GROUND)),
        )));
        ball = Some(ctx.world.spawn((
            Transform3D::at(vec3(3.0, 2.0, 0.0)),
            RigidBody3D::dynamic(),
            Collider3D::sphere(0.5),
        )));
    });
    let y = position(&mut world, ghost.unwrap()).y;
    assert!(y < -1.0, "the ghost doesn't touch the ground: {y}");
    let y = position(&mut world, ball.unwrap()).y;
    assert!((y - 0.5).abs() < 0.02, "the ball does: {y}");
}

#[test]
fn restitution_bounces_and_friction_slows() {
    // Each surface is averaged with what it touches.
    let peak = Rc::new(Cell::new(0.0f32));
    let seen = peak.clone();
    let (mut ice, mut rubber) = (None, None);
    let mut world = Game::new("t", 1, 1)
        .physics3d(GRAVITY)
        .sim_system(move |ctx: &mut Ctx| {
            if ctx.sim_tick() > 50 {
                ctx.world
                    .query::<(&Transform3D, &Collider3D)>()
                    .iter()
                    .filter(|(_, c)| c.restitution > 0.5)
                    .for_each(|(t, _)| seen.set(seen.get().max(t.position.y)));
            }
        })
        .run_headless(100, |ctx| {
            let (t, r, c) = floor();
            ctx.world.spawn((t, r, c.restitution(0.9)));
            // A bouncy ball dropped from 3 m.
            ctx.world.spawn((
                Transform3D::at(vec3(-3.0, 3.0, 5.0)),
                RigidBody3D::dynamic(),
                Collider3D::sphere(0.5).restitution(0.9),
            ));
            // Two crates sliding at 4 m/s.
            for (z, friction, slot) in [(0.0, 0.0, &mut ice), (-3.0, 1.0, &mut rubber)] {
                *slot = Some(ctx.world.spawn((
                    Transform3D::at(vec3(0.0, 0.5, z)),
                    RigidBody3D::dynamic(),
                    Collider3D::cuboid(Vec3::ONE).friction(friction),
                    Velocity3D::linear(vec3(4.0, 0.0, 0.0)),
                )));
            }
        });
    assert!(peak.get() > 1.5, "bounced back up to {}", peak.get());
    let (ice, rubber) = (
        position(&mut world, ice.unwrap()).x,
        position(&mut world, rubber.unwrap()).x,
    );
    assert!(ice > rubber + 1.0, "ice slides further: {ice} vs {rubber}");
}

#[test]
fn rays_hit_the_closest_and_all_hits_come_sorted() {
    let sensor = Rc::new(Cell::new(None));
    let sensor_set = sensor.clone();
    let (closest, hits, not_first, with_sensor) = query_after(
        1,
        Vec3::ZERO,
        move |ctx| {
            // Walls at x = 9, 3 and 6 (spawned out of order), 1 m thick.
            for x in [9.0, 3.0, 6.0] {
                ctx.world.spawn((
                    Transform3D::at(vec3(x, 0.0, 0.0)),
                    RigidBody3D::fixed(),
                    Collider3D::cuboid(vec3(1.0, 4.0, 4.0)),
                ));
            }
            sensor_set.set(Some(ctx.world.spawn((
                Transform3D::at(vec3(1.5, 0.0, 0.0)),
                RigidBody3D::fixed(),
                Collider3D::cuboid(vec3(1.0, 4.0, 4.0)).sensor(),
            ))));
        },
        |ctx| {
            let f = QueryFilter3D::default();
            let closest = ctx.physics3d.cast_ray(Vec3::ZERO, Vec3::X, 100.0, f);
            let mut hits = Vec::new();
            ctx.physics3d.cast_ray_all(Vec3::ZERO, Vec3::X, 100.0, f, &mut hits);
            let first = closest.unwrap().entity;
            let not_first = ctx.physics3d.cast_ray(Vec3::ZERO, Vec3::X, 100.0, f.exclude(first));
            let mut with_sensor = Vec::new();
            ctx.physics3d
                .cast_ray_all(Vec3::ZERO, Vec3::X, 100.0, f.with_sensors(), &mut with_sensor);
            (closest, hits, not_first, with_sensor)
        },
    );
    let closest = closest.expect("hit a wall");
    assert!((closest.distance - 2.5).abs() < 1e-4, "{closest:?}");
    assert!((closest.normal - Vec3::NEG_X).length() < 1e-4, "{closest:?}");
    assert!((closest.point - vec3(2.5, 0.0, 0.0)).length() < 1e-4);
    let distances: Vec<f32> = hits.iter().map(|h| h.distance).collect();
    assert_eq!(distances.len(), 3, "sensors are left out by default: {distances:?}");
    for (d, want) in distances.iter().zip([2.5, 5.5, 8.5]) {
        assert!((d - want).abs() < 1e-4, "{distances:?}");
    }
    assert!((not_first.unwrap().distance - 5.5).abs() < 1e-4);
    assert_eq!(with_sensor.len(), 4);
    assert_eq!(Some(with_sensor[0].entity), sensor.get());
}

#[test]
fn shape_casts_stop_where_the_shape_first_touches() {
    let (sphere, capsule, missed, grouped) = query_after(
        1,
        Vec3::ZERO,
        |ctx| {
            ctx.world.spawn((
                Transform3D::at(vec3(5.0, 0.0, 0.0)),
                RigidBody3D::fixed(),
                Collider3D::cuboid(vec3(1.0, 4.0, 4.0)).groups(CollisionGroups::new(1, u32::MAX)),
            ));
        },
        |ctx| {
            let f = QueryFilter3D::default();
            let p = &ctx.physics3d;
            (
                p.cast_sphere(Vec3::ZERO, 0.5, Vec3::X, 100.0, f),
                p.cast_capsule(Vec3::ZERO, 1.0, 0.25, Vec3::X, 100.0, f),
                // Too short to reach.
                p.cast_sphere(Vec3::ZERO, 0.5, Vec3::X, 3.0, f),
                // A query that doesn't touch the wall's group.
                p.cast_sphere(
                    Vec3::ZERO,
                    0.5,
                    Vec3::X,
                    100.0,
                    f.groups(CollisionGroups::new(u32::MAX, 1 << 3)),
                ),
            )
        },
    );
    let s = sphere.expect("the sphere hits the wall");
    assert!((s.distance - 4.0).abs() < 1e-3, "{s:?}");
    assert!(
        (s.point.x - 4.5).abs() < 1e-3 && (s.normal - Vec3::NEG_X).length() < 1e-3,
        "{s:?}"
    );
    let c = capsule.expect("the capsule hits the wall");
    assert!((c.distance - 4.25).abs() < 1e-3, "{c:?}");
    assert!(missed.is_none());
    assert!(grouped.is_none());
}

#[test]
fn overlap_tests_find_what_is_inside() {
    let near = Rc::new(Cell::new(None));
    let near_set = near.clone();
    let (found, boxed, capsuled) = query_after(
        1,
        Vec3::ZERO,
        move |ctx| {
            near_set.set(Some(ctx.world.spawn((
                Transform3D::at(vec3(1.5, 0.0, 0.0)),
                RigidBody3D::fixed(),
                Collider3D::sphere(1.0),
            ))));
            ctx.world.spawn((
                Transform3D::at(vec3(10.0, 0.0, 0.0)),
                RigidBody3D::fixed(),
                Collider3D::sphere(0.5),
            ));
            ctx.world.spawn((
                Transform3D::at(vec3(0.0, 2.2, 10.0)),
                RigidBody3D::fixed(),
                Collider3D::sphere(0.5),
            ));
            ctx.world.spawn((
                Transform3D::at(vec3(0.0, 40.0, 0.0)),
                RigidBody3D::fixed(),
                Collider3D::sphere(1.0),
            ));
        },
        |ctx| {
            let f = QueryFilter3D::default();
            let (mut found, mut boxed, mut capsuled) = (Vec::new(), Vec::new(), Vec::new());
            ctx.physics3d.overlap_sphere(Vec3::ZERO, 2.0, f, &mut found);
            ctx.physics3d
                .overlap_box(vec3(10.0, 0.0, 0.0), Vec3::splat(2.0), Quat::IDENTITY, f, &mut boxed);
            ctx.physics3d
                .overlap_capsule(vec3(0.0, 0.0, 10.0), 4.0, 0.5, f, &mut capsuled);
            (found, boxed, capsuled)
        },
    );
    assert_eq!(found, vec![near.get().unwrap()]);
    assert_eq!(boxed.len(), 1);
    assert_eq!(capsuled.len(), 1);
}

#[test]
fn convex_hulls_and_trimeshes_collide() {
    let mut rock = None;
    let mut world = Game::new("t", 1, 1).physics3d(GRAVITY).run_headless(150, |ctx| {
        // A floor made of two triangles, and a pyramid dropped on it.
        let floor = ctx
            .physics3d
            .trimesh(
                &[
                    vec3(-10.0, 0.0, -10.0),
                    vec3(10.0, 0.0, -10.0),
                    vec3(-10.0, 0.0, 10.0),
                    vec3(10.0, 0.0, 10.0),
                ],
                &[[0, 2, 1], [1, 2, 3]],
            )
            .unwrap();
        ctx.world
            .spawn((Transform3D::default(), RigidBody3D::fixed(), Collider3D::mesh(floor)));
        let pyramid = ctx
            .physics3d
            .convex_hull(&[
                vec3(-0.5, 0.0, -0.5),
                vec3(0.5, 0.0, -0.5),
                vec3(-0.5, 0.0, 0.5),
                vec3(0.5, 0.0, 0.5),
                vec3(0.0, 1.0, 0.0),
            ])
            .unwrap();
        rock = Some(ctx.world.spawn((
            Transform3D::at(vec3(0.0, 3.0, 0.0)),
            RigidBody3D::dynamic(),
            Collider3D::mesh(pyramid),
        )));
    });
    let y = position(&mut world, rock.unwrap()).y;
    assert!(y.abs() < 0.02, "the pyramid stands on its base: {y}");
}

#[test]
fn freed_meshes_are_gone_and_their_handles_stay_dead() {
    let mut physics = Physics3D::default();
    let a = physics.heightfield(&[0.0; 4], 1.0);
    assert!(physics.is_mesh_live(a));
    physics.free_mesh(a);
    assert!(!physics.is_mesh_live(a));
    let b = physics.heightfield(&[0.0; 4], 1.0);
    assert_ne!(a, b, "the slot is reused under a new generation");
    physics.free_mesh(a);
    assert!(physics.is_mesh_live(b), "freeing a stale handle does nothing");
}

#[test]
fn impulses_push_dynamic_bodies() {
    let mut ball = None;
    let mut world = Game::new("t", 1, 1)
        .physics3d(Vec3::ZERO)
        .sim_system(|ctx: &mut Ctx| {
            if ctx.sim_tick() == 1 {
                let e = ctx.world.query::<Entity>().with::<RigidBody3D>().iter().next().unwrap();
                let mass = ctx.physics3d.mass(e).unwrap();
                assert!(ctx.physics3d.apply_impulse(e, vec3(2.0 * mass, 0.0, 0.0)));
            }
        })
        .run_headless(51, |ctx| {
            ball = Some(
                ctx.world
                    .spawn((Transform3D::default(), RigidBody3D::dynamic(), Collider3D::sphere(0.5))),
            );
        });
    let x = position(&mut world, ball.unwrap()).x;
    assert!((x - 2.0).abs() < 0.05, "2 m/s for a second: {x}");
}

// --- the character controller ---

/// Flat ground for x < 0, then a slope rising at `degrees` along +X.
fn ramp(physics: &mut Physics3D, degrees: f32) -> CollisionMesh {
    let top = 20.0 * degrees.to_radians().tan();
    physics
        .trimesh(
            &[
                vec3(-20.0, 0.0, -10.0),
                vec3(0.0, 0.0, -10.0),
                vec3(-20.0, 0.0, 10.0),
                vec3(0.0, 0.0, 10.0),
                vec3(20.0, top, -10.0),
                vec3(20.0, top, 10.0),
            ],
            &[[0, 2, 1], [1, 2, 3], [1, 3, 4], [4, 3, 5]],
        )
        .unwrap()
}

/// A character at `start` that walks along +X at `speed` for `ticks`.
fn walk(start: Vec3, speed: f32, ticks: u64, level: impl FnOnce(&mut Ctx) + 'static) -> (Vec3, Character3D) {
    let mut player = None;
    let mut world = Game::new("t", 1, 1)
        .sim_system(move |ctx: &mut Ctx| {
            ctx.world.query::<&mut Character3D>().for_each(|c| c.velocity.x = speed);
            // Facing where it walks: turning isn't moving.
            ctx.world
                .query::<&mut Transform3D>()
                .with::<Character3D>()
                .for_each(|t| t.rotation = Quat::from_rotation_y(0.1) * t.rotation);
        })
        .physics3d(GRAVITY)
        .run_headless(ticks, |ctx| {
            level(ctx);
            player = Some(ctx.world.spawn((Transform3D::at(start), Character3D::new(1.8, 0.3))));
        });
    let e = player.unwrap();
    (position(&mut world, e), *world.get::<Character3D>(e).unwrap())
}

#[test]
fn a_character_stands_on_the_ground() {
    let (p, c) = walk(vec3(0.0, 1.0, 0.0), 0.0, 100, |ctx| {
        ctx.world.spawn(floor());
    });
    assert!(c.grounded);
    assert!(p.y.abs() < 0.03, "feet on the floor: {p}");
    assert!(c.velocity.y.abs() < 1e-3, "not falling any more: {:?}", c.velocity);
}

#[test]
fn a_character_walks_up_a_30_degree_slope() {
    let (p, c) = walk(vec3(-2.0, 0.05, 0.0), 2.0, 200, |ctx| {
        let m = ramp(&mut ctx.physics3d, 30.0);
        ctx.world
            .spawn((Transform3D::default(), RigidBody3D::fixed(), Collider3D::mesh(m)));
    });
    assert!(p.x > 3.0, "walked up the slope: {p}");
    let on_slope = p.x * 30f32.to_radians().tan();
    assert!(
        (p.y - on_slope).abs() < 0.1,
        "feet on the slope: {p}, slope at {on_slope}"
    );
    assert!(c.grounded);
}

#[test]
fn a_character_cannot_walk_up_a_60_degree_slope() {
    let (p, _) = walk(vec3(-2.0, 0.05, 0.0), 2.0, 200, |ctx| {
        let m = ramp(&mut ctx.physics3d, 60.0);
        ctx.world
            .spawn((Transform3D::default(), RigidBody3D::fixed(), Collider3D::mesh(m)));
    });
    assert!(p.x < 0.5, "stopped at the foot of the slope: {p}");
    assert!(p.y < 0.6, "didn't climb: {p}");
}

#[test]
fn a_character_steps_up_a_30_cm_step() {
    let (p, c) = walk(vec3(-2.0, 0.05, 0.0), 2.0, 150, |ctx| {
        ctx.world.spawn(floor());
        // A 0.3 m step from x = 0 on.
        ctx.world.spawn((
            Transform3D::at(vec3(5.0, 0.15, 0.0)),
            RigidBody3D::fixed(),
            Collider3D::cuboid(vec3(10.0, 0.3, 10.0)),
        ));
    });
    assert!(p.x > 1.5, "went on: {p}");
    assert!((p.y - 0.3).abs() < 0.05, "stands on the step: {p}");
    assert!(c.grounded);
}

#[test]
fn a_character_does_not_climb_a_wall() {
    let (p, _) = walk(vec3(-2.0, 0.05, 0.0), 2.0, 150, |ctx| {
        ctx.world.spawn(floor());
        ctx.world.spawn((
            Transform3D::at(vec3(5.0, 0.5, 0.0)),
            RigidBody3D::fixed(),
            Collider3D::cuboid(vec3(10.0, 1.0, 10.0)),
        ));
    });
    assert!(p.x < 0.0 && p.y < 0.1, "blocked by a 1 m wall: {p}");
}

#[test]
fn a_character_stays_grounded_walking_downhill() {
    let airborne = Rc::new(Cell::new(0));
    let seen = airborne.clone();
    let mut player = None;
    let mut world = Game::new("t", 1, 1)
        .sim_system(|ctx: &mut Ctx| {
            // Down the slope: towards -X.
            ctx.world.query::<&mut Character3D>().for_each(|c| c.velocity.x = -4.0);
        })
        .physics3d(GRAVITY)
        .sim_system(move |ctx: &mut Ctx| {
            if ctx.sim_tick() > 10 {
                let c = *ctx.world.query::<&Character3D>().iter().next().unwrap();
                if !c.grounded {
                    seen.set(seen.get() + 1);
                }
            }
        })
        .run_headless(220, |ctx| {
            let m = ramp(&mut ctx.physics3d, 25.0);
            ctx.world
                .spawn((Transform3D::default(), RigidBody3D::fixed(), Collider3D::mesh(m)));
            let top = 15.0 * 25f32.to_radians().tan();
            player = Some(
                ctx.world
                    .spawn((Transform3D::at(vec3(15.0, top + 0.05, 0.0)), Character3D::new(1.8, 0.3))),
            );
        });
    let p = position(&mut world, player.unwrap());
    assert!(p.x < 0.0, "reached the bottom: {p}");
    assert_eq!(airborne.get(), 0, "never left the ground");
}

#[test]
fn a_character_jumps_and_lands() {
    let peak = Rc::new(Cell::new(0.0f32));
    let seen = peak.clone();
    let (p, c) = {
        let mut player = None;
        let mut world = Game::new("t", 1, 1)
            .sim_system(|ctx: &mut Ctx| {
                if ctx.sim_tick() == 30 {
                    ctx.world.query::<&mut Character3D>().for_each(|c| {
                        assert!(c.grounded);
                        c.velocity.y = 5.0;
                    });
                }
            })
            .physics3d(GRAVITY)
            .sim_system(move |ctx: &mut Ctx| {
                let y = ctx
                    .world
                    .query::<&Transform3D>()
                    .with::<Character3D>()
                    .iter()
                    .next()
                    .unwrap()
                    .position
                    .y;
                seen.set(seen.get().max(y));
            })
            .run_headless(150, |ctx| {
                ctx.world.spawn(floor());
                player = Some(
                    ctx.world
                        .spawn((Transform3D::at(vec3(0.0, 0.05, 0.0)), Character3D::new(1.8, 0.3))),
                );
            });
        let e = player.unwrap();
        (position(&mut world, e), *world.get::<Character3D>(e).unwrap())
    };
    // v² / 2g = 25 / 19.62 ≈ 1.27 m (a little less, stepped 50 times a second).
    assert!((peak.get() - 1.23).abs() < 0.06, "peak {}", peak.get());
    assert!(c.grounded && p.y.abs() < 0.03, "landed: {p}");
}

#[test]
fn a_character_pushes_dynamic_bodies() {
    let mut crate_box = None;
    let mut world = Game::new("t", 1, 1)
        .sim_system(|ctx: &mut Ctx| {
            ctx.world.query::<&mut Character3D>().for_each(|c| c.velocity.x = 2.0);
        })
        .physics3d(GRAVITY)
        .run_headless(150, |ctx| {
            ctx.world.spawn(floor());
            ctx.world
                .spawn((Transform3D::at(vec3(-1.0, 0.05, 0.0)), Character3D::new(1.8, 0.3)));
            // A 50 kg crate in the way.
            crate_box = Some(ctx.world.spawn((
                Transform3D::at(vec3(1.0, 0.25, 0.0)),
                RigidBody3D::dynamic(),
                Collider3D::cuboid(Vec3::splat(0.5)).density(400.0),
            )));
        });
    let x = position(&mut world, crate_box.unwrap()).x;
    assert!(x > 2.0, "pushed along: {x}");
}

#[test]
fn a_character_sets_off_triggers() {
    let entered = Rc::new(Cell::new(0));
    let seen = entered.clone();
    Game::new("t", 1, 1)
        .sim_system(|ctx: &mut Ctx| {
            ctx.world.query::<&mut Character3D>().for_each(|c| c.velocity.x = 3.0);
        })
        .physics3d(GRAVITY)
        .sim_system(move |ctx: &mut Ctx| {
            let n = ctx
                .events
                .read::<CollisionStarted>()
                .iter()
                .filter(|c| c.sensor)
                .count();
            seen.set(seen.get() + n);
        })
        .run_headless(100, |ctx| {
            ctx.world.spawn(floor());
            ctx.world
                .spawn((Transform3D::at(vec3(-2.0, 0.05, 0.0)), Character3D::new(1.8, 0.3)));
            ctx.world.spawn((
                Transform3D::at(vec3(3.0, 1.0, 0.0)),
                RigidBody3D::fixed(),
                Collider3D::cuboid(vec3(1.0, 2.0, 4.0)).sensor(),
            ));
        });
    assert_eq!(entered.get(), 1);
}

#[test]
fn turning_a_character_is_not_a_teleport() {
    let (p, _) = walk(vec3(0.0, 0.05, 0.0), 1.0, 50, |ctx| {
        ctx.world.spawn(floor());
    });
    // It turned every tick and still walked: one second at 1 m/s.
    assert!((p.x - 1.0).abs() < 0.05, "{p}");
}

/// A scene file under `assets/` that is deleted when the test ends.
struct TempScene(&'static str);
impl Drop for TempScene {
    fn drop(&mut self) {
        let _ = std::fs::remove_file(
            std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
                .join("assets")
                .join(self.0),
        );
    }
}

#[test]
fn scenes_make_mesh_colliders_from_mesh_files_and_save_them_by_file() {
    let file = TempScene("test-mesh-collider.json");
    let saved = TempScene("test-mesh-collider-saved.json");
    let collider = |convex: bool, x: f32| {
        serde_json::json!({
            "Transform3D": { "position": [x, 0.0, 0.0], "rotation": [0.0, 0.0, 0.0, 1.0], "scale": [1.0, 1.0, 1.0] },
            "RigidBody3D": { "kind": "Fixed" },
            "Collider3D": { "shape": { "Mesh": { "file": "models/house.glb", "scale": [1.0, 3.0, 1.0], "convex": convex } } },
        })
    };
    let scene = serde_json::json!({
        "format": "crabby-world",
        "version": 1,
        "entities": [
            { "entity": 0, "components": collider(false, 0.0) },
            { "entity": 1, "components": collider(true, 10.0) },
            { "entity": 2, "components": collider(false, 20.0) },
        ],
        "resources": {},
    });
    let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("assets")
        .join(file.0);
    std::fs::write(&path, serde_json::to_string(&scene).unwrap()).unwrap();
    let (tops, meshes) = query_after(
        1,
        Vec3::ZERO,
        |ctx| {
            ctx.load_scene(file.0).unwrap();
            ctx.save_scene(saved.0).unwrap();
        },
        |ctx| {
            let top = |x: f32| {
                ctx.physics3d
                    .cast_ray(vec3(x, 50.0, 0.0), -Vec3::Y, 100.0, QueryFilter3D::default())
                    .map(|hit| hit.point.y)
            };
            let tops = [top(0.0), top(10.0), top(20.0), top(30.0)];
            let meshes: std::collections::HashSet<_> = ctx
                .world
                .query::<&Collider3D>()
                .iter()
                .map(|c| format!("{:?}", c.shape))
                .collect();
            (tops, meshes)
        },
    );
    // The house's walls are 1.6 m and its roof 2.7 m high, made 3 times taller.
    for top in &tops[..3] {
        let y = top.expect("the ray hits the house");
        assert!((4.7..=8.2).contains(&y), "{tops:?}");
    }
    assert_eq!(tops[3], None, "nothing beside the houses");
    assert_eq!(
        meshes.len(),
        2,
        "the triangle mesh is made once and shared, the hull is another: {meshes:?}"
    );
    let text = std::fs::read_to_string(
        std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
            .join("assets")
            .join(saved.0),
    )
    .unwrap();
    let json: serde_json::Value = serde_json::from_str(&text).unwrap();
    let shapes: Vec<_> = json["entities"]
        .as_array()
        .unwrap()
        .iter()
        .map(|e| e["components"]["Collider3D"]["shape"].clone())
        .collect();
    for (shape, entity) in shapes.iter().zip(scene["entities"].as_array().unwrap()) {
        assert_eq!(shape, &entity["components"]["Collider3D"]["shape"], "saved by its file");
    }
}

/// A crowd big enough to plan its moves in parallel lands in exactly the same
/// places on every run.
#[test]
fn a_crowd_of_characters_moves_the_same_way_every_run() {
    let run = || {
        query_after(
            60,
            GRAVITY,
            |ctx| {
                ctx.world.spawn(floor());
                for i in 0..40 {
                    let (x, z) = ((i % 8) as f32 * 0.7 - 2.5, (i / 8) as f32 * 0.7 - 1.5);
                    let mut c = Character3D::new(1.8, 0.3);
                    // Everyone walks toward the middle, so they bump into each other.
                    c.velocity = vec3(-x, 0.0, -z);
                    ctx.world.spawn((Transform3D::at(vec3(x, 0.05, z)), c));
                }
            },
            |ctx| {
                let mut out: Vec<[u32; 3]> = Vec::new();
                ctx.world.query::<(&Transform3D, &Character3D)>().for_each(|(t, _)| {
                    out.push(t.position.to_array().map(f32::to_bits));
                });
                out
            },
        )
    };
    let first = run();
    assert_eq!(first.len(), 40);
    assert_eq!(first, run());
    assert_eq!(first, run());
}