gizmo-engine 0.9.1

A custom ECS and physics engine aimed for realistic simulations.
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
//! Automatic BOX-COLLIDER — derive the size from `Transform.scale` (do NOT write the size TWICE).
//!
//! When spawning a box the developer has until now had to write the size twice: once as the
//! visual scale (`Transform::with_scale(half)`), once as the matching collider
//! (`Collider::box_collider(half)`). If the two diverge, physics and the visual silently come
//! apart from each other. This is the perfect example of "forcing the developer into low-level
//! repetition" that the user pointed out.
//!
//! Solution — an opt-in marker that follows the [`Spin`](crate::systems::spin) /
//! [`LifetimeSystem`](crate::systems::lifetime) idiom: add an [`AutoBoxCollider`] component (+ a
//! placeholder box collider) and let [`PhysicsPlugin`](crate::plugins::PhysicsPlugin) run the
//! system automatically. [`AutoBoxColliderSystem`] resolves every fresh marker once, BEFORE the
//! first physics step: `Transform.scale · base` → collider half-extents, inertia is re-derived,
//! then the marker is removed. The size is written only ONCE (as Transform.scale).
//!
//! There is also a synchronous short-cut for `Prefab`
//! ([`Prefab::auto_box_collider`](crate::bundles::Prefab::auto_box_collider)): since
//! `Prefab::spawn` already holds the transform in hand, it resolves the collider at spawn time
//! (no one-frame delay). Both paths use the same pure [`derived_box_half_extents`] helper.
//!
//! ```
//! use gizmo::prelude::*;
//! # use gizmo::core::system::System;
//! # use gizmo::systems::auto_collider::AutoBoxColliderSystem;
//! # use gizmo_physics_core::ColliderShape;
//! # let mut world = World::new();
//! // The raw (Prefab-less) path — works through any spawn channel:
//! let plank = world.spawn();
//! world.add_component(
//!     plank,
//!     Transform::new(Vec3::ZERO).with_scale(Vec3::new(4.0, 0.4, 1.0)), // the size, written ONCE
//! );
//! world.add_component(plank, Collider::box_collider(Vec3::ONE)); // placeholder
//! world.add_component(plank, AutoBoxCollider::new()); // resolved before the first physics step
//!
//! AutoBoxColliderSystem.run(&world, 1.0 / 60.0);
//! world.apply_commands();
//!
//! let colliders = world.borrow::<Collider>();
//! let shape = &colliders.get(plank.id()).unwrap().shape;
//! assert!(matches!(shape, ColliderShape::Box(b) if b.half_extents == Vec3::new(4.0, 0.4, 1.0)));
//! // …and the marker is gone, so the resolution happens exactly once.
//! assert!(world.borrow::<AutoBoxCollider>().get(plank.id()).is_none());
//! ```

use gizmo_core::world::World;
use gizmo_math::Vec3;
use gizmo_physics_core::{BoxShape, Collider, ColliderShape, Transform};
use gizmo_physics_rigid::components::RigidBody;

/// Minimum half-extent that clamps a degenerate (zero) scale axis — prevents a zero-thickness
/// box (a degenerate manifold / NaN in broadphase/narrowphase) from forming.
pub const MIN_HE: f32 = 1e-4;

/// Derives the box HALF-EXTENTS from scale + base factor — the SINGLE source of the size math
/// (both [`AutoBoxColliderSystem`] and `Prefab` call this, so the two paths never diverge).
/// `.abs()` guards against negative scale; `.max(MIN_HE)` clamps a degenerate axis. No GPU →
/// headless testable.
pub fn derived_box_half_extents(scale: Vec3, base: Vec3) -> Vec3 {
    (scale * base).abs().max(Vec3::splat(MIN_HE))
}

