oxiphysics-python 0.1.0

Python bindings for the OxiPhysics engine
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
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
// Copyright 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! Python-friendly wrapper types with serde support.
//!
//! All types in this module are designed for easy FFI/Python interop:
//! - No lifetimes
//! - Public fields only
//! - Serialize/Deserialize support
//! - Arrays instead of nalgebra types where applicable

#![allow(missing_docs)]

use oxiphysics_core::math::Vec3;
use oxiphysics_core::{Aabb, Transform};
use serde::{Deserialize, Serialize};

// ---------------------------------------------------------------------------
// PyVec3
// ---------------------------------------------------------------------------

/// A 3-component vector suitable for Python interop.
///
/// Uses `f64` components and maps directly to/from `oxiphysics_core::math::Vec3`.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
pub struct PyVec3 {
    /// X component.
    pub x: f64,
    /// Y component.
    pub y: f64,
    /// Z component.
    pub z: f64,
}

impl PyVec3 {
    /// Create a new `PyVec3`.
    pub fn new(x: f64, y: f64, z: f64) -> Self {
        Self { x, y, z }
    }

    /// Zero vector.
    pub fn zero() -> Self {
        Self::new(0.0, 0.0, 0.0)
    }

    /// Dot product with another vector.
    pub fn dot(&self, other: &PyVec3) -> f64 {
        self.x * other.x + self.y * other.y + self.z * other.z
    }

    /// Cross product with another vector.
    pub fn cross(&self, other: &PyVec3) -> PyVec3 {
        PyVec3::new(
            self.y * other.z - self.z * other.y,
            self.z * other.x - self.x * other.z,
            self.x * other.y - self.y * other.x,
        )
    }

    /// Squared length.
    pub fn length_squared(&self) -> f64 {
        self.x * self.x + self.y * self.y + self.z * self.z
    }

    /// Euclidean length.
    pub fn length(&self) -> f64 {
        self.length_squared().sqrt()
    }

    /// Normalized (unit) vector; returns zero vector if length is near zero.
    pub fn normalized(&self) -> PyVec3 {
        let len = self.length();
        if len < 1e-12 {
            PyVec3::zero()
        } else {
            PyVec3::new(self.x / len, self.y / len, self.z / len)
        }
    }

    /// Scale all components by `s`.
    pub fn scale(&self, s: f64) -> PyVec3 {
        PyVec3::new(self.x * s, self.y * s, self.z * s)
    }

    /// Add another vector.
    pub fn add(&self, other: &PyVec3) -> PyVec3 {
        PyVec3::new(self.x + other.x, self.y + other.y, self.z + other.z)
    }

    /// Subtract another vector.
    pub fn sub(&self, other: &PyVec3) -> PyVec3 {
        PyVec3::new(self.x - other.x, self.y - other.y, self.z - other.z)
    }

    /// Convert to a plain `[f64; 3]` array.
    pub fn to_array(&self) -> [f64; 3] {
        [self.x, self.y, self.z]
    }

    /// Create from a `[f64; 3]` array.
    pub fn from_array(arr: [f64; 3]) -> Self {
        Self::new(arr[0], arr[1], arr[2])
    }
}

impl From<Vec3> for PyVec3 {
    fn from(v: Vec3) -> Self {
        Self {
            x: v.x,
            y: v.y,
            z: v.z,
        }
    }
}

impl From<PyVec3> for Vec3 {
    fn from(v: PyVec3) -> Self {
        Vec3::new(v.x, v.y, v.z)
    }
}

impl From<[f64; 3]> for PyVec3 {
    fn from(arr: [f64; 3]) -> Self {
        Self::new(arr[0], arr[1], arr[2])
    }
}

impl From<PyVec3> for [f64; 3] {
    fn from(v: PyVec3) -> Self {
        [v.x, v.y, v.z]
    }
}

// ---------------------------------------------------------------------------
// PyTransform
// ---------------------------------------------------------------------------

/// A transform (position + quaternion rotation) suitable for Python interop.
///
/// Rotation is stored as `[x, y, z, w]` quaternion components.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PyTransform {
    /// Position in world space.
    pub position: PyVec3,
    /// Rotation as a quaternion `[x, y, z, w]`.
    pub rotation: [f64; 4],
}

