gizmo-engine 0.8.0

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
//! Bevy tarzı önceden tanımlanmış Bundle yapıları.
//!
//! Bir entity'ye birden fazla bileşeni tek seferde eklemek için kullanılır.
//!
//! ```ignore
//! world.spawn_bundle(CameraBundle {
//!     position: Vec3::new(0.0, 3.0, 10.0),
//!     fov: 60.0_f32.to_radians(),
//!     ..default()
//! });
//! ```

use crate::core::{Bundle, Entity, EntityName, World};
use crate::math::{Quat, Vec3};
use gizmo_physics_core::Transform;
use crate::renderer::components::{
    Camera, DirectionalLight, LightRole, Material, Mesh, MeshRenderer, PointLight, SpotLight,
};

// ============================================================
//  DirectionalLightBundle
// ============================================================

/// Yönlü ışık (güneş) için hazır bundle.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DirectionalLightBundle {
    pub rotation: Quat,
    pub color: Vec3,
    pub intensity: f32,
    pub role: LightRole,
}

impl Default for DirectionalLightBundle {
    fn default() -> Self {
        Self {
            rotation: Quat::from_rotation_x(-std::f32::consts::PI / 4.0),
            color: Vec3::new(1.0, 1.0, 1.0),
            intensity: 3.0,
            role: LightRole::Sun,
        }
    }
}

impl Bundle for DirectionalLightBundle {
    fn get_infos() -> Vec<gizmo_core::archetype::ComponentInfo> { vec![] }
    unsafe fn write_to_archetype(self, _arch: &mut gizmo_core::archetype::Archetype, _row: usize, _tick: u32) {}
    fn apply(self, world: &mut World, entity: Entity) {
        world.add_component(
            entity,
            Transform::new(Vec3::ZERO).with_rotation(self.rotation),
        );
        world.add_component(entity, gizmo_physics_core::components::GlobalTransform::default());
        world.add_component(
            entity,
            DirectionalLight::new(self.color, self.intensity, self.role),
        );
    }
}

// ============================================================
//  PointLightBundle
// ============================================================

/// Nokta ışığı için hazır bundle.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PointLightBundle {
    pub position: Vec3,
    pub color: Vec3,
    pub intensity: f32,
    pub radius: f32,
}

impl Default for PointLightBundle {
    fn default() -> Self {
        Self {
            position: Vec3::ZERO,
            color: Vec3::new(1.0, 1.0, 1.0),
            intensity: 5.0,
            radius: 20.0,
        }
    }
}

impl Bundle for PointLightBundle {
    fn get_infos() -> Vec<gizmo_core::archetype::ComponentInfo> { vec![] }
    unsafe fn write_to_archetype(self, _arch: &mut gizmo_core::archetype::Archetype, _row: usize, _tick: u32) {}
    fn apply(self, world: &mut World, entity: Entity) {
        world.add_component(entity, Transform::new(self.position));
        world.add_component(entity, gizmo_physics_core::components::GlobalTransform::default());
        world.add_component(
            entity,
            PointLight::new(self.color, self.intensity, self.radius),
        );
    }
}

// ============================================================
//  SpotLightBundle
// ============================================================

/// Spot ışığı için hazır bundle.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SpotLightBundle {
    pub position: Vec3,
    pub rotation: Quat,
    pub color: Vec3,
    pub intensity: f32,
    pub radius: f32,
    pub inner_angle: f32,
    pub outer_angle: f32,
}

impl Default for SpotLightBundle {
    fn default() -> Self {
        Self {
            position: Vec3::ZERO,
            rotation: Quat::IDENTITY,
            color: Vec3::new(1.0, 1.0, 1.0),
            intensity: 10.0,
            radius: 30.0,
            inner_angle: 0.4,
            outer_angle: 0.6,
        }
    }
}

impl Bundle for SpotLightBundle {
    fn get_infos() -> Vec<gizmo_core::archetype::ComponentInfo> { vec![] }
    unsafe fn write_to_archetype(self, _arch: &mut gizmo_core::archetype::Archetype, _row: usize, _tick: u32) {}
    fn apply(self, world: &mut World, entity: Entity) {
        world.add_component(
            entity,
            Transform::new(self.position).with_rotation(self.rotation),
        );
        world.add_component(entity, gizmo_physics_core::components::GlobalTransform::default());
        world.add_component(
            entity,
            SpotLight::new(
                self.color,
                self.intensity,
                self.radius,
                self.inner_angle,
                self.outer_angle,
            ),
        );
    }
}

