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
use bevy_math::primitives::{
    BoxedPolyline3d, Capsule3d, Cone, Cuboid, Cylinder, Line3d, Plane3d, Polyline3d, Segment3d,
    Sphere,
};
use parry::shape::SharedShape;

use crate::{AdjustPrecision, Collider, IntoCollider, Quaternion, Vector};

impl IntoCollider<Collider> for Sphere {
    fn collider(&self) -> Collider {
        Collider::sphere(self.radius.adjust_precision())
    }
}

impl IntoCollider<Collider> for Plane3d {
    fn collider(&self) -> Collider {
        let half_size = 10_000.0;
        let rotation = Quaternion::from_rotation_arc(Vector::Y, self.normal.adjust_precision());
        let vertices = vec![
            rotation * Vector::new(half_size, 0.0, -half_size),
            rotation * Vector::new(-half_size, 0.0, -half_size),
            rotation * Vector::new(-half_size, 0.0, half_size),
            rotation * Vector::new(half_size, 0.0, half_size),
        ];

        Collider::trimesh(vertices, vec![[0, 1, 2], [1, 2, 0]])
    }
}

impl IntoCollider<Collider> for Line3d {
    fn collider(&self) -> Collider {
        let vec = self.direction.adjust_precision() * 10_000.0;
        Collider::segment(-vec, vec)
    }
}

impl IntoCollider<Collider> for Segment3d {
    fn collider(&self) -> Collider {
        let (point1, point2) = (self.point1(), self.point2());
        Collider::segment(point1.adjust_precision(), point2.adjust_precision())
    }
}

impl<const N: usize> IntoCollider<Collider> for Polyline3d<N> {
    fn collider(&self) -> Collider {
        let vertices = self.vertices.map(|v| v.adjust_precision());
        Collider::polyline(vertices.to_vec(), None)
    }
}

impl IntoCollider<Collider> for BoxedPolyline3d {
    fn collider(&self) -> Collider {
        let vertices = self.vertices.iter().map(|v| v.adjust_precision());
        Collider::polyline(vertices.collect(), None)
    }
}

impl IntoCollider<Collider> for Cuboid {
    fn collider(&self) -> Collider {
        let [hx, hy, hz] = self.half_size.adjust_precision().to_array();
        Collider::from(SharedShape::cuboid(hx, hy, hz))
    }
}

impl IntoCollider<Collider> for Cylinder {
    fn collider(&self) -> Collider {
        Collider::from(SharedShape::cylinder(
            self.half_height.adjust_precision(),
            self.radius.adjust_precision(),
        ))
    }
}

impl IntoCollider<Collider> for Capsule3d {
    fn collider(&self) -> Collider {
        Collider::capsule(
            self.radius.adjust_precision(),
            2.0 * self.half_length.adjust_precision(),
        )
    }
}

impl IntoCollider<Collider> for Cone {
    fn collider(&self) -> Collider {
        Collider::cone(
            self.radius.adjust_precision(),
            self.height.adjust_precision(),
        )
    }
}

// TODO: ConicalFrustum
// TODO: Torus