impl PyTransform {
    /// Create an identity transform (origin, no rotation).
    pub fn identity() -> Self {
        Self {
            position: PyVec3::zero(),
            rotation: [0.0, 0.0, 0.0, 1.0],
        }
    }

    /// Compose two transforms: `self` applied first, then `other`.
    ///
    /// Position is summed; rotation is the quaternion product `other * self`.
    pub fn compose(&self, other: &PyTransform) -> PyTransform {
        let (ax, ay, az, aw) = (
            self.rotation[0],
            self.rotation[1],
            self.rotation[2],
            self.rotation[3],
        );
        let (bx, by, bz, bw) = (
            other.rotation[0],
            other.rotation[1],
            other.rotation[2],
            other.rotation[3],
        );
        let rx = bw * ax + bx * aw + by * az - bz * ay;
        let ry = bw * ay - bx * az + by * aw + bz * ax;
        let rz = bw * az + bx * ay - by * ax + bz * aw;
        let rw = bw * aw - bx * ax - by * ay - bz * az;
        PyTransform {
            position: PyVec3::new(
                self.position.x + other.position.x,
                self.position.y + other.position.y,
                self.position.z + other.position.z,
            ),
            rotation: [rx, ry, rz, rw],
        }
    }

    /// Invert the transform.
    pub fn inverse(&self) -> PyTransform {
        // Invert quaternion: conjugate (negated xyz, same w) / norm^2
        // For unit quaternion conjugate == inverse
        let inv_rot = [
            -self.rotation[0],
            -self.rotation[1],
            -self.rotation[2],
            self.rotation[3],
        ];
        // Inverse position: -inv_rot * position
        let px = self.position.x;
        let py = self.position.y;
        let pz = self.position.z;
        let (qx, qy, qz, qw) = (inv_rot[0], inv_rot[1], inv_rot[2], inv_rot[3]);
        // Rotate position by inv_rot
        let t_x = 2.0 * (qy * pz - qz * py);
        let t_y = 2.0 * (qz * px - qx * pz);
        let t_z = 2.0 * (qx * py - qy * px);
        let rx = px + qw * t_x + (qy * t_z - qz * t_y);
        let ry = py + qw * t_y + (qz * t_x - qx * t_z);
        let rz = pz + qw * t_z + (qx * t_y - qy * t_x);
        PyTransform {
            position: PyVec3::new(-rx, -ry, -rz),
            rotation: inv_rot,
        }
    }
}

impl Default for PyTransform {
    fn default() -> Self {
        Self::identity()
    }
}

impl From<Transform> for PyTransform {
    fn from(t: Transform) -> Self {
        let q = t.rotation;
        Self {
            position: PyVec3::from(t.position),
            rotation: [q.i, q.j, q.k, q.w],
        }
    }
}

impl From<PyTransform> for Transform {
    fn from(t: PyTransform) -> Self {
        let pos: Vec3 = t.position.into();
        let q = nalgebra::UnitQuaternion::from_quaternion(nalgebra::Quaternion::new(
            t.rotation[3],
            t.rotation[0],
            t.rotation[1],
            t.rotation[2],
        ));
        Transform::new(pos, q)
    }
}

// ---------------------------------------------------------------------------
// PyAabb
// ---------------------------------------------------------------------------

/// An axis-aligned bounding box suitable for Python interop.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PyAabb {
    /// Minimum corner.
    pub min: PyVec3,
    /// Maximum corner.
    pub max: PyVec3,
}

impl PyAabb {
    /// Create a new AABB from min and max corners.
    pub fn new(min: PyVec3, max: PyVec3) -> Self {
        Self { min, max }
    }

    /// Center of the AABB.
    pub fn center(&self) -> PyVec3 {
        PyVec3::new(
            (self.min.x + self.max.x) * 0.5,
            (self.min.y + self.max.y) * 0.5,
            (self.min.z + self.max.z) * 0.5,
        )
    }

