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
pub mod shapes;
pub mod distance;
pub mod time_of_impact;

use std::mem;
use common::settings::MAX_MANIFOLD_POINTS;
use common::math::{Vec2, Transform};
use collision::shapes::Shape;

#[repr(u8)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum ContactFeatureType {
    Vertex,
    Face,
}

#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct ContactFeature {
    pub index_a: u8,
    pub index_b: u8,
    pub type_a: ContactFeatureType,
    pub type_b: ContactFeatureType,
}

#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct ContactId(u32);

impl ContactId {
    pub fn feature(&self) -> ContactFeature {
        unsafe { mem::transmute(self.0) }
    }

    pub fn key(&self) -> u32 {
        self.0
    }
}

#[repr(C)]
#[derive(Clone, Debug)]
pub struct ManifoldPoint {
    pub local_point: Vec2,
    pub normal_impulse: f32,
    pub tangent_impulse: f32,
    pub id: ContactId,
}

#[repr(C)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum ManifoldType {
    Circles,
    FaceA,
    FaceB,
}

#[repr(C)]
#[derive(Debug)]
pub struct Manifold {
    pub points: [ManifoldPoint; MAX_MANIFOLD_POINTS],
    pub local_normal: Vec2,
    pub local_point: Vec2,
    pub manifold_type: ManifoldType,
    pub count: i32,
}

impl Manifold {
    pub fn world_manifold(&self,
                          xf_a: &Transform,
                          radius_a: f32,
                          xf_b: &Transform,
                          radius_b: f32)
                          -> WorldManifold {
        unsafe {
            let mut w = mem::zeroed();
            ffi::WorldManifold_Initialize(&mut w, self, xf_a, radius_a, xf_b, radius_b);
            w
        }
    }
}

#[repr(C)]
#[derive(Clone, Debug)]
pub struct WorldManifold {
    pub normal: Vec2,
    pub points: [Vec2; MAX_MANIFOLD_POINTS],
    pub separations: [f32; MAX_MANIFOLD_POINTS],
}

#[repr(C)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum PointState {
    Null,
    Add,
    Persist,
    Remove,
}

pub fn get_point_states(m1: &Manifold,
                        m2: &Manifold)
                        -> ([PointState; MAX_MANIFOLD_POINTS],
                            [PointState; MAX_MANIFOLD_POINTS]) {
    unsafe {
        let (mut s1, mut s2) = mem::zeroed();
        ffi::get_point_states(&mut s1, &mut s2, m1, m2);
        (s1, s2)
    }
}

#[repr(C)]
#[derive(Clone, Debug)]
pub struct RayCastInput {
    pub p1: Vec2,
    pub p2: Vec2,
    pub max_fraction: f32,
}

#[repr(C)]
#[derive(Clone, Debug)]
pub struct RayCastOutput {
    pub normal: Vec2,
    pub fraction: f32,
}

#[repr(C)]
#[derive(Clone, Debug)]
pub struct AABB {
    pub lower: Vec2,
    pub upper: Vec2,
}

impl AABB {
    pub fn new() -> AABB {
        AABB {
            lower: Vec2 { x: 0., y: 0. },
            upper: Vec2 { x: 0., y: 0. },
        }
    }
}

pub fn test_overlap<A, B>(shape_a: &A,
                          index_a: i32,
                          xf_a: &Transform,
                          shape_b: &B,
                          index_b: i32,
                          xf_b: &Transform)
                          -> bool
    where A: Shape,
          B: Shape
{
    unsafe {
        ffi::test_overlap(shape_a.base_ptr(),
                          index_a,
                          shape_b.base_ptr(),
                          index_b,
                          xf_a,
                          xf_b)
    }
}

#[doc(hidden)]
pub mod ffi {
    pub use collision::shapes::ffi::Shape;
    use common::math::Transform;
    use common::settings::MAX_MANIFOLD_POINTS;
    use super::{Manifold, WorldManifold, PointState};

    extern "C" {
        pub fn WorldManifold_Initialize(slf: *mut WorldManifold,
                                        manifold: *const Manifold,
                                        xf_a: *const Transform,
                                        radius_a: f32,
                                        xf_b: *const Transform,
                                        radius_b: f32);
        pub fn get_point_states(s1: &mut [PointState; MAX_MANIFOLD_POINTS],
                                s2: &mut [PointState; MAX_MANIFOLD_POINTS],
                                m1: *const Manifold,
                                m2: *const Manifold);
        pub fn test_overlap(shape_a: *const Shape,
                            index_a: i32,
                            shape_b: *const Shape,
                            index_b: i32,
                            xf_a: *const Transform,
                            xf_b: *const Transform)
                            -> bool;
    }
}