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
//! The collide module provides the Collide trait for objects that can collide along with several
//! implementations for various types.

use core::ops::Range;

use crate::boxes::{AABox, RBox};
use crate::math::Vec2;
use spine::skeleton::srt::SRT;

// For information on the SAT, see: http://www.dyn4j.org/2010/01/sat/.

/// A trait for objects that can collide with other objects.
pub trait Collide<Rhs: ?Sized = Self> {
    fn collides(&self, other: &Rhs) -> bool;
}

/// A trait for common objects to be collidable with other common objects.
pub trait Collidable: Collide<RBox> + Collide<SRT> + Collide<AABox> + Collide<Vec2> {}

fn left_under(v1: Vec2, v2: Vec2) -> bool {
    v1.x() < v2.x() && v1.y() < v2.y()
}

#[derive(Debug)]
struct Projection {
    min: f32,
    max: f32,
}

impl Collide for Projection {
    fn collides(&self, other: &Self) -> bool {
        self.max >= other.min && self.min <= other.max
    }
}

fn project(rbox: &RBox, axis: Vec2) -> Projection {
    // the vertices of rbox without rbox.pos
    let vertices = [
        rbox.pos + rbox.v1,
        rbox.pos + rbox.v2,
        rbox.pos + rbox.v1 + rbox.v2,
    ];
    // project each vertex onto axis
    let p = axis.dot(rbox.pos);
    let mut proj = Projection { min: p, max: p };
    for &vertex in vertices.iter() {
        let p = axis.dot(vertex);
        if p < proj.min {
            proj.min = p;
        } else if p > proj.max {
            proj.max = p;
        }
    }
    proj
}

/// Calculate the bound in a line segment that collides an AABox projected onto an axis.
/// `bound` is a tuple of the start and ending point of the AABB.
/// `pos` is a component of the position vector of the line segment.
/// `direction` is a component of the direction vector of the line segment.
fn calculate_aabox_rbox_component_bounds(
    bound: Range<f32>,
    pos: f32,
    direction: f32,
) -> (f32, f32) {
    if direction == 0.0 {
        return (0.0, 1.0);
    }
    // get bounds of s by transforming "g(s) = pos + s * direction"
    // and applying the inequality g(s) >= bound.start and g(s) <= bound.end
    let (s1, s2) = (
        (bound.start - pos) / direction,
        (bound.end - pos) / direction,
    );
    // if direction is negative, you have to switch the values
    if direction > 0.0 {
        (s1, s2)
    } else {
        (s2, s1)
    }
}

/// Test for collision between an AABox and an edge of a rbox
fn collide_aabox_rbox_segment(
    xbound: Range<f32>,
    ybound: Range<f32>,
    pos: Vec2,
    direction: Vec2,
) -> bool {
    let sbound1 = calculate_aabox_rbox_component_bounds(xbound, pos.x(), direction.x());
    if sbound1.0 > sbound1.1 {
        return false;
    }
    let sbound2 = calculate_aabox_rbox_component_bounds(ybound, pos.y(), direction.y());
    if sbound2.0 > sbound2.1 {
        return false;
    }
    let (sbound1, sbound2) = (sbound1.0..sbound1.1, sbound2.0..sbound2.1);

    sbound1.end >= sbound2.start
        && sbound1.start <= sbound2.end
        && sbound1.end >= 0.0
        && sbound2.end >= 0.0
        && sbound1.start <= 1.0
        && sbound2.start <= 1.0
}

impl Collide for Vec2 {
    fn collides(&self, other: &Self) -> bool {
        self == other
    }
}

impl Collide<AABox> for Vec2 {
    fn collides(&self, other: &AABox) -> bool {
        other.collides(self)
    }
}

impl Collide<RBox> for Vec2 {
    fn collides(&self, other: &RBox) -> bool {
        other.collides(self)
    }
}

impl Collide<SRT> for Vec2 {
    fn collides(&self, other: &SRT) -> bool {
        other.collides(self)
    }
}

impl Collide<Vec2> for AABox {
    fn collides(&self, other: &Vec2) -> bool {
        left_under(self.pos, *other) && left_under(*other, self.pos + self.size)
    }
}

impl Collide<RBox> for AABox {
    fn collides(&self, other: &RBox) -> bool {
        other.collides(self)
    }
}

impl Collide<SRT> for AABox {
    fn collides(&self, other: &SRT) -> bool {
        other.collides(self)
    }
}

impl Collide for AABox {
    fn collides(&self, other: &Self) -> bool {
        left_under(self.pos, other.pos + other.size) && left_under(other.pos, self.pos + self.size)
    }
}

impl Collide<Vec2> for RBox {
    fn collides(&self, other: &Vec2) -> bool {
        let v1_proj = project(self, self.v1);
        let p1 = other.dot(self.v1);
        let v2_proj = project(self, self.v2);
        let p2 = other.dot(self.v2);
        v1_proj.min <= p1 && v1_proj.max >= p1 && v2_proj.min <= p2 && v2_proj.max >= p2
    }
}

impl Collide<Vec2> for SRT {
    fn collides(&self, other: &Vec2) -> bool {
        let rbox: RBox = self.into();
        rbox.collides(other)
    }
}

impl Collide<AABox> for SRT {
    fn collides(&self, other: &AABox) -> bool {
        let rbox: RBox = self.into();
        rbox.collides(other)
    }
}

impl Collide<RBox> for SRT {
    fn collides(&self, other: &RBox) -> bool {
        let rbox: RBox = self.into();
        rbox.collides(other)
    }
}

impl Collide for SRT {
    fn collides(&self, other: &Self) -> bool {
        let (rbox1, rbox2): (RBox, RBox) = (self.into(), other.into());
        rbox1.collides(&rbox2)
    }
}

impl Collide<AABox> for RBox {
    fn collides(&self, other: &AABox) -> bool {
        let xbound = other.pos.x()..other.pos.x() + other.size.x();
        let ybound = other.pos.y()..other.pos.y() + other.size.y();
        collide_aabox_rbox_segment(xbound.clone(), ybound.clone(), self.pos, self.v1)
            || collide_aabox_rbox_segment(xbound.clone(), ybound.clone(), self.pos, self.v2)
            || collide_aabox_rbox_segment(
                xbound.clone(),
                ybound.clone(),
                self.pos + self.v1,
                self.v2,
            )
            || collide_aabox_rbox_segment(xbound, ybound, self.pos + self.v2, self.v1)
    }
}

impl Collide<SRT> for RBox {
    fn collides(&self, other: &SRT) -> bool {
        let rbox: RBox = other.into();
        rbox.collides(self)
    }
}

impl Collide for RBox {
    fn collides(&self, other: &Self) -> bool {
        // using the SAT
        // TODO: optimization: remove duplicate axes
        let axes = [self.v1, self.v2, other.v1, other.v2];
        axes.iter()
            .all(|axis| project(self, *axis).collides(&project(other, *axis)))
    }
}

impl<S, T: Collide<S>> Collide<S> for [T] {
    fn collides(&self, other: &S) -> bool {
        self.iter().any(|x| x.collides(other))
    }
}

impl<S, T: Collide<S>> Collide<[T]> for S {
    fn collides(&self, other: &[T]) -> bool {
        other.collides(self)
    }
}

impl Collidable for RBox {}
impl Collidable for SRT {}
impl Collidable for AABox {}
impl Collidable for Vec2 {}