    /// Half-extents of the AABB.
    pub fn half_extents(&self) -> PyVec3 {
        PyVec3::new(
            (self.max.x - self.min.x) * 0.5,
            (self.max.y - self.min.y) * 0.5,
            (self.max.z - self.min.z) * 0.5,
        )
    }

    /// Whether the given point is inside this AABB.
    pub fn contains_point(&self, p: PyVec3) -> bool {
        p.x >= self.min.x
            && p.x <= self.max.x
            && p.y >= self.min.y
            && p.y <= self.max.y
            && p.z >= self.min.z
            && p.z <= self.max.z
    }

    /// Whether this AABB intersects another.
    pub fn intersects(&self, other: &PyAabb) -> bool {
        self.min.x <= other.max.x
            && self.max.x >= other.min.x
            && self.min.y <= other.max.y
            && self.max.y >= other.min.y
            && self.min.z <= other.max.z
            && self.max.z >= other.min.z
    }
}

impl From<Aabb> for PyAabb {
    fn from(a: Aabb) -> Self {
        Self {
            min: PyVec3::from(a.min),
            max: PyVec3::from(a.max),
        }
    }
}

impl From<PyAabb> for Aabb {
    fn from(a: PyAabb) -> Self {
        Aabb::new(a.min.into(), a.max.into())
    }
}

// ---------------------------------------------------------------------------
// PyColliderShape
// ---------------------------------------------------------------------------

/// Shape variant for a rigid body collider.
///
/// Only primitive shapes are supported; they map to underlying collision shapes.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum PyColliderShape {
    /// A sphere with the given radius.
    Sphere {
        /// Radius in meters.
        radius: f64,
    },
    /// An axis-aligned box with given half-extents.
    Box {
        /// Half-extents `[hx, hy, hz]` in meters.
        half_extents: [f64; 3],
    },
    /// An infinite plane defined by a normal and distance from origin.
    Plane {
        /// Outward normal (should be unit length).
        normal: [f64; 3],
        /// Signed distance from origin along the normal.
        distance: f64,
    },
    /// A capsule (cylinder + two hemispheres) along the Y axis.
    Capsule {
        /// Radius of the cylinder and end hemispheres.
        radius: f64,
        /// Half-height of the cylindrical portion (total height = 2 * half_height + 2 * radius).
        half_height: f64,
    },
    /// A cylinder aligned with the Y axis.
    Cylinder {
        /// Radius of the cylinder.
        radius: f64,
        /// Half-height of the cylinder.
        half_height: f64,
    },
    /// A cone aligned with the Y axis, apex pointing up.
    Cone {
        /// Radius of the base.
        radius: f64,
        /// Half-height of the cone.
        half_height: f64,
    },
}

impl PyColliderShape {
    /// Create a sphere shape.
    pub fn sphere(radius: f64) -> Self {
        PyColliderShape::Sphere { radius }
    }

    /// Create a box shape from half-extents.
    pub fn box_shape(hx: f64, hy: f64, hz: f64) -> Self {
        PyColliderShape::Box {
            half_extents: [hx, hy, hz],
        }
    }

    /// Create a plane shape.
    pub fn plane(normal: [f64; 3], distance: f64) -> Self {
        PyColliderShape::Plane { normal, distance }
    }

    /// Create a capsule shape.
    pub fn capsule(radius: f64, half_height: f64) -> Self {
        PyColliderShape::Capsule {
            radius,
            half_height,
        }
    }

    /// Compute an approximate volume for mass/inertia estimation.
    pub fn approximate_volume(&self) -> f64 {
        const PI: f64 = std::f64::consts::PI;
        match self {
            PyColliderShape::Sphere { radius } => (4.0 / 3.0) * PI * radius * radius * radius,
            PyColliderShape::Box { half_extents } => {
                8.0 * half_extents[0] * half_extents[1] * half_extents[2]
            }
            PyColliderShape::Plane { .. } => f64::INFINITY,
            PyColliderShape::Capsule {
                radius,
                half_height,
            } => PI * radius * radius * (2.0 * half_height + (4.0 / 3.0) * radius),
            PyColliderShape::Cylinder {
                radius,
                half_height,
            } => PI * radius * radius * 2.0 * half_height,
            PyColliderShape::Cone {
                radius,
                half_height,
            } => (1.0 / 3.0) * PI * radius * radius * 2.0 * half_height,
        }
    }

