euv-engine 0.28.39

A high-performance 2D game engine built on the euv framework, featuring ECS, fixed-timestep game loop, canvas rendering, physics, collision detection, sprite animation, and audio.
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
use super::*;

const EPSILON: f64 = 1e-9;

#[test]
fn step_applies_torque_to_3d_angular_velocity() {
    let mut world: PhysicsWorld3D = PhysicsWorld3D::default();
    let mut body: RigidBody3D = RigidBody3D::new_dynamic(1, Vector3D::new(0.0, 0.0, 0.0));
    body.apply_torque(Vector3D::new(0.0, 0.0, 2.0));
    world.add_body(body);
    world.step(1.0);
    let omega: Vector3D = world.get_body(1).unwrap().get_angular_velocity();
    assert!(
        omega.get_x().abs() < EPSILON,
        "unexpected x angular velocity: {}",
        omega.get_x(),
    );
    assert!(
        omega.get_y().abs() < EPSILON,
        "unexpected y angular velocity: {}",
        omega.get_y(),
    );
    let expected_z: f64 = 2.0;
    assert!(
        (omega.get_z() - expected_z).abs() < EPSILON,
        "expected z angular velocity {}, got {}",
        expected_z,
        omega.get_z(),
    );
    world.step(1.0);
    let omega_after: Vector3D = world.get_body(1).unwrap().get_angular_velocity();
    assert!(
        (omega_after.get_z() - expected_z).abs() < EPSILON,
        "torque_accumulator leaked into a second step: z = {}",
        omega_after.get_z(),
    );
}

#[test]
fn step_static_3d_body_ignores_torque() {
    let mut world: PhysicsWorld3D = PhysicsWorld3D::default();
    let mut body: RigidBody3D = RigidBody3D::new_static(1, Vector3D::new(0.0, 0.0, 0.0));
    body.apply_torque(Vector3D::new(1.0, 0.0, 0.0));
    world.add_body(body);
    world.step(1.0);
    let omega: Vector3D = world.get_body(1).unwrap().get_angular_velocity();
    assert!(
        omega.get_x().abs() < EPSILON
            && omega.get_y().abs() < EPSILON
            && omega.get_z().abs() < EPSILON,
        "static body must remain rotationally inert, got ({}, {}, {})",
        omega.get_x(),
        omega.get_y(),
        omega.get_z(),
    );
}

#[test]
fn step_torque_accumulates_over_multiple_steps() {
    let mut world: PhysicsWorld3D = PhysicsWorld3D::default();
    let body: RigidBody3D = RigidBody3D::new_dynamic(1, Vector3D::new(0.0, 0.0, 0.0));
    world.add_body(body);
    for _ in 0..4 {
        world
            .get_body_mut(1)
            .unwrap()
            .apply_torque(Vector3D::new(0.0, 1.0, 0.0));
        world.step(1.0);
    }
    let omega: Vector3D = world.get_body(1).unwrap().get_angular_velocity();
    assert!(
        (omega.get_y() - 4.0).abs() < EPSILON,
        "expected cumulative angular velocity 4.0 on y axis, got {}",
        omega.get_y(),
    );
}
#[test]
fn step_2d_angular_velocity_unchanged() {
    let mut world: PhysicsWorld2D = PhysicsWorld2D::default();
    let body: RigidBody2D = RigidBody2D::new_dynamic(1, Vector2D::new(0.0, 0.0));
    world.add_body(body);
    world.step(1.0);
    let omega: f64 = world.get_body(1).unwrap().get_angular_velocity();
    assert!(
        omega.abs() < EPSILON,
        "2D angular velocity should remain 0 with no input, got {}",
        omega,
    );
}

#[test]
fn friction_2d_slows_tangential_slide_to_rest() {
    let config: PhysicsConfig = PhysicsConfig::new(Vector2D::zero(), 0.0, 0.0);
    let mut world: PhysicsWorld2D = PhysicsWorld2D::with_config(config);
    let mut ground: RigidBody2D = RigidBody2D::new_static(1, Vector2D::new(0.0, -5.0));
    ground.update_collider(BodyCollider::Aabb(AabbCollider::new(Rect::new(
        -50.0, 0.0, 100.0, 100.0,
    ))));
    world.add_body(ground);
    let mut slider: RigidBody2D = RigidBody2D::new_dynamic(2, Vector2D::new(0.0, 0.19));
    slider.update_collider(BodyCollider::Circle(CircleCollider::from_center(
        Vector2D::zero(),
        0.2,
    )));
    world.add_body(slider);
    world
        .get_body_mut(2)
        .unwrap()
        .set_velocity(Vector2D::new(2.0, 0.0));
    let mut previous: f64 = 2.0;
    let mut monotonic: bool = true;
    for _ in 0..900 {
        world.step(1.0 / 60.0);
        let tangent: f64 = world.get_body(2).unwrap().get_velocity().get_x();
        if tangent > previous + 1e-9 {
            monotonic = false;
        }
        previous = tangent;
    }
    assert!(
        monotonic,
        "tangential speed must never increase while sliding",
    );
    let final_tangent: f64 = world.get_body(2).unwrap().get_velocity().get_x();
    assert!(
        final_tangent < 0.5,
        "friction should bleed off most tangential speed, got {}",
        final_tangent,
    );
}

