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
use sdl2::rect::Rect;
use sprite::SpriteRectangle;
use std::mem;
use std::ops::{BitAnd, BitOr};
use vector::PositionChange;

/// Checks if a rectangle contains another rectangle
pub fn rect_contains_rect(parent: Rect, child: Rect) -> bool {
    let x_min = child.x();
    let x_max = x_min + child.width() as i32;
    let y_min = child.y();
    let y_max = y_min + child.height() as i32;

    let check_xmin = x_min >= parent.x() && x_min <= parent.x() + parent.width() as i32;
    let check_xmax = x_max >= parent.x() && x_max <= parent.x() + parent.width() as i32;
    let check_ymin = y_min >= parent.y() && y_min <= parent.y() + parent.height() as i32;
    let check_ymax = y_max >= parent.y() && y_max <= parent.y() + parent.height() as i32;

    check_xmin && check_xmax && check_ymin && check_ymax
}

/// Returns the center point of a rectangle as a tuple of decimals
pub fn center_point(rect: &Rect) -> (f64, f64) {
    ((rect.x() as f64) + 0.5 * (rect.width() as f64),
     (rect.y() as f64) + 0.5 * (rect.height() as f64))
}

#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum CollisionSide {
    Left = 0b1000,
    Right = 0b0100,
    Top = 0b0010,
    Bottom = 0b0001,
}

impl CollisionSide {
    /// Reverses a collision side
    pub fn reverse(side: CollisionSide) -> u8 {
        CollisionSide::reverse_u8(side as u8)
    }

    /// Reverses a collision side byte
    pub fn reverse_u8(side: u8) -> u8 {
        let mut side = side;

        if side & 0b0011 != 0b0011 && side & 0b0011 != 0b0000 {
            side ^= 0b0011;
        }

        if side & 0b1100 != 0b1100 && side & 0b1100 != 0b0000 {
            side ^= 0b1100;
        }

        side
    }

    pub fn print(self) {
        print_collision_side_u8(self as u8);
    }
}

impl BitAnd<CollisionSide> for CollisionSide {
    type Output = u8;

    fn bitand(self, other: CollisionSide) -> u8 {
        (self as u8) & (other as u8)
    }
}

impl BitAnd<u8> for CollisionSide {
    type Output = u8;

    fn bitand(self, other: u8) -> u8 {
        (self as u8) & other
    }
}

impl BitAnd<CollisionSide> for u8 {
    type Output = u8;

    fn bitand(self, other: CollisionSide) -> u8 {
        self & (other as u8)
    }
}

impl BitOr<CollisionSide> for CollisionSide {
    type Output = u8;

    fn bitor(self, other: CollisionSide) -> u8 {
        (self as u8) | (other as u8)
    }
}

impl BitOr<u8> for CollisionSide {
    type Output = u8;

    fn bitor(self, other: u8) -> u8 {
        (self as u8) | other
    }
}

impl BitOr<CollisionSide> for u8 {
    type Output = u8;

    fn bitor(self, other: CollisionSide) -> u8 {
        self | (other as u8)
    }
}

impl PartialEq<u8> for CollisionSide {
    fn eq(&self, other: &u8) -> bool {
        (*self as u8) == *other
    }
}

impl PartialEq<CollisionSide> for u8 {
    fn eq(&self, other: &CollisionSide) -> bool {
        *self == (*other as u8)
    }
}

impl From<u8> for CollisionSide {
    fn from(side: u8) -> CollisionSide {
        assert!(side == CollisionSide::Left || side == CollisionSide::Right ||
                side == CollisionSide::Bottom || side == CollisionSide::Top);
        unsafe { mem::transmute(side) }
    }
}

/// Prints the collision side as a byte
pub fn print_collision_side_u8(direction: u8) {
    print!("Collisions: (");
    let mut names = Vec::with_capacity(4);
    if direction & CollisionSide::Left != 0 {
        names.push("Left");
    }
    if direction & CollisionSide::Right != 0 {
        names.push("Right");
    }
    if direction & CollisionSide::Top != 0 {
        names.push("Top");
    }
    if direction & CollisionSide::Bottom != 0 {
        names.push("Bottom");
    }

    let names_str = names.join(",");
    print!("{}", names_str);
    print!(")");
}