    /// Whether this shape is a plane (infinite extents).
    pub fn is_infinite(&self) -> bool {
        matches!(self, PyColliderShape::Plane { .. })
    }
}

// ---------------------------------------------------------------------------
// PyRigidBodyConfig
// ---------------------------------------------------------------------------

/// Configuration for creating a new rigid body.
///
/// Passed to `PyPhysicsWorld::add_rigid_body`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PyRigidBodyConfig {
    /// Mass in kilograms. Use 0.0 or `f64::INFINITY` for a static/kinematic body.
    pub mass: f64,
    /// Initial position `[x, y, z]`.
    pub position: [f64; 3],
    /// Initial linear velocity `[vx, vy, vz]`. Defaults to zero.
    pub velocity: [f64; 3],
    /// Initial orientation as quaternion `[x, y, z, w]`. Defaults to identity.
    pub orientation: [f64; 4],
    /// Initial angular velocity `[wx, wy, wz]`. Defaults to zero.
    pub angular_velocity: [f64; 3],
    /// Collider shapes attached to this body.
    pub shapes: Vec<PyColliderShape>,
    /// Friction coefficient (0 = frictionless, 1 = high friction).
    pub friction: f64,
    /// Restitution/bounciness (0 = perfectly inelastic, 1 = perfectly elastic).
    pub restitution: f64,
    /// Whether this body is static (immovable, infinite mass).
    pub is_static: bool,
    /// Whether this body is kinematic (moved manually, not by forces).
    pub is_kinematic: bool,
    /// Whether this body can go to sleep when motion is negligible.
    pub can_sleep: bool,
    /// Linear damping coefficient (drag).
    pub linear_damping: f64,
    /// Angular damping coefficient (rotational drag).
    pub angular_damping: f64,
    /// Optional user-defined tag/name for identification.
    pub tag: Option<String>,
}

impl PyRigidBodyConfig {
    /// Create a dynamic body at the given position with default settings.
    pub fn dynamic(mass: f64, position: [f64; 3]) -> Self {
        Self {
            mass,
            position,
            velocity: [0.0; 3],
            orientation: [0.0, 0.0, 0.0, 1.0],
            angular_velocity: [0.0; 3],
            shapes: vec![],
            friction: 0.5,
            restitution: 0.3,
            is_static: false,
            is_kinematic: false,
            can_sleep: true,
            linear_damping: 0.0,
            angular_damping: 0.0,
            tag: None,
        }
    }

    /// Create a static body at the given position with default settings.
    pub fn static_body(position: [f64; 3]) -> Self {
        Self {
            mass: 0.0,
            position,
            velocity: [0.0; 3],
            orientation: [0.0, 0.0, 0.0, 1.0],
            angular_velocity: [0.0; 3],
            shapes: vec![],
            friction: 0.5,
            restitution: 0.3,
            is_static: true,
            is_kinematic: false,
            can_sleep: false,
            linear_damping: 0.0,
            angular_damping: 0.0,
            tag: None,
        }
    }

    /// Add a collider shape to this body config and return `self` for chaining.
    pub fn with_shape(mut self, shape: PyColliderShape) -> Self {
        self.shapes.push(shape);
        self
    }

    /// Set friction and return `self` for chaining.
    pub fn with_friction(mut self, friction: f64) -> Self {
        self.friction = friction;
        self
    }

    /// Set restitution and return `self` for chaining.
    pub fn with_restitution(mut self, restitution: f64) -> Self {
        self.restitution = restitution;
        self
    }

    /// Set linear damping and return `self` for chaining.
    pub fn with_linear_damping(mut self, damping: f64) -> Self {
        self.linear_damping = damping;
        self
    }

    /// Set angular damping and return `self` for chaining.
    pub fn with_angular_damping(mut self, damping: f64) -> Self {
        self.angular_damping = damping;
        self
    }

    /// Set tag and return `self` for chaining.
    pub fn with_tag(mut self, tag: impl Into<String>) -> Self {
        self.tag = Some(tag.into());
        self
    }