/// Opt-in MARKER component: "size my box collider from `Transform.scale`."
///
/// Add it together with a placeholder box [`Collider`]; [`AutoBoxColliderSystem`] resolves it
/// BEFORE the first physics step. `base` is the per-site factor: [`AutoBoxCollider::new`]
/// (base = `Vec3::ONE`) for `create_cube`, whose mesh half-extent == scale;
/// [`AutoBoxCollider::scaled`]`(Vec3::splat(0.5))` for the 0.5-factor mesh family.
///
/// NOTE (hierarchy trap): the local `Transform.scale` is read, NOT the composed world scale — a
/// box under a parent with a non-zero scale is sized incorrectly (consistent with the rest of
/// the physics: the local Transform is taken to be the world one). Also, **resolution happens
/// once**; if you change `Transform.scale` after spawn the collider goes stale (deliberate, in
/// order not to perturb the warm-start/block-solver every frame).
///
/// NOTE(translation): the original says "non-zero scale" here; the intended sense is most likely
/// "non-unit scale" (a scale other than 1). Translated literally rather than corrected.
///
/// ⚠️ TIMING TRAP: this marker is resolved through the `Added<T>` gate by
/// [`AutoBoxColliderSystem`], which runs BEFORE the physics step. In the windowed app loop the
/// `update` hook runs AFTER the physics `schedule.run` → the `added_tick` of a marker
/// **spawned in the update hook** ends up equal to the next frame's `change_ref_tick`, and
/// `Added`, which is a strict `>`, MISSES it → the marker is NEVER resolved (the collider stays
/// at the placeholder). Therefore the marker path is safe ONLY for entities spawned in setup or
/// inside a SYSTEM that runs before `physics_step`. For runtime (update-hook) spawns use the
/// SYNCHRONOUS path: [`Prefab::auto_box_collider`](crate::bundles::Prefab::auto_box_collider) or
/// an explicit `Collider::box_collider(scale)`. Regression test:
/// `marker_spawned_after_schedule_run_is_missed_by_added_gate`.
#[derive(Debug, Clone, Copy)]
pub struct AutoBoxCollider {
    /// Per-axis base factor multiplied by the scale (usually `Vec3::ONE`).
    pub base: Vec3,
}

impl AutoBoxCollider {
    /// `base = Vec3::ONE` — mesh half-extent == `Transform.scale` (e.g. `create_cube`).
    pub fn new() -> Self {
        Self { base: Vec3::ONE }
    }

    /// Custom per-axis base factor (e.g. `Vec3::splat(0.5)` → half-extent = scale/2).
    pub fn scaled(base: Vec3) -> Self {
        Self { base }
    }
}

impl Default for AutoBoxCollider {
    fn default() -> Self {
        Self::new()
    }
}

gizmo_core::impl_component!(AutoBoxCollider);

/// Sizes the box collider of entities carrying a fresh [`AutoBoxCollider`] marker from
/// `Transform.scale` and re-derives the inertia; then removes the marker. Because it is gated
/// with `Added<AutoBoxCollider>` it runs EXACTLY ONCE per marker (even if the marker cannot be
/// removed). It does not match entities whose marker is untouched → determinism-neutral.
/// [`PhysicsPlugin`] adds it automatically BEFORE `physics_step`.
///
/// [`PhysicsPlugin`]: crate::plugins::PhysicsPlugin
pub struct AutoBoxColliderSystem;

impl gizmo_core::system::System for AutoBoxColliderSystem {
    fn access_info(&self) -> gizmo_core::system::AccessInfo {
        let mut info = gizmo_core::system::AccessInfo::new();
        // Collider + RigidBody'ye mutable erişir ve Commands ile işareti kaldırır.
        info.is_exclusive = true;
        info
    }