/// Checks collisions for different objects
pub trait Collision<T> {
    fn collides_with(&self, other: T) -> Option<CollisionSide>;
}

impl Collision<Rect> for Rect {
    fn collides_with(&self, other: Rect) -> Option<CollisionSide> {
        let w = 0.5 * (self.width() + other.width()) as f64;
        let h = 0.5 * (self.height() + other.height()) as f64;
        let dx = center_point(self).0 - center_point(&other).0;
        let dy = center_point(self).1 - center_point(&other).1;

        if dx.abs() <= w && dy.abs() <= h {
            let wy = w * dy;
            let hx = h * dx;

            if wy > hx {
                if wy > -hx {
                    return Some(CollisionSide::Top);
                } else {
                    return Some(CollisionSide::Right);
                }
            } else {
                if wy > -hx {
                    return Some(CollisionSide::Left);
                } else {
                    return Some(CollisionSide::Bottom);
                }
            }
        }

        None
    }
}

impl Collision<SpriteRectangle> for Rect {
    fn collides_with(&self, other: SpriteRectangle) -> Option<CollisionSide> {
        if let Some(rect) = other.to_sdl() {
            return self.collides_with(rect);
        }

        None
    }
}

impl Collision<Rect> for SpriteRectangle {
    fn collides_with(&self, other: Rect) -> Option<CollisionSide> {
        if let Some(rect) = self.to_sdl() {
            return rect.collides_with(other);
        }

        None
    }
}

impl Collision<SpriteRectangle> for SpriteRectangle {
    fn collides_with(&self, other: SpriteRectangle) -> Option<CollisionSide> {
        if let Some(rect) = other.to_sdl() {
            return self.collides_with(rect);
        }

        None
    }
}

#[derive(Clone, PartialEq)]
pub enum BoundingBox {
    Rectangle(SpriteRectangle),
}

impl BoundingBox {
    pub fn apply_change(&mut self, change: &PositionChange) {
        match *self {
            BoundingBox::Rectangle(ref mut rect) => {
                rect.x += change.x;
                rect.y += change.y;
            }
        }
    }
}

impl<'a> Collision<&'a BoundingBox> for BoundingBox {
    fn collides_with(&self, other: &'a BoundingBox) -> Option<CollisionSide> {
        match (self, other) {
            (&BoundingBox::Rectangle(ref rect1), &BoundingBox::Rectangle(ref rect2)) => {
                // TODO(DarinM223): avoid cloning the second rectangle
                rect1.collides_with(rect2.clone())
            }
        }
    }
}

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

    #[test]
    fn test_collision_reverse() {
        let side = 0b1111;
        assert_eq!(CollisionSide::reverse_u8(side), 0b1111);

        let side = 0b1110;
        assert_eq!(CollisionSide::reverse_u8(side), 0b1101);

        let side = 0b0111;
        assert_eq!(CollisionSide::reverse_u8(side), 0b1011);

        let side = 0b0110;
        assert_eq!(CollisionSide::reverse_u8(side), 0b1001);

        let side = 0b0000;
        assert_eq!(CollisionSide::reverse_u8(side), 0b0000);
    }

    #[test]
    fn test_left_right_rect_collision() {
        let left_rect = Rect::new_unwrap(-10, 0, 20, 20);
        let right_rect = Rect::new_unwrap(0, 0, 20, 20);

        assert_eq!(left_rect.collides_with(right_rect),
                   Some(CollisionSide::Right));
        assert_eq!(right_rect.collides_with(left_rect),
                   Some(CollisionSide::Left));
    }

    #[test]
    fn test_up_down_rect_collision() {
        let up_rect = Rect::new_unwrap(0, -20, 20, 20);
        let down_rect = Rect::new_unwrap(0, 0, 20, 20);

        assert_eq!(up_rect.collides_with(down_rect),
                   Some(CollisionSide::Bottom));
        assert_eq!(down_rect.collides_with(up_rect), Some(CollisionSide::Top));
    }
}