#[test]
fn friction_2d_frictionless_body_keeps_sliding() {
    let config: PhysicsConfig = PhysicsConfig::new(Vector2D::zero(), 0.0, 0.0);
    let mut world: PhysicsWorld2D = PhysicsWorld2D::with_config(config);
    let mut ground: RigidBody2D = RigidBody2D::new_static(1, Vector2D::new(0.0, -5.0));
    ground.update_collider(BodyCollider::Aabb(AabbCollider::new(Rect::new(
        -50.0, 0.0, 100.0, 100.0,
    ))));
    world.add_body(ground);
    let mut slider: RigidBody2D = RigidBody2D::new_dynamic(2, Vector2D::new(0.0, 0.19));
    slider.update_collider(BodyCollider::Circle(CircleCollider::from_center(
        Vector2D::zero(),
        0.2,
    )));
    slider.set_friction(0.0);
    world.add_body(slider);
    world
        .get_body_mut(2)
        .unwrap()
        .set_velocity(Vector2D::new(2.0, 0.0));
    for _ in 0..900 {
        world.step(1.0 / 60.0);
    }
    let final_tangent: f64 = world.get_body(2).unwrap().get_velocity().get_x();
    assert!(
        final_tangent > 1.0,
        "a zero-friction body must keep sliding, got {}",
        final_tangent,
    );
}

#[test]
fn friction_3d_slows_tangential_slide() {
    let config: PhysicsConfig3D = PhysicsConfig3D::new(Vector3D::zero(), 0.0, 0.0);
    let mut world: PhysicsWorld3D = PhysicsWorld3D::with_config(config);
    let mut ground: RigidBody3D = RigidBody3D::new_static(1, Vector3D::zero());
    ground.update_collider(BodyCollider3D::Sphere(SphereCollider3D::from_center(
        Vector3D::zero(),
        1.0,
    )));
    world.add_body(ground);
    let mut slider: RigidBody3D = RigidBody3D::new_dynamic(2, Vector3D::new(0.0, 1.19, 0.0));
    slider.update_collider(BodyCollider3D::Sphere(SphereCollider3D::from_center(
        Vector3D::zero(),
        0.2,
    )));
    world.add_body(slider);
    world
        .get_body_mut(2)
        .unwrap()
        .set_velocity(Vector3D::new(2.0, 0.0, 0.0));
    let mut previous: f64 = 2.0;
    let mut monotonic: bool = true;
    for _ in 0..900 {
        world.step(1.0 / 60.0);
        let tangent: f64 = world.get_body(2).unwrap().get_velocity().get_x();
        if tangent > previous + 1e-9 {
            monotonic = false;
        }
        previous = tangent;
    }
    assert!(
        monotonic,
        "tangential speed must never increase while sliding",
    );
    let final_tangent: f64 = world.get_body(2).unwrap().get_velocity().get_x();
    assert!(
        final_tangent < 2.0,
        "friction must reduce tangential speed, got {}",
        final_tangent,
    );
}

fn at(x: f64, y: f64) -> Vector2D {
    Vector2D::new(x, y)
}

fn at3(x: f64, y: f64, z: f64) -> Vector3D {
    Vector3D::new(x, y, z)
}

fn close(actual: f64, expected: f64) -> bool {
    (actual - expected).abs() < 1e-9
}

#[test]
fn a_dynamic_body_is_dynamic_and_a_static_one_is_not() {
    let dynamic: RigidBody2D = RigidBody2D::new_dynamic(1, at(0.0, 0.0));
    let fixed: RigidBody2D = RigidBody2D::new_static(2, at(0.0, 0.0));
    assert!(dynamic.is_dynamic(), "new_dynamic must report dynamic");
    assert!(!fixed.is_dynamic(), "new_static must not report dynamic");
    assert_eq!(dynamic.get_id(), 1, "the id round-trips");
    assert!(
        !dynamic.get_force_accumulator().get_x().is_nan(),
        "force starts as a real number"
    );
}

#[test]
fn a_body_keeps_the_position_it_was_built_with() {
    let body: RigidBody2D = RigidBody2D::new_dynamic(7, at(3.0, -4.0));
    assert_eq!(body.get_position().get_x(), 3.0, "x round-trips");
    assert_eq!(body.get_position().get_y(), -4.0, "y round-trips");
}