    #[tracing::instrument(skip_all, level = "trace", name = "auto_box_collider")]
    fn run(&mut self, world: &World, dt: f32) {
        use gizmo_core::commands::Commands;
        use gizmo_core::query::{Added, Mut};
        use gizmo_core::system::SystemParam;

        // Commands YOKSA nazikçe küçül: yine de boyutlandır (Added geçidi doğruluğu korur),
        // yalnız işaret kaldırma atlanır → işaret öylece kalır (atıl).
        let mut commands = Commands::fetch(world, dt).ok();
        if commands.is_none() {
            tracing::trace!(
                "AutoBoxColliderSystem: Commands (CommandQueue) yok — işaretler boyutlanacak ama kaldırılamayacak"
            );
        }

        // Bu çalıştırmada gerçekten yapılan işi say → çıkışta tek AGGREGATE debug! (per-entity
        // log YOK; Added geçidi çoğu frame'de 0 işaret döndürür).
        let mut resolved = 0usize;
        let mut skipped_non_box = 0usize;
        let mut inertia_refreshed = 0usize;

        // ── PASS 1: collider'ı boyutlandır (+ işaret kaldırmayı kuyruğa al). ──
        // RigidBody GEREKTİRMEZ → trigger-only (RigidBody'siz) kutular da boyutlanır.
        // SAFETY: exclusive sistem; scheduler bu çalışırken disjoint mutable erişim garanti eder.
        if let Some(mut q) = unsafe {
            world
                .query_unchecked::<(&Transform, Mut<Collider>, &AutoBoxCollider, Added<AutoBoxCollider>)>()
        } {
            for (id, (t, mut col, cfg, _)) in q.iter_mut() {
                // Savunma: bir küre/kapsül collider'ı ASLA kutuya dönüştürme.
                if !matches!(col.shape, ColliderShape::Box(_)) {
                    skipped_non_box += 1;
                    tracing::warn!(
                        entity = id,
                        "AutoBoxCollider kutu-olmayan collider'a takılı — atlanıyor"
                    );
                    continue;
                }
                let he = derived_box_half_extents(t.scale, cfg.base);
                // YALNIZ .shape'e dokun → material/friction/restitution/layer/is_trigger korunur.
                col.shape = ColliderShape::Box(BoxShape { half_extents: he });
                resolved += 1;

                if let (Some(cmds), Some(e)) = (commands.as_mut(), world.entity(id)) {
                    cmds.entity(e).remove::<AutoBoxCollider>();
                }
            }
        }

        // ── PASS 2: ataleti tazele (yalnız RigidBody'si OLAN varlıklar). ──
        // Mevcut `update_inertia_from_collider`'ı YENİDEN KULLAN — Box kolu yarı→tam
        // ikilemeyi kendi içinde yapar, böylece FULL-vs-HALF ×2 tuzağı yapısal olarak imkânsız.
        // Statik/kinematik yazımı zararsızca yutar (inv_inertia=0); trigger-only doğal dışlanır.
        if let Some(mut q) = unsafe {
            world
                .query_unchecked::<(&Transform, Mut<RigidBody>, &Collider, &AutoBoxCollider, Added<AutoBoxCollider>)>()
        } {
            for (_id, (t, mut rb, col, cfg, _)) in q.iter_mut() {
                // Pass 1 ile SİMETRİ: kutu-olmayan collider'a takılı işarette Pass 1 şekli
                // KORUDU (atladı); burada da inertia'yı EZME — yoksa collider küre kalır ama
                // rb.local_inertia kutu-inertia olur (≫60× hata, tutarsız iki pass).
                if !matches!(col.shape, ColliderShape::Box(_)) {
                    continue;
                }
                let he = derived_box_half_extents(t.scale, cfg.base);
                rb.update_inertia_from_collider(&Collider::box_collider(he));
                inertia_refreshed += 1;
            }
        }

        if resolved > 0 || skipped_non_box > 0 {
            tracing::debug!(
                resolved,
                inertia_refreshed,
                skipped_non_box,
                "AutoBoxCollider: taze işaretler Transform.scale'den çözüldü"
            );
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use gizmo_core::commands::CommandQueue;
    use gizmo_core::system::System;

    fn world_with_commands() -> World {
        let mut world = World::new();
        world.insert_resource(CommandQueue::default());
        world
    }

    // "physics_step" etiketli bir sonda-çalışan prob: çalıştığı anda gördüğü ilk kutu
    // collider'ının yarı-genişliğini bir kaynağa yazar → resolver ondan ÖNCE çözdüyse
    // ölçekli değeri yakalar.
    #[derive(Default)]
    struct ProbeCapture {
        he: Option<Vec3>,
    }

    struct ProbeSystem;
    impl System for ProbeSystem {
        fn access_info(&self) -> gizmo_core::system::AccessInfo {
            let mut i = gizmo_core::system::AccessInfo::new();
            i.is_exclusive = true;
            i
        }
        fn run(&mut self, world: &World, _dt: f32) {
            let mut captured = None;
            if let Some(q) = world.query::<&Collider>() {
                for (_id, col) in q.iter() {
                    if let ColliderShape::Box(b) = &col.shape {
                        captured = Some(b.half_extents);
                        break;
                    }
                }
            }
            if let Some(mut cap) = world.get_resource_mut::<ProbeCapture>() {
                cap.he = captured;
            }
        }
    }

    struct NoopSystem;
    impl System for NoopSystem {
        fn access_info(&self) -> gizmo_core::system::AccessInfo {
            gizmo_core::system::AccessInfo::new()
        }
        fn run(&mut self, _w: &World, _dt: f32) {}
    }

    /// TRAP DOCUMENT: if a marker is spawned AFTER the physics `schedule.run` (e.g. in the update
    /// hook), its `added_tick` becomes EQUAL to the next frame's `change_ref_tick` →
    /// `Added`, which is a strict `>`, MISSES it → the marker is NEVER resolved. Therefore
    /// load-bearing statics spawned in the update hook must be sized NOT via the marker path but
    /// via the synchronous path (Prefab or an explicit collider). (This is why the yikim_ustasi
    /// level transition spawns its statics with an explicit collider.)
    #[test]
    fn marker_spawned_after_schedule_run_is_missed_by_added_gate() {
        use gizmo_core::system::{Schedule, SystemConfig};

        let mut world = world_with_commands();
        let mut schedule = Schedule::new();
        schedule.add_di_system(
            SystemConfig::new(Box::new(AutoBoxColliderSystem))
                .label("auto_box_collider")
                .before("physics_step"),
        );
        schedule.add_di_system(SystemConfig::new(Box::new(NoopSystem)).label("physics_step"));

        // Frame 1: fizik loop'u (henüz marker yok).
        schedule.run(&mut world, 0.016);

        // "update hook" (fizikten SONRA): marker'lı statik spawn'la.
        let e = world.spawn();
        world.add_component(e, Transform::new(Vec3::ZERO).with_scale(Vec3::new(12.0, 0.6, 10.0)));
        world.add_component(e, Collider::box_collider(Vec3::ONE));
        world.add_component(e, RigidBody::new_static());
        world.add_component(e, AutoBoxCollider::new());
        world.apply_commands();

        // Sonraki frame'lerin fizik loop'ları.
        schedule.run(&mut world, 0.016);
        schedule.run(&mut world, 0.016);

        // Marker çözülmedi → collider hâlâ yer-tutucu birim kutu (senkron yol şart).
        let col = world.borrow::<Collider>().get(e.id()).cloned().unwrap();
        match col.shape {
            ColliderShape::Box(b) => assert_eq!(
                b.half_extents,
                Vec3::ONE,
                "update-hook marker'ı Added ile çözülmez — tuzak belgelendi"
            ),
            _ => panic!("kutu olmalı"),
        }
    }

    /// Ordering-edge: the resolver must be wired with `.before("physics_step")` → by the time the
    /// system labeled "physics_step" runs the collider must ALREADY be a scaled box (the first
    /// physics step never runs with the placeholder unit box). If a wrong or missing label
    /// silently dropped the ordering, this test would break.
    #[test]
    fn resolver_runs_before_physics_step_label() {
        use gizmo_core::system::{Schedule, SystemConfig};

        let mut world = world_with_commands();
        world.insert_resource(ProbeCapture::default());
        let e = world.spawn();
        world.add_component(e, Transform::new(Vec3::ZERO).with_scale(Vec3::new(2.0, 3.0, 4.0)));
        world.add_component(e, Collider::box_collider(Vec3::ONE));
        world.add_component(e, RigidBody::new(1.0, true));
        world.add_component(e, AutoBoxCollider::new());

        let mut schedule = Schedule::new();
        schedule.add_di_system(
            SystemConfig::new(Box::new(AutoBoxColliderSystem))
                .label("auto_box_collider")
                .before("physics_step"),
        );
        schedule.add_di_system(SystemConfig::new(Box::new(ProbeSystem)).label("physics_step"));

        schedule.run(&mut world, 0.016); // schedule begin_change_frame'i kendi çağırır

        let cap = world.get_resource::<ProbeCapture>().unwrap();
        assert_eq!(
            cap.he,
            Some(Vec3::new(2.0, 3.0, 4.0)),
            "physics_step çalıştığında collider ölçekli olmalıydı (resolver .before ile bağlanmadı mı?)"
        );
    }

    /// Spawn a box + Transform.scale + marker; the system must match the collider to the scale,
    /// match the inertia one-to-one with the inertia of the same scaled box, and remove the marker.
    #[test]
    fn resolves_box_from_scale_and_derives_inertia() {
        let mut world = world_with_commands();
        let e = world.spawn();
        world.add_component(e, Transform::new(Vec3::ZERO).with_scale(Vec3::new(2.0, 3.0, 4.0)));
        world.add_component(e, Collider::box_collider(Vec3::ONE)); // yer-tutucu
        world.add_component(e, RigidBody::new(10.0, true));
        world.add_component(e, AutoBoxCollider::new());

        world.begin_change_frame(0); // Added penceresini aç
        AutoBoxColliderSystem.run(&world, 0.016);
        world.apply_commands();

        // collider yarı-genişliği == ölçek
        let col = world.borrow::<Collider>().get(e.id()).cloned().unwrap();
        match col.shape {
            ColliderShape::Box(b) => assert_eq!(b.half_extents, Vec3::new(2.0, 3.0, 4.0)),
            _ => panic!("kutu olmalı"),
        }

        // atalet: referans gövdeyle bire bir (FULL-vs-HALF kilidi)
        let mut reference = RigidBody::new(10.0, true);
        reference.update_inertia_from_collider(&Collider::box_collider(Vec3::new(2.0, 3.0, 4.0)));
        let rb = world.borrow::<RigidBody>().get(e.id()).cloned().unwrap();
        assert_eq!(rb.local_inertia, reference.local_inertia);

        // işaret kaldırıldı
        assert!(world.borrow::<AutoBoxCollider>().get(e.id()).is_none());
    }

    /// base = 0.5 → half-extent = scale / 2 (the 0.5-factor mesh family).
    #[test]
    fn base_factor_halves_extents() {
        let mut world = world_with_commands();
        let e = world.spawn();
        world.add_component(e, Transform::new(Vec3::ZERO).with_scale(Vec3::splat(4.0)));
        world.add_component(e, Collider::box_collider(Vec3::ONE));
        world.add_component(e, RigidBody::new(1.0, true));
        world.add_component(e, AutoBoxCollider::scaled(Vec3::splat(0.5)));

        world.begin_change_frame(0);
        AutoBoxColliderSystem.run(&world, 0.016);
        world.apply_commands();

        let col = world.borrow::<Collider>().get(e.id()).cloned().unwrap();
        match col.shape {
            ColliderShape::Box(b) => assert_eq!(b.half_extents, Vec3::splat(2.0)),
            _ => panic!("kutu olmalı"),
        }
    }

    /// Added gate: even if the marker is NOT REMOVED (no Commands) the system must not re-size on
    /// the second frame — idempotency rests on Added, not on the presence of the marker.
    #[test]
    fn runs_once_via_added_gate_without_commands() {
        let mut world = World::new(); // CommandQueue YOK → işaret kalır
        let e = world.spawn();
        world.add_component(e, Transform::new(Vec3::ZERO).with_scale(Vec3::new(2.0, 2.0, 2.0)));
        world.add_component(e, Collider::box_collider(Vec3::ONE));
        world.add_component(e, RigidBody::new(1.0, true));
        world.add_component(e, AutoBoxCollider::new());

        world.begin_change_frame(0);
        AutoBoxColliderSystem.run(&world, 0.016);
        // Commands yok → işaret hâlâ orada
        assert!(world.borrow::<AutoBoxCollider>().get(e.id()).is_some(), "işaret kalmalı");

        // Frame 2: birileri collider'ı bozsun, sonra sistem YENİDEN çalışsın.
        // Added artık tetiklenmediği için collider dokunulmamış kalmalı.
        {
            let mut q = world.borrow_mut::<Collider>();
            let mut c = q.get_mut(e.id()).unwrap();
            c.shape = ColliderShape::Box(BoxShape { half_extents: Vec3::splat(9.0) });
        }
        let prev = world.tick;
        world.begin_change_frame(prev);
        AutoBoxColliderSystem.run(&world, 0.016);

        let col = world.borrow::<Collider>().get(e.id()).cloned().unwrap();
        match col.shape {
            // 9.0 korunmalı — sistem yeniden boyutlandırmadı (Added kapalı).
            ColliderShape::Box(b) => assert_eq!(b.half_extents, Vec3::splat(9.0)),
            _ => panic!("kutu olmalı"),
        }
    }

    /// A non-box collider (sphere) MUST NOT BE CHANGED even if it carries the marker.
    #[test]
    fn non_box_collider_is_skipped() {
        let mut world = world_with_commands();
        let e = world.spawn();
        world.add_component(e, Transform::new(Vec3::ZERO).with_scale(Vec3::splat(3.0)));
        world.add_component(e, Collider::sphere(0.5));
        world.add_component(e, RigidBody::new(1.0, true));
        world.add_component(e, AutoBoxCollider::new());

        world.begin_change_frame(0);
        AutoBoxColliderSystem.run(&world, 0.016);
        world.apply_commands();

        let col = world.borrow::<Collider>().get(e.id()).cloned().unwrap();
        assert!(matches!(col.shape, ColliderShape::Sphere(_)), "küre korunmalı");
    }

    /// A degenerate (zero) scale axis must be clamped to MIN_HE — no zero-thickness box.
    #[test]
    fn degenerate_scale_is_clamped() {
        let mut world = world_with_commands();
        let e = world.spawn();
        world.add_component(e, Transform::new(Vec3::ZERO).with_scale(Vec3::new(0.0, 2.0, 3.0)));
        world.add_component(e, Collider::box_collider(Vec3::ONE));
        world.add_component(e, RigidBody::new(1.0, true));
        world.add_component(e, AutoBoxCollider::new());

        world.begin_change_frame(0);
        AutoBoxColliderSystem.run(&world, 0.016);
        world.apply_commands();

        let col = world.borrow::<Collider>().get(e.id()).cloned().unwrap();
        match col.shape {
            ColliderShape::Box(b) => {
                assert_eq!(b.half_extents.x, MIN_HE);
                assert_eq!(b.half_extents.y, 2.0);
            }
            _ => panic!("kutu olmalı"),
        }
    }

    /// The collider material (friction/bounce) MUST SURVIVE the re-size.
    #[test]
    fn resize_preserves_material() {
        let mut world = world_with_commands();
        let e = world.spawn();
        world.add_component(e, Transform::new(Vec3::ZERO).with_scale(Vec3::splat(2.0)));
        world.add_component(
            e,
            Collider::box_collider(Vec3::ONE)
                .with_friction(0.85)
                .with_restitution(0.3),
        );
        world.add_component(e, RigidBody::new(1.0, true));
        world.add_component(e, AutoBoxCollider::new());

        world.begin_change_frame(0);
        AutoBoxColliderSystem.run(&world, 0.016);
        world.apply_commands();

        let col = world.borrow::<Collider>().get(e.id()).cloned().unwrap();
        match col.shape {
            ColliderShape::Box(b) => assert_eq!(b.half_extents, Vec3::splat(2.0)),
            _ => panic!("kutu olmalı"),
        }
        assert_eq!(col.material.static_friction, 0.85);
        assert_eq!(col.material.restitution, 0.3);
    }

    /// Trigger-only (RigidBody-less) entity: the collider must be sized, Pass 2 must not panic.
    #[test]
    fn trigger_only_without_rigidbody_no_panic() {
        let mut world = world_with_commands();
        let e = world.spawn();
        world.add_component(e, Transform::new(Vec3::ZERO).with_scale(Vec3::splat(5.0)));
        let mut trig = Collider::box_collider(Vec3::ONE);
        trig.is_trigger = true;
        world.add_component(e, trig);
        world.add_component(e, AutoBoxCollider::new());

        world.begin_change_frame(0);
        AutoBoxColliderSystem.run(&world, 0.016); // RigidBody yok → Pass 2 no-match, panik yok
        world.apply_commands();

        let col = world.borrow::<Collider>().get(e.id()).cloned().unwrap();
        match col.shape {
            ColliderShape::Box(b) => assert_eq!(b.half_extents, Vec3::splat(5.0)),
            _ => panic!("kutu olmalı"),
        }
        assert!(col.is_trigger, "trigger bayrağı korunmalı");
    }

    /// Static body: the collider must be sized, the inertia write is harmless (no panic).
    #[test]
    fn static_body_resizes_without_panic() {
        let mut world = world_with_commands();
        let e = world.spawn();
        world.add_component(
            e,
            Transform::new(Vec3::ZERO).with_scale(Vec3::new(600.0, 1.0, 600.0)),
        );
        world.add_component(e, Collider::box_collider(Vec3::ONE));
        world.add_component(e, RigidBody::new_static());
        world.add_component(e, AutoBoxCollider::new());

        world.begin_change_frame(0);
        AutoBoxColliderSystem.run(&world, 0.016);
        world.apply_commands();

        let col = world.borrow::<Collider>().get(e.id()).cloned().unwrap();
        match col.shape {
            ColliderShape::Box(b) => assert_eq!(b.half_extents, Vec3::new(600.0, 1.0, 600.0)),
            _ => panic!("kutu olmalı"),
        }
    }

    /// Pure helper: negative scale is made absolute, base is multiplied in, degenerate is clamped.
    #[test]
    fn derived_helper_is_pure_and_guards() {
        assert_eq!(
            derived_box_half_extents(Vec3::new(2.0, 0.5, 2.0), Vec3::ONE),
            Vec3::new(2.0, 0.5, 2.0)
        );
        assert_eq!(
            derived_box_half_extents(Vec3::splat(4.0), Vec3::splat(0.5)),
            Vec3::splat(2.0)
        );
        // negatif ölçek → mutlak değer
        assert_eq!(
            derived_box_half_extents(Vec3::new(-3.0, 2.0, 1.0), Vec3::ONE),
            Vec3::new(3.0, 2.0, 1.0)
        );
        // sıfır → MIN_HE
        assert_eq!(derived_box_half_extents(Vec3::ZERO, Vec3::ONE), Vec3::splat(MIN_HE));
    }
}