    /// Effective inverse mass (0 for static/infinite).
    pub fn inverse_mass(&self) -> f64 {
        if self.is_static || self.mass <= 0.0 {
            0.0
        } else {
            1.0 / self.mass
        }
    }
}

impl Default for PyRigidBodyConfig {
    fn default() -> Self {
        Self::dynamic(1.0, [0.0; 3])
    }
}

// ---------------------------------------------------------------------------
// PyContactResult
// ---------------------------------------------------------------------------

/// Result of a contact/collision between two bodies.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PyContactResult {
    /// Handle of the first body involved.
    pub body_a: u32,
    /// Handle of the second body involved.
    pub body_b: u32,
    /// Contact point in world space.
    pub contact_point: [f64; 3],
    /// Contact normal (from body_a to body_b).
    pub normal: [f64; 3],
    /// Penetration depth (positive = overlapping).
    pub depth: f64,
    /// Combined friction coefficient for this contact.
    pub friction: f64,
    /// Combined restitution coefficient for this contact.
    pub restitution: f64,
    /// Impulse applied to resolve this contact.
    pub impulse: f64,
}

impl PyContactResult {
    /// Create a new contact result.
    pub fn new(
        body_a: u32,
        body_b: u32,
        contact_point: [f64; 3],
        normal: [f64; 3],
        depth: f64,
    ) -> Self {
        Self {
            body_a,
            body_b,
            contact_point,
            normal,
            depth,
            friction: 0.5,
            restitution: 0.3,
            impulse: 0.0,
        }
    }

    /// Whether this contact represents a genuine collision (positive depth).
    pub fn is_colliding(&self) -> bool {
        self.depth > 0.0
    }

    /// Relative velocity of body_a with respect to body_b projected onto the normal.
    /// A negative value means separating; positive means approaching.
    pub fn separating_velocity(&self, vel_a: [f64; 3], vel_b: [f64; 3]) -> f64 {
        let rel = [
            vel_a[0] - vel_b[0],
            vel_a[1] - vel_b[1],
            vel_a[2] - vel_b[2],
        ];
        rel[0] * self.normal[0] + rel[1] * self.normal[1] + rel[2] * self.normal[2]
    }
}

// ---------------------------------------------------------------------------
// PySimConfig
// ---------------------------------------------------------------------------

/// Top-level simulation configuration.
///
/// Passed to `PyPhysicsWorld::new` to configure global simulation parameters.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PySimConfig {
    /// Gravity vector `[gx, gy, gz]`.
    pub gravity: [f64; 3],
    /// Number of constraint solver iterations per step.
    pub solver_iterations: u32,
    /// Linear velocity threshold for sleep.
    pub linear_sleep_threshold: f64,
    /// Angular velocity threshold for sleep.
    pub angular_sleep_threshold: f64,
    /// Time (in seconds) a body must be below thresholds before sleeping.
    pub time_before_sleep: f64,
    /// Enable continuous collision detection.
    pub ccd_enabled: bool,
    /// Coefficient of restitution mixing method: "average", "min", or "max".
    pub restitution_mixing: String,
    /// Friction mixing method: "average", "min", or "max".
    pub friction_mixing: String,
    /// Maximum allowed penetration depth before a contact is ignored.
    pub max_penetration: f64,
    /// Baumgarte stabilization factor (0..1, typical 0.1–0.3).
    pub baumgarte_factor: f64,
    /// Whether sleeping is enabled globally.
    pub sleep_enabled: bool,
    /// Maximum substeps for CCD.
    pub ccd_max_substeps: u32,
}

impl PySimConfig {
    /// Standard Earth-gravity configuration.
    pub fn earth_gravity() -> Self {
        Self {
            gravity: [0.0, -9.81, 0.0],
            ..Self::default()
        }
    }

    /// Zero-gravity (space) configuration.
    pub fn zero_gravity() -> Self {
        Self {
            gravity: [0.0, 0.0, 0.0],
            ..Self::default()
        }
    }