#[test]
fn apply_force_accumulates_and_a_step_consumes_it() {
    let mut body: RigidBody2D = RigidBody2D::new_dynamic(1, at(0.0, 0.0));
    body.apply_force(at(10.0, 0.0));
    assert!(
        close(body.get_force_accumulator().get_x(), 10.0),
        "the force is held until the next step"
    );
    let mut world: PhysicsWorld2D = PhysicsWorld2D::with_config(PhysicsConfig::default());
    world.add_body(body);
    world.step(1.0);
    let moved: Option<&RigidBody2D> = world.get_body(1);
    let after: &RigidBody2D = moved.expect("the body must still be in the world");
    assert!(
        after.get_position().get_x() > 0.0,
        "the force accelerated it along +x"
    );
    assert!(
        close(after.get_force_accumulator().get_x(), 0.0),
        "a step must clear the accumulator"
    );
}

#[test]
fn apply_impulse_changes_velocity_directly() {
    let mut body: RigidBody2D = RigidBody2D::new_dynamic(1, at(0.0, 0.0));
    let before: f64 = body.get_velocity().get_x();
    body.apply_impulse(at(5.0, 0.0));
    let after: f64 = body.get_velocity().get_x();
    assert!(
        after > before,
        "an impulse must raise the velocity immediately, {before} -> {after}"
    );
    assert!(after > 0.0, "and it must point along the impulse");
}

#[test]
fn a_zero_mass_body_becomes_immovable() {
    let mut body: RigidBody2D = RigidBody2D::new_dynamic(1, at(0.0, 0.0));
    body.update_mass(0.0);
    assert!(
        close(body.get_inverse_mass(), 0.0),
        "an infinite mass has a zero inverse mass"
    );
    let before: f64 = body.get_position().get_x();
    body.apply_force(at(1000.0, 0.0));
    let mut world: PhysicsWorld2D = PhysicsWorld2D::with_config(PhysicsConfig::default());
    world.add_body(body);
    world.step(1.0);
    let after: f64 = world
        .get_body(1)
        .expect("body present")
        .get_position()
        .get_x();
    assert!(
        close(before, after),
        "an immovable body must not be accelerated, {before} -> {after}"
    );
}

#[test]
fn bounding_box_is_none_without_a_collider_and_some_with_one() {
    let mut body: RigidBody2D = RigidBody2D::new_dynamic(1, at(0.0, 0.0));
    assert!(
        body.bounding_box().is_none(),
        "a body with no collider has no box"
    );
    body.update_collider(BodyCollider::Aabb(AabbCollider::from_center(
        at(0.0, 0.0),
        2.0,
        4.0,
    )));
    let box_rect: Rect = body.bounding_box().expect("a collider yields a box");
    assert!(
        close(box_rect.size().get_x(), 2.0),
        "the box is twice the half width"
    );
    assert!(
        close(box_rect.size().get_y(), 4.0),
        "and twice the half height"
    );
}

#[test]
fn three_dimensional_bodies_cover_the_same_ground() {
    let mut body: RigidBody3D = RigidBody3D::new_dynamic(1, at3(0.0, 0.0, 0.0));
    let fixed: RigidBody3D = RigidBody3D::new_static(2, at3(0.0, 0.0, 0.0));
    assert!(body.is_dynamic(), "new_dynamic reports dynamic");
    assert!(!fixed.is_dynamic(), "new_static does not");
    let before: f64 = body.get_angular_velocity().get_x();
    body.apply_torque(at3(0.0, 0.0, 4.0));
    assert!(
        close(body.get_torque_accumulator().get_z(), 4.0),
        "a torque is accumulated, not applied instantly"
    );
    assert!(
        close(body.get_angular_velocity().get_x(), before),
        "and the angular velocity does not move until a step"
    );
    let mut world: PhysicsWorld3D = PhysicsWorld3D::with_config(PhysicsConfig3D::default());
    world.add_body(body);
    world.step(1.0);
    let stepped: &RigidBody3D = world.get_body(1).expect("body 1 must be present");
    assert!(
        stepped.get_angular_velocity().get_z() > 0.0,
        "the step turns the accumulated torque into spin, got {:?}",
        stepped.get_angular_velocity()
    );
    assert!(
        close(stepped.get_torque_accumulator().get_z(), 0.0),
        "and clears the accumulator"
    );
}

#[test]
fn the_world_looks_bodies_up_and_forgets_them_by_id() {
    let mut world: PhysicsWorld2D = PhysicsWorld2D::with_config(PhysicsConfig::default());
    world.add_body(RigidBody2D::new_dynamic(1, at(0.0, 0.0)));
    world.add_body(RigidBody2D::new_dynamic(2, at(10.0, 0.0)));
    assert!(world.get_body(1).is_some(), "body 1 was added");
    assert!(world.get_body(2).is_some(), "body 2 was added");
    assert!(world.get_body(3).is_none(), "body 3 was never added");
    world.remove_body(1);
    assert!(world.get_body(1).is_none(), "body 1 was removed");
    assert!(
        world.get_body(2).is_some(),
        "removing one body keeps the other"
    );
}