// ============================================================
//  CameraBundle
// ============================================================

/// Kamera için hazır bundle.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CameraBundle {
    pub position: Vec3,
    pub fov: f32,
    pub near: f32,
    pub far: f32,
    pub yaw: f32,
    pub pitch: f32,
    pub exposure: f32,
    pub primary: bool,
}

impl Default for CameraBundle {
    fn default() -> Self {
        Self {
            position: Vec3::new(0.0, 5.0, 10.0),
            fov: std::f32::consts::FRAC_PI_3,
            near: 0.1,
            far: 1500.0,
            yaw: 0.0,
            pitch: 0.0,
            exposure: 1.0,
            primary: true,
        }
    }
}

impl Bundle for CameraBundle {
    fn get_infos() -> Vec<gizmo_core::archetype::ComponentInfo> { vec![] }
    unsafe fn write_to_archetype(self, _arch: &mut gizmo_core::archetype::Archetype, _row: usize, _tick: u32) {}
    fn apply(self, world: &mut World, entity: Entity) {
        world.add_component(entity, Transform::new(self.position));
        world.add_component(entity, gizmo_physics_core::components::GlobalTransform::default());
        let mut cam = Camera::new(
            self.fov,
            self.near,
            self.far,
            self.yaw,
            self.pitch,
            self.primary,
        );
        cam.exposure = self.exposure;
        world.add_component(entity, cam);
    }
}

// ============================================================
//  MeshBundle
// ============================================================

/// Mesh + Material + MeshRenderer için hazır bundle.
///
/// ```ignore
/// world.spawn_bundle(
///     MeshBundle::new(renderer.create_cube(), my_material)
///         .with_name("Oyuncu")
///         .at(Vec3::new(0.0, 5.0, 0.0))
/// );
/// ```
pub struct MeshBundle {
    pub position: Vec3,
    pub rotation: Quat,
    pub scale: Vec3,
    pub mesh: crate::core::asset::Handle<Mesh>,
    pub material: crate::core::asset::Handle<Material>,
    pub name: Option<String>,
}

impl MeshBundle {
    /// Yeni bir MeshBundle oluşturur (mesh ve material zorunlu).
    pub fn new(
        mesh: crate::core::asset::Handle<Mesh>,
        material: crate::core::asset::Handle<Material>,
    ) -> Self {
        Self {
            position: Vec3::ZERO,
            rotation: Quat::IDENTITY,
            scale: Vec3::ONE,
            mesh,
            material,
            name: None,
        }
    }

    /// Pozisyon ayarlar.
    pub fn at(mut self, position: Vec3) -> Self {
        self.position = position;
        self
    }

    /// Rotasyon ayarlar.
    pub fn with_rotation(mut self, rotation: Quat) -> Self {
        self.rotation = rotation;
        self
    }

    /// Ölçek ayarlar.
    pub fn with_scale(mut self, scale: Vec3) -> Self {
        self.scale = scale;
        self
    }

    /// İsim verir.
    pub fn with_name(mut self, name: &str) -> Self {
        self.name = Some(name.to_string());
        self
    }
}

impl Bundle for MeshBundle {
    fn get_infos() -> Vec<gizmo_core::archetype::ComponentInfo> { vec![] }
    unsafe fn write_to_archetype(self, _arch: &mut gizmo_core::archetype::Archetype, _row: usize, _tick: u32) {}
    fn apply(self, world: &mut World, entity: Entity) {
        world.add_component(
            entity,
            Transform::new(self.position)
                .with_rotation(self.rotation)
                .with_scale(self.scale),
        );
        world.add_component(entity, gizmo_physics_core::components::GlobalTransform::default());
        world.add_component(entity, self.mesh);
        world.add_component(entity, self.material);
        world.add_component(entity, MeshRenderer::new());
        if let Some(name) = self.name {
            world.add_component(entity, EntityName(name));
        }
    }
}