    /// Moon-gravity configuration (~1/6 of Earth).
    pub fn moon_gravity() -> Self {
        Self {
            gravity: [0.0, -1.62, 0.0],
            ..Self::default()
        }
    }
}

impl Default for PySimConfig {
    fn default() -> Self {
        Self {
            gravity: [0.0, -9.81, 0.0],
            solver_iterations: 8,
            linear_sleep_threshold: 0.01,
            angular_sleep_threshold: 0.01,
            time_before_sleep: 0.5,
            ccd_enabled: true,
            restitution_mixing: "average".to_string(),
            friction_mixing: "average".to_string(),
            max_penetration: 0.001,
            baumgarte_factor: 0.2,
            sleep_enabled: true,
            ccd_max_substeps: 10,
        }
    }
}

// ---------------------------------------------------------------------------
// Legacy types kept for backward compatibility
// ---------------------------------------------------------------------------

/// Description for creating a rigid body from Python (legacy).
///
/// Prefer `PyRigidBodyConfig` for new code.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PyRigidBodyDesc {
    /// Mass in kilograms.
    pub mass: f64,
    /// Initial position.
    pub position: PyVec3,
    /// Whether this body is static (immovable).
    pub is_static: bool,
}

/// Description for creating a collider from Python (legacy).
///
/// Prefer `PyColliderShape` for new code.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PyColliderDesc {
    /// Shape type name (e.g. "sphere", "box").
    pub shape_type: String,
    /// Half-extents for box shapes.
    pub half_extents: Option<PyVec3>,
    /// Radius for sphere shapes.
    pub radius: Option<f64>,
    /// Friction coefficient.
    pub friction: f64,
    /// Restitution (bounciness) coefficient.
    pub restitution: f64,
}

impl PyColliderDesc {
    /// Create a sphere collider description with default material properties.
    pub fn sphere(radius: f64) -> Self {
        Self {
            shape_type: "sphere".to_string(),
            half_extents: None,
            radius: Some(radius),
            friction: 0.5,
            restitution: 0.3,
        }
    }

    /// Create a box collider description with default material properties.
    pub fn box_shape(half_extents: PyVec3) -> Self {
        Self {
            shape_type: "box".to_string(),
            half_extents: Some(half_extents),
            radius: None,
            friction: 0.5,
            restitution: 0.3,
        }
    }
}