#[test]
fn a_mutable_borrow_lets_the_caller_move_a_body_in_place() {
    let mut world: PhysicsWorld2D = PhysicsWorld2D::with_config(PhysicsConfig::default());
    world.add_body(RigidBody2D::new_dynamic(1, at(0.0, 0.0)));
    let borrowed: Option<&mut RigidBody2D> = world.get_body_mut(1);
    let body: &mut RigidBody2D = borrowed.expect("body 1 must be present");
    body.set_position(at(42.0, 7.0));
    assert_eq!(
        world.get_body(1).expect("body 1").get_position().get_x(),
        42.0,
        "the write through the mutable borrow stuck"
    );
}

#[test]
fn stepping_an_empty_or_singleton_world_is_harmless() {
    let mut empty: PhysicsWorld2D = PhysicsWorld2D::with_config(PhysicsConfig::default());
    empty.step(1.0);
    empty.step(0.016);
    let mut single: PhysicsWorld2D = PhysicsWorld2D::with_config(PhysicsConfig::default());
    single.add_body(RigidBody2D::new_dynamic(1, at(0.0, 0.0)));
    single.step(0.016);
    assert!(
        single.get_body(1).is_some(),
        "a single body needs no pair resolution and must survive"
    );
}

#[test]
fn a_static_body_is_not_integrated_by_a_step() {
    let mut world: PhysicsWorld2D = PhysicsWorld2D::with_config(PhysicsConfig::default());
    world.add_body(RigidBody2D::new_static(1, at(5.0, 5.0)));
    world.step(1.0);
    let body: &RigidBody2D = world.get_body(1).expect("body 1 must be present");
    assert!(
        close(body.get_position().get_x(), 5.0),
        "a static body must not drift, got {:?}",
        body.get_position()
    );
}

#[test]
fn gravity_pulls_a_free_body_towards_the_configured_direction() {
    let mut config: PhysicsConfig = PhysicsConfig::default();
    config.set_gravity(at(0.0, -10.0));
    let mut world: PhysicsWorld2D = PhysicsWorld2D::with_config(config);
    world.add_body(RigidBody2D::new_dynamic(1, at(0.0, 0.0)));
    world.step(0.5);
    let body: &RigidBody2D = world.get_body(1).expect("body 1 must be present");
    assert!(
        body.get_position().get_y() < 0.0,
        "downward gravity must move it to negative y, got {:?}",
        body.get_position()
    );
}

#[test]
fn zero_inertia_makes_a_three_dimensional_body_immovable_about_every_axis() {
    let mut body: RigidBody3D = RigidBody3D::new_dynamic(1, at3(0.0, 0.0, 0.0));
    body.update_inertia(0.0);
    assert!(
        close(body.get_inverse_inertia(), 0.0),
        "zero inertia gives zero inverse inertia"
    );
    body.apply_torque(at3(0.0, 0.0, 4.0));
    let mut world: PhysicsWorld3D = PhysicsWorld3D::with_config(PhysicsConfig3D::default());
    world.add_body(body);
    world.step(1.0);
    let stepped: &RigidBody3D = world.get_body(1).expect("body 1 must be present");
    assert!(
        close(stepped.get_angular_velocity().get_z(), 0.0),
        "a body with no inertia cannot be spun, got {:?}",
        stepped.get_angular_velocity()
    );
}

#[test]
fn a_dynamic_body_reports_the_dynamic_body_type() {
    let body: RigidBody2D = RigidBody2D::new_dynamic(1, Vector2D::new(4.0, 5.0));
    assert_eq!(body.get_body_type(), BodyType::Dynamic);
    assert!(body.is_dynamic(), "a dynamic body takes part in simulation");
}

#[test]
fn a_static_body_reports_the_static_body_type_and_stays_put() {
    let body: RigidBody2D = RigidBody2D::new_static(2, Vector2D::new(4.0, 5.0));
    assert_eq!(body.get_body_type(), BodyType::Static);
    assert!(!body.is_dynamic(), "a static body is never moved by forces");
}

#[test]
fn the_three_dimensional_constructors_set_the_same_two_body_types() {
    assert_eq!(
        RigidBody3D::new_dynamic(3, Vector3D::zero()).get_body_type(),
        BodyType::Dynamic
    );
    assert_eq!(
        RigidBody3D::new_static(4, Vector3D::zero()).get_body_type(),
        BodyType::Static
    );
}