// ============================================================
//  RigidBodyBundle
// ============================================================

use gizmo_physics_core::Collider;
use gizmo_physics_rigid::components::{RigidBody, Velocity};

/// Fizik nesnesi oluşturmak için sıfır-yük (zero-overhead) Bundle.
/// Velocity veya Collider eklemeyi unutma hatalarını önler.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct RigidBodyBundle {
    pub rigid_body: RigidBody,
    pub velocity: Velocity,
    pub collider: Collider,
}


impl RigidBodyBundle {
    pub fn dynamic(mass: f32) -> Self {
        Self {
            rigid_body: RigidBody::new(mass, true),
            ..Default::default()
        }
    }

    pub fn static_body() -> Self {
        Self {
            rigid_body: RigidBody::new_static(),
            ..Default::default()
        }
    }

    /// Kinematic body — user-driven motion (moving platforms, scripted blades).
    /// `new_kinematic` turns CCD on by default, so fast kinematic movers get
    /// tunnelling prevention without a separate `.with_ccd()`.
    pub fn kinematic() -> Self {
        Self {
            rigid_body: RigidBody::new_kinematic(),
            ..Default::default()
        }
    }

    pub fn with_collider(mut self, collider: Collider) -> Self {
        self.collider = collider;
        self
    }

    /// Give the body an initial linear velocity (the bundle otherwise spawns at rest).
    pub fn with_velocity(mut self, linear: Vec3) -> Self {
        self.velocity = Velocity::new(linear);
        self
    }

    /// Enable Continuous Collision Detection: the body is swept against obstacles
    /// each substep so it can't tunnel through thin/other geometry at high speed.
    /// Off by default (discrete detection) — turn it on for bullets, fast balls,
    /// anything that moves more than its own thickness per frame.
    pub fn with_ccd(mut self) -> Self {
        self.rigid_body.ccd_enabled = true;
        self
    }

    /// Fiziksel hava direnci (½·ρ·Cd·A·v²) açar → düşen/uçan cisim doğal terminal hıza
    /// oturur. `cd` sürükleme katsayısı (küre ~0.47, küp ~1.05), `area` frontal alan (m²).
    /// Örn: `RigidBodyBundle::dynamic(2.0).with_air_drag(0.47, 0.5)`.
    pub fn with_air_drag(mut self, cd: f32, area: f32) -> Self {
        self.rigid_body = self.rigid_body.with_air_drag(cd, area);
        self
    }

    /// Collider'ın zıplaklığını (restitution) ayarlar.
    /// Örn: `RigidBodyBundle::dynamic(1.0).with_collider(Collider::sphere(0.5)).with_restitution(0.9)`.
    pub fn with_restitution(mut self, restitution: f32) -> Self {
        self.collider = self.collider.with_restitution(restitution);
        self
    }

    /// Collider'ın sürtünmesini ayarlar (statik = dinamik).
    pub fn with_friction(mut self, friction: f32) -> Self {
        self.collider = self.collider.with_friction(friction);
        self
    }

    /// Lineer + açısal sönümü ayarlar (kaba enerji kaybı). Gerçekçi hava direnci için
    /// `with_air_drag`.
    pub fn with_damping(mut self, linear: f32, angular: f32) -> Self {
        self.rigid_body = self.rigid_body.with_damping(linear, angular);
        self
    }

    /// Yerçekimini aç/kapat.
    pub fn with_gravity(mut self, enabled: bool) -> Self {
        self.rigid_body = self.rigid_body.with_gravity(enabled);
        self
    }

    /// Kütle merkezini (gövde-yerel) ayarlar.
    pub fn with_center_of_mass(mut self, com: Vec3) -> Self {
        self.rigid_body = self.rigid_body.with_center_of_mass(com);
        self
    }

    /// Üç dönme eksenini kilitler — cisim devrilmez (karakter, dik nesneler).
    pub fn lock_rotation(mut self) -> Self {
        self.rigid_body = self.rigid_body.lock_rotation();
        self
    }

    /// Başlangıç açısal hızı verir (rad/s).
    pub fn with_angular_velocity(mut self, angular: Vec3) -> Self {
        self.velocity.angular = angular;
        self
    }
}

impl Bundle for RigidBodyBundle {
    fn get_infos() -> Vec<gizmo_core::archetype::ComponentInfo> {
        <(RigidBody, Velocity, Collider)>::get_infos()
    }

    unsafe fn write_to_archetype(self, arch: &mut gizmo_core::archetype::Archetype, row: usize, tick: u32) {
        let mut rb = self.rigid_body;
        rb.update_inertia_from_collider(&self.collider);
        (rb, self.velocity, self.collider).write_to_archetype(arch, row, tick)
    }

    fn apply(self, world: &mut World, entity: Entity) {
        let mut rb = self.rigid_body;
        // Derive rotational inertia from the collider shape so callers don't have to
        // remember `calculate_*_inertia` — the default inertia otherwise gives wrong
        // spin dynamics. No-op for static/kinematic bodies (the calculators guard on
        // `is_dynamic`), and idempotent if the caller already set a matching inertia.
        rb.update_inertia_from_collider(&self.collider);
        (rb, self.velocity, self.collider).apply(world, entity)
    }
}

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

    #[test]
    fn ergonomic_collider_and_bundle_builders() {
        // Collider zıplaklık/sürtünme kısayolları — tam PhysicsMaterial kurmadan.
        let c = Collider::sphere(0.5).with_restitution(0.9).with_friction(0.3);
        assert_eq!(c.material.restitution, 0.9);
        assert_eq!(c.material.static_friction, 0.3);
        assert_eq!(c.material.dynamic_friction, 0.3);

        // Bundle: collider + hava direnci + zıplaklık TEK zincirde.
        let b = RigidBodyBundle::dynamic(2.0)
            .with_collider(Collider::sphere(0.5))
            .with_air_drag(0.47, 0.8)
            .with_restitution(0.85);
        assert_eq!(b.rigid_body.drag_coefficient, 0.47);
        assert_eq!(b.rigid_body.drag_area, 0.8);
        assert_eq!(b.collider.material.restitution, 0.85);

        // Genişletilmiş akıcı set: damping + gravity + lock + COM + açısal hız tek zincirde.
        let b2 = RigidBodyBundle::dynamic(1.0)
            .with_damping(0.1, 0.2)
            .with_gravity(false)
            .lock_rotation()
            .with_center_of_mass(Vec3::new(0.0, 0.5, 0.0))
            .with_angular_velocity(Vec3::new(0.0, 3.0, 0.0));
        assert_eq!(b2.rigid_body.linear_damping, 0.1);
        assert!(!b2.rigid_body.use_gravity);
        assert!(b2.rigid_body.lock_rotation_x);
        assert_eq!(b2.rigid_body.center_of_mass, Vec3::new(0.0, 0.5, 0.0));
        assert_eq!(b2.velocity.angular, Vec3::new(0.0, 3.0, 0.0));
    }

    #[test]
    fn rigid_body_bundle_derives_inertia_from_collider() {
        // Spawn purely via the bundle — no manual calculate_*_inertia.
        let mut world = World::new();
        let e = world
            .spawn_bundle(RigidBodyBundle::dynamic(2.0).with_collider(Collider::sphere(0.5)));

        let rbs = world.borrow::<RigidBody>();
        let rb = rbs.get(e.id()).expect("rigid body spawned");

        // Solid sphere: I = 0.4·m·r² = 0.4·2·0.25 = 0.2 per axis (calculate_sphere_inertia).
        assert!(
            (rb.local_inertia - Vec3::splat(0.2)).length() < 1e-6,
            "bundle must derive sphere inertia from the collider, got {:?}",
            rb.local_inertia
        );
        // Regression: must NOT be the un-derived default Vec3::splat(1.0).
        assert!(
            (rb.local_inertia - Vec3::splat(1.0)).length() > 1e-3,
            "inertia must be derived, not left at the default"
        );
    }

    #[test]
    fn rigid_body_bundle_static_body_inertia_derivation_is_noop() {
        // Static bodies must spawn fine; inertia derivation is a no-op for them.
        let mut world = World::new();
        let e = world
            .spawn_bundle(RigidBodyBundle::static_body().with_collider(Collider::sphere(0.5)));
        assert!(world.borrow::<RigidBody>().get(e.id()).is_some());
    }
}