/// Material properties for Python interop.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PyMaterial {
    /// Material name.
    pub name: String,
    /// Density in kg/m^3.
    pub density: f64,
    /// Friction coefficient.
    pub friction: f64,
    /// Restitution coefficient.
    pub restitution: f64,
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn test_pyvec3_roundtrip() {
        let original = PyVec3::new(1.0, 2.0, 3.0);
        let v3: Vec3 = original.into();
        let back = PyVec3::from(v3);
        assert_eq!(original, back);
    }

    #[test]
    fn test_pytransform_conversion() {
        let t = Transform::default();
        let py_t = PyTransform::from(t);
        assert!((py_t.position.x).abs() < 1e-10);
        assert!((py_t.rotation[3] - 1.0).abs() < 1e-10); // w=1 for identity

        let back: Transform = py_t.into();
        assert!(back.position.norm() < 1e-10);
    }

    #[test]
    fn test_py_vec3_arithmetic() {
        let a = PyVec3::new(1.0, 2.0, 3.0);
        let b = PyVec3::new(4.0, 5.0, 6.0);

        // add
        let sum = a.add(&b);
        assert!((sum.x - 5.0).abs() < 1e-10);
        assert!((sum.y - 7.0).abs() < 1e-10);
        assert!((sum.z - 9.0).abs() < 1e-10);

        // sub
        let diff = b.sub(&a);
        assert!((diff.x - 3.0).abs() < 1e-10);
        assert!((diff.y - 3.0).abs() < 1e-10);
        assert!((diff.z - 3.0).abs() < 1e-10);

        // scale
        let scaled = a.scale(2.0);
        assert!((scaled.x - 2.0).abs() < 1e-10);
        assert!((scaled.y - 4.0).abs() < 1e-10);
        assert!((scaled.z - 6.0).abs() < 1e-10);
    }

    #[test]
    fn test_py_transform_compose() {
        let t1 = PyTransform {
            position: PyVec3::new(1.0, 0.0, 0.0),
            rotation: [0.0, 0.0, 0.0, 1.0],
        };
        let t2 = PyTransform {
            position: PyVec3::new(2.0, 0.0, 0.0),
            rotation: [0.0, 0.0, 0.0, 1.0],
        };
        let composed = t1.compose(&t2);
        assert!((composed.position.x - 3.0).abs() < 1e-10);
        assert!((composed.position.y).abs() < 1e-10);
        assert!((composed.position.z).abs() < 1e-10);
        // identity * identity = identity quaternion
        assert!((composed.rotation[3] - 1.0).abs() < 1e-10);
        assert!((composed.rotation[0]).abs() < 1e-10);
    }

    #[test]
    fn test_py_collider_desc_sphere() {
        let desc = PyColliderDesc::sphere(0.5);
        assert_eq!(desc.shape_type, "sphere");
        assert!((desc.radius.unwrap() - 0.5).abs() < 1e-10);
        assert!(desc.half_extents.is_none());

        let json = serde_json::to_string(&desc).expect("serialize");
        assert!(json.contains("0.5"));
        assert!(json.contains("sphere"));
    }

    #[test]
    fn test_py_json_roundtrip() {
        let original = PyVec3::new(1.5, -2.3, 0.7);
        let json = serde_json::to_string(&original).expect("serialize PyVec3");
        let restored: PyVec3 = serde_json::from_str(&json).expect("deserialize PyVec3");
        assert!((restored.x - original.x).abs() < 1e-10);
        assert!((restored.y - original.y).abs() < 1e-10);
        assert!((restored.z - original.z).abs() < 1e-10);
    }

    #[test]
    fn test_collider_shape_sphere_volume() {
        let s = PyColliderShape::sphere(1.0);
        let vol = s.approximate_volume();
        let expected = (4.0 / 3.0) * std::f64::consts::PI;
        assert!((vol - expected).abs() < 1e-10);
    }

    #[test]
    fn test_collider_shape_box_volume() {
        let b = PyColliderShape::box_shape(1.0, 2.0, 3.0);
        let vol = b.approximate_volume();
        // 8 * 1 * 2 * 3 = 48
        assert!((vol - 48.0).abs() < 1e-10);
    }

    #[test]
    fn test_collider_shape_plane_is_infinite() {
        let p = PyColliderShape::plane([0.0, 1.0, 0.0], 0.0);
        assert!(p.is_infinite());
    }

    #[test]
    fn test_rigid_body_config_inverse_mass() {
        let cfg = PyRigidBodyConfig::dynamic(2.0, [0.0; 3]);
        assert!((cfg.inverse_mass() - 0.5).abs() < 1e-10);

        let static_cfg = PyRigidBodyConfig::static_body([0.0; 3]);
        assert!((static_cfg.inverse_mass()).abs() < 1e-10);
    }

    #[test]
    fn test_rigid_body_config_builder() {
        let cfg = PyRigidBodyConfig::dynamic(5.0, [1.0, 2.0, 3.0])
            .with_friction(0.8)
            .with_restitution(0.1)
            .with_tag("test_body")
            .with_shape(PyColliderShape::sphere(0.5));

        assert!((cfg.friction - 0.8).abs() < 1e-10);
        assert!((cfg.restitution - 0.1).abs() < 1e-10);
        assert_eq!(cfg.tag.as_deref(), Some("test_body"));
        assert_eq!(cfg.shapes.len(), 1);
    }

    #[test]
    fn test_sim_config_default() {
        let cfg = PySimConfig::default();
        assert!((cfg.gravity[1] + 9.81).abs() < 1e-10);
        assert_eq!(cfg.solver_iterations, 8);
    }

    #[test]
    fn test_sim_config_moon_gravity() {
        let cfg = PySimConfig::moon_gravity();
        assert!((cfg.gravity[1] + 1.62).abs() < 1e-10);
    }

    #[test]
    fn test_contact_result_is_colliding() {
        let contact = PyContactResult::new(0, 1, [0.0, 0.0, 0.0], [0.0, 1.0, 0.0], 0.05);
        assert!(contact.is_colliding());

        let no_contact = PyContactResult::new(0, 1, [0.0, 0.0, 0.0], [0.0, 1.0, 0.0], -0.01);
        assert!(!no_contact.is_colliding());
    }

    #[test]
    fn test_pyvec3_dot_cross() {
        let a = PyVec3::new(1.0, 0.0, 0.0);
        let b = PyVec3::new(0.0, 1.0, 0.0);
        assert!((a.dot(&b)).abs() < 1e-10);

        let c = a.cross(&b);
        // (1,0,0) x (0,1,0) = (0,0,1)
        assert!(c.x.abs() < 1e-10);
        assert!(c.y.abs() < 1e-10);
        assert!((c.z - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_pyvec3_normalize() {
        let v = PyVec3::new(3.0, 0.0, 4.0);
        let n = v.normalized();
        assert!((n.length() - 1.0).abs() < 1e-10);

        let zero = PyVec3::zero();
        let nz = zero.normalized();
        assert!(nz.length() < 1e-10);
    }

    #[test]
    fn test_pyaabb_contains() {
        let aabb = PyAabb::new(PyVec3::new(-1.0, -1.0, -1.0), PyVec3::new(1.0, 1.0, 1.0));
        assert!(aabb.contains_point(PyVec3::new(0.0, 0.0, 0.0)));
        assert!(!aabb.contains_point(PyVec3::new(2.0, 0.0, 0.0)));
    }

    #[test]
    fn test_pyaabb_intersects() {
        let a = PyAabb::new(PyVec3::new(0.0, 0.0, 0.0), PyVec3::new(2.0, 2.0, 2.0));
        let b = PyAabb::new(PyVec3::new(1.0, 1.0, 1.0), PyVec3::new(3.0, 3.0, 3.0));
        let c = PyAabb::new(PyVec3::new(5.0, 5.0, 5.0), PyVec3::new(6.0, 6.0, 6.0));
        assert!(a.intersects(&b));
        assert!(!a.intersects(&c));
    }

    #[test]
    fn test_rigid_body_config_json_roundtrip() {
        let cfg = PyRigidBodyConfig::dynamic(3.0, [1.0, 2.0, 3.0])
            .with_shape(PyColliderShape::sphere(0.5))
            .with_friction(0.7);
        let json = serde_json::to_string(&cfg).expect("serialize");
        let restored: PyRigidBodyConfig = serde_json::from_str(&json).expect("deserialize");
        assert!((restored.mass - 3.0).abs() < 1e-10);
        assert_eq!(restored.shapes.len(), 1);
        assert!((restored.friction - 0.7).abs() < 1e-10);
    }

    #[test]
    fn test_contact_separating_velocity() {
        let contact = PyContactResult::new(0, 1, [0.0; 3], [0.0, 1.0, 0.0], 0.01);
        // Bodies moving apart: vel_a = (0,1,0), vel_b = (0,-1,0)
        let sv = contact.separating_velocity([0.0, 1.0, 0.0], [0.0, -1.0, 0.0]);
        // relative = (0,2,0), dot with normal (0,1,0) = 2
        assert!((sv - 2.0).abs() < 1e-10);
    }

    #[test]
    fn test_collider_shape_capsule_volume() {
        let c = PyColliderShape::capsule(1.0, 2.0);
        let vol = c.approximate_volume();
        let expected = std::f64::consts::PI * 1.0 * 1.0 * (4.0 + (4.0 / 3.0) * 1.0);
        assert!((vol - expected).abs() < 1e-10);
    }

    #[test]
    fn test_sim_config_zero_gravity() {
        let cfg = PySimConfig::zero_gravity();
        for &g in &cfg.gravity {
            assert!(g.abs() < 1e-10);
        }
    }

    #[test]
    fn test_collider_shape_serde_roundtrip() {
        let shape = PyColliderShape::box_shape(0.5, 1.0, 1.5);
        let json = serde_json::to_string(&shape).expect("serialize shape");
        let back: PyColliderShape = serde_json::from_str(&json).expect("deserialize shape");
        assert_eq!(shape, back);
    }
}