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
//! Structures for defining geometry primitives.
//!
//! TODO move hittesting to a separate crate?

use crate::math::*;
use rand;
use std::fmt;

pub struct Interval {
  pub min: f32,
  pub max: f32,
}

impl Interval {
  pub fn new(min: f32, max: f32) -> Interval {
    Interval { min, max }
  }
}

#[derive(Default, Copy, Clone)]
pub struct Circle {
  pub center: V2,
  pub r: f32,
}

#[derive(Default, Copy, Clone)]
pub struct Rect {
  pub min_x: f32,
  pub min_y: f32,
  pub max_x: f32,
  pub max_y: f32,
}

#[derive(Default, Copy, Clone)]
pub struct LineSegment {
  pub a: V2,
  pub b: V2,
  pub normal: V2,
}

impl LineSegment {
  pub fn new(a: V2, b: V2, normal: V2) -> LineSegment {
    LineSegment { a, b, normal }
  }
}

#[derive(Default, Copy, Clone)]
pub struct OverlapResult {
  pub normal: V2,
  pub distance: f32,
}

#[derive(Default, Copy, Clone)]
pub struct SweepResult {
  pub normal: V2,
  pub t: f32,
}

#[derive(Copy, Clone)]
pub enum Shape {
  Point(V2),
  Circle(Circle),
  Rect(Rect),
}

impl Default for Shape {
  fn default() -> Self {
    Shape::Point(V2::ZERO)
  }
}

impl fmt::Display for Shape {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    match self {
      Shape::Point(p) => write!(f, "Point ({}, {})", p.x, p.y),
      Shape::Circle(c) => write!(
        f,
        "Circle (r: {}, center: {}, {})",
        c.r, c.center.x, c.center.y
      ),
      Shape::Rect(r) => write!(
        f,
        "Rect ({}, {}, {}, {})",
        r.min_x, r.max_x, r.min_y, r.max_y
      ),
    }
  }
}

pub struct ProjectionResult {
  pub step: V2,
  pub step_mag: f32,
  pub collision_normal: V2,
}

///////////////////////////////////////////////////////////////////////////////

impl Rect {
  pub fn left_edge(self) -> LineSegment {
    let a: V2 = v2(self.min_x, self.min_y);
    let b: V2 = v2(self.min_x, self.max_y);
    return LineSegment::new(a, b, V2::LEFT);
  }

  pub fn bottom_edge(self) -> LineSegment {
    let a: V2 = v2(self.min_x, self.min_y);
    let b: V2 = v2(self.max_x, self.min_y);
    return LineSegment::new(a, b, V2::DOWN);
  }

  pub fn top_edge(self) -> LineSegment {
    let a: V2 = v2(self.min_x, self.max_y);
    let b: V2 = v2(self.max_x, self.max_y);
    return LineSegment::new(a, b, V2::UP);
  }

  pub fn right_edge(self: Rect) -> LineSegment {
    let a: V2 = v2(self.max_x, self.max_y);
    let b: V2 = v2(self.max_x, self.min_y);
    return LineSegment::new(a, b, V2::RIGHT);
  }

  pub fn w(self) -> f32 {
    self.max_x - self.min_x
  }
  pub fn h(self) -> f32 {
    self.max_y - self.min_y
  }
}
///////////////////////////////////////////////////////////////////////////////

// TODO handle with Option<V2> ?
pub fn get_line_intersection(p0: V2, p1: V2, p2: V2, p3: V2, i: &mut V2) -> bool {
  // shamelessly borrowed from https://stackoverflow.com/a/1968345

  let s1: V2 = p1 - p0;
  let s2: V2 = p3 - p2;

  let s = (-s1.y * (p0.x - p2.x) + s1.x * (p0.y - p2.y)) / (-s2.x * s1.y + s1.x * s2.y);
  let t = (s2.x * (p0.y - p2.y) - s2.y * (p0.x - p2.x)) / (-s2.x * s1.y + s1.x * s2.y);

  if s >= 0.0 && s <= 1.0 && t >= 0.0 && t <= 1.0 {
    // TODO rewrite this to be more idiomatic in Rust
    // Collision detected
    i.x = p0.x + (t * s1.x);
    i.y = p0.y + (t * s1.y);
    return true;
  } else {
    // No collision
    return false;
  }
}

///////////////////////////////////////////////////////////////////////////////

pub fn project_circle_v_segment(result: &mut ProjectionResult, c: Circle, edge: LineSegment) {
  // use the edge's normal to find the nearest point on the circle, this
  // is the earliest possible point of intersection, and therefore the
  // only one we care about.
  let circ_edge_a: V2 = c.center - (edge.normal * c.r);
  let circ_edge_b: V2 = circ_edge_a + result.step;

  let mut intersection: V2 = V2::ZERO;
  let intersection_exists =
    get_line_intersection(edge.a, edge.b, circ_edge_a, circ_edge_b, &mut intersection);
  if intersection_exists {
    let hit_dist: V2 = intersection - circ_edge_a;
    let hit_magnitude = hit_dist.mag();
    if hit_magnitude < result.step_mag {
      result.step_mag = hit_magnitude;
      result.step = (result.step.normalize()) * hit_magnitude;
      result.collision_normal = edge.normal;
    }
  }
}

///////////////////////////////////////////////////////////////////////////////

pub fn test_interval_overlap(a: Interval, b: Interval) -> bool {
  return a.min < b.max && b.min < a.max;
}

///////////////////////////////////////////////////////////////////////////////

pub fn get_interval_overlap(a: Interval, b: Interval) -> f32 {
  // TODO switch to cmp in Rust
  if a.max > b.min {
    return a.max - b.min;
  }
  if b.max > a.min {
    return a.min - b.max;
  }
  return 0.0;
}

///////////////////////////////////////////////////////////////////////////////

pub fn test_point_v_circle(p: V2, c: Circle) -> bool {
  let delta_v: V2 = p - c.center;
  let x_sq = delta_v.x * delta_v.x;
  let y_sq = delta_v.y * delta_v.y;
  let r_sq = c.r * c.r;
  return (x_sq + y_sq) < r_sq;
}

///////////////////////////////////////////////////////////////////////////////

pub fn test_point_v_aabb(p: V2, aabb: Rect) -> bool {
  return p.x > aabb.min_x && p.x < aabb.max_x && p.y > aabb.min_y && p.y < aabb.max_y;
}

///////////////////////////////////////////////////////////////////////////////

pub fn test_aabb_v_aabb(a: Rect, b: Rect) -> bool {
  {
    let interval_a = Interval::new(a.min_y, a.max_y);
    let interval_b = Interval::new(b.min_y, b.max_y);
    if !test_interval_overlap(interval_a, interval_b) {
      return false;
    }
  }
  {
    let interval_a = Interval::new(a.min_x, a.max_x);
    let interval_b = Interval::new(b.min_x, b.max_x);
    if !test_interval_overlap(interval_a, interval_b) {
      return false;
    }
  }
  return true;
}

///////////////////////////////////////////////////////////////////////////////

pub fn test_overlap(a: Shape, b: Shape) -> bool {
  // sort the shapes to reduce the test s
  // if a > b
  // {
  //   let c = b;
  //   b = a;
  //   a = c;
  // }

  match (a, b) {
    (Shape::Point(a), Shape::Point(b)) => V2::nearly(a, b),
    (Shape::Point(a), Shape::Circle(b)) | (Shape::Circle(b), Shape::Point(a)) => {
      test_point_v_circle(a, b)
    }
    (Shape::Point(a), Shape::Rect(b)) | (Shape::Rect(b), Shape::Point(a)) => {
      test_point_v_aabb(a, b)
    }
    (Shape::Circle(a), Shape::Circle(b)) => {
      // merge one circle into the other so we can do a simple point test
      let expanded_c = Circle {
        center: b.center,
        r: a.r + b.r,
      };
      test_point_v_circle(a.center, expanded_c)
    }

    (Shape::Circle(a), Shape::Rect(b)) | (Shape::Rect(b), Shape::Circle(a)) => {
      let corner: V2 = v2(b.min_x, b.min_y);
      if test_point_v_circle(corner, a) {
        return true;
      }
      let corner = v2(b.min_x, b.max_y);
      if test_point_v_circle(corner, a) {
        return true;
      }
      let corner = v2(b.max_x, b.max_y);
      if test_point_v_circle(corner, a) {
        return true;
      }
      let corner = v2(b.max_x, b.min_y);
      if test_point_v_circle(corner, a) {
        return true;
      }
      let aabb_y = Rect {
        min_x: b.min_x,
        max_x: b.max_x,
        min_y: b.min_y - a.r,
        max_y: b.max_y + a.r,
      };
      if test_point_v_aabb(a.center, aabb_y) {
        return true;
      }
      let aabb_x = Rect {
        min_x: b.min_x - a.r,
        max_x: b.max_x + a.r,
        min_y: b.min_y,
        max_y: b.max_y,
      };
      if test_point_v_aabb(a.center, aabb_x) {
        return true;
      }
      return false;
    }

    (Shape::Rect(a), Shape::Rect(b)) => test_aabb_v_aabb(a, b),
  }
}

///////////////////////////////////////////////////////////////////////////////

pub fn get_overlap_point_v_aabb(p: V2, aabb: Rect) -> OverlapResult {
  // start with the left edge
  let mut min_axis: V2 = V2::LEFT;
  let mut min_mag = p.x - aabb.min_x;
  {
    let r_mag = aabb.max_x - p.x;
    if r_mag < min_mag {
      min_axis = V2::RIGHT;
      min_mag = r_mag;
    }
  }
  {
    let u_mag = aabb.max_y - p.y;
    if u_mag < min_mag {
      min_axis = V2::UP;
      min_mag = u_mag;
    }
  }
  {
    let d_mag = p.y - aabb.min_y;
    if d_mag < min_mag {
      min_axis = V2::DOWN;
      min_mag = d_mag;
    }
  }
  OverlapResult {
    normal: min_axis,
    distance: min_mag,
  }
}

///////////////////////////////////////////////////////////////////////////////

pub fn get_overlap_point_v_circle(p: V2, c: Circle) -> OverlapResult {
  let dist: V2 = p - c.center;
  let dist_mag = V2::mag(dist);
  let normal: V2 = V2::normalize(dist);
  OverlapResult {
    normal,
    distance: c.r - dist_mag,
  }
}

///////////////////////////////////////////////////////////////////////////////

pub fn get_overlap(a: Shape, b: Shape) -> OverlapResult {
  let mut result = OverlapResult {
    normal: V2::ZERO,
    distance: f32::MAX,
  };

  // TODO not possible in Rust?
  // if a.type == SHAPE_NONE || b.type == SHAPE_NONE
  // {
  //   return result;
  // }

  match (a, b) {
    (Shape::Point(_), Shape::Point(_)) => result,
    (Shape::Point(a), Shape::Rect(b)) => get_overlap_point_v_aabb(a, b),
    (Shape::Point(a), Shape::Circle(b)) => get_overlap_point_v_circle(a, b),

    (Shape::Circle(a), Shape::Circle(b)) => {
      let p: V2 = a.center;
      let c: Circle = Circle {
        center: b.center,
        r: a.r + b.r,
      };
      return get_overlap_point_v_circle(p, c);
    }
    (Shape::Circle(a), Shape::Rect(b)) => {
      if a.center.x > b.max_x && a.center.y > b.max_y {
        let corner: V2 = v2(b.max_x, b.max_y);
        let corner_c = Circle {
          center: corner,
          r: a.r,
        };
        return get_overlap_point_v_circle(a.center, corner_c);
      }
      if a.center.x < b.min_x && a.center.y > b.max_y {
        let corner: V2 = V2 {
          x: b.min_x,
          y: b.max_y,
        };
        let corner_c = Circle {
          center: corner,
          r: a.r,
        };
        return get_overlap_point_v_circle(a.center, corner_c);
      }
      if a.center.x > b.max_x && a.center.y < b.min_y {
        let corner: V2 = v2(b.max_x, b.min_y);
        let corner_c: Circle = Circle {
          center: corner,
          r: a.r,
        };
        return get_overlap_point_v_circle(a.center, corner_c);
      }
      if a.center.x < b.min_x && a.center.y < b.min_y {
        let corner: V2 = v2(b.min_x, b.min_y);
        let corner_c: Circle = Circle {
          center: corner,
          r: a.r,
        };
        return get_overlap_point_v_circle(a.center, corner_c);
      }
      let aabb_expanded: Rect = Rect {
        min_x: b.min_x - a.r,
        max_x: b.max_x + a.r,
        min_y: b.min_y - a.r,
        max_y: b.max_y + a.r,
      };
      return get_overlap_point_v_aabb(a.center, aabb_expanded);
    }

    (Shape::Rect(a), Shape::Rect(b)) => {
      let mut min_distance = f32::MAX;
      let mut min_normal: V2 = V2::ZERO;
      {
        let distance = a.max_x - b.min_x;
        if distance < min_distance {
          min_distance = distance;
          min_normal = V2::LEFT;
        }
      }
      {
        let distance = b.max_x - a.min_x;
        if distance < min_distance {
          min_distance = distance;
          min_normal = V2::RIGHT;
        }
      }
      {
        let distance = a.max_y - b.min_y;
        if distance < min_distance {
          min_distance = distance;
          min_normal = V2::DOWN;
        }
      }
      {
        let distance = b.max_y - a.min_y;
        if distance < min_distance {
          min_distance = distance;
          min_normal = V2::UP;
        }
      }
      result.distance = min_distance;
      result.normal = min_normal;
      return result;
    }

    _ => {
      // TODO is there a better way? This could result in an infinite loop for unhandled cases
      // and the compiler doesn't catch it
      result = get_overlap(b, a);
      result.normal = result.normal * -1.0;
      return result;
    }
  }
}

// ----------------------------------------------------------------------------
// sweep point v. edge
//
//   thanks, Wikipedia.

pub fn sweep_point_v_edge(result: &mut SweepResult, step: V2, p: V2, edge: LineSegment) {
  // TODO when doing several of these in series for the same p and step,
  // this calc can be redundant. Perhaps I could prime it as an extra param.
  let p_stepped: V2 = add2(p, step);

  // TODO points in right order?
  let x1 = p.x;
  let y1 = p.y;
  let x2 = p_stepped.x;
  let y2 = p_stepped.y;

  let x3 = edge.a.x;
  let y3 = edge.a.y;
  let x4 = edge.b.x;
  let y4 = edge.b.y;

  // this gets the "time" of intersection along one line with the other
  // t < 0 means the intersection is before the first point and
  // t > 0 means the intersection is after the second (stepped) point
  let t_denominator = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
  if t_denominator == 0.0 {
    // this can happen when the lines are parallel
    return;
  }
  let t_numerator = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
  let t = t_numerator / t_denominator;
  if t <= 0.0 || t >= 1.0 {
    // TODO epsilon?
    return;
  }

  // t only gets us the time of the intersection with respect to the
  // line that the edge falls on. We need to also make sure the
  // intersection falls between the
  let u_numerator = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
  let u_denominator = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
  let u = -u_numerator / u_denominator;

  if u <= 0.0 || u >= 1.0 {
    // TODO epsilon?
    return;
  }

  if t >= 0.0 && t <= 1.0 {
    if t < result.t {
      result.t = t;
      result.normal = edge.normal;
    }
  }
}

// ----------------------------------------------------------------------------
// sweep point v. aabb

pub fn sweep_point_v_aabb(result: &mut SweepResult, step: V2, p: V2, aabb: Rect) {
  if step.x > 0.0 {
    let edge = aabb.left_edge();
    sweep_point_v_edge(result, step, p, edge);
  } else {
    let edge = aabb.right_edge();
    sweep_point_v_edge(result, step, p, edge);
  }
  if step.y > 0.0 {
    let edge = aabb.bottom_edge();
    sweep_point_v_edge(result, step, p, edge);
  } else {
    let edge = aabb.top_edge();
    sweep_point_v_edge(result, step, p, edge);
  }
}

///////////////////////////////////////////////////////////////////////////////

pub fn sweep_point_v_circle(result: &mut SweepResult, step: V2, p: V2, circle: Circle) {
  // shamelessly borrowed from https://stackoverflow.com/a/1084899
  let e: V2 = p; // E is the starting point of the ray,
  let l: V2 = p + step; // L is the end point of the ray,

  let c: V2 = circle.center; // C is the center of sphere you're testing against
  let r = circle.r;

  let d: V2 = l - e; // d = L - E (Direction vector of ray, from start to end)
  let f: V2 = e - c; // f = E - C (Vector from center sphere to ray start)

  let a = V2::dot(d, d);
  let b = 2.0 * V2::dot(f, d);
  let c = V2::dot(f, f) - r * r;

  let discriminant = b * b - 4.0 * a * c;
  if discriminant < NEAR_ZERO {
    // no intersection
    return;
  }

  // ray didn't totally miss sphere,
  // so there is a solution to
  // the equation.
  let discriminant = f32::sqrt(discriminant);

  // either solution may be on or off the ray so need to test both
  // t1 is always the smaller value, because BOTH discriminant and
  // a are nonnegative.
  let t1 = (-b - discriminant) / (2.0 * a);
  let t2 = (-b + discriminant) / (2.0 * a);

  // 6x cases:
  //  ---(-----)-->           ---(-->  )            (   --)->
  // Impale(t1 hit,t2 hit), Poke(t1 hit,t2>1), ExitWound(t1<0, t2 hit),
  //
  //  -> (     )                 (     ) ->         ( --> )
  // FallShort (t1>1,t2>1), Past (t1<0,t2<0), CompletelyInside(t1<0, t2>1)

  // FallShort, Past
  // in these cases, there is no collision to handle, so just move along
  if (t1 > 1.0) || (t1 < 0.0) {
    return;
  }
  // We now know that somewhere along the axis of movement, the two bodies
  // overlap.

  let t = if t1 < t2 { t1 } else { t2 };

  // TODO in the AABB check, I could early out here if t is not less than
  // a previous result
  if t > result.t {
    return;
  }

  // in all other scenarios, we're chosing to project the offending entity
  // back out of collision along its vector of motion (result.step). Along
  // the ray, t1 is always the "first" hit (which may mean moving backward)
  result.t = t;
  let collision_pos: V2 = add2(p, mul2(step, t));
  result.normal = V2::normalize(sub2(collision_pos, circle.center));
  return;
}

// ----------------------------------------------------------------------------
// A continuous collision detector.
//
//  Takes in a step vector and two shapes. The first shape is assumed to be
//  the one in motion. The result object will include a .t value which will
//  be 1.0 if no collision is found along the step, and will be a value
//  between 0 and 1 when a collision is found, indicating the per

pub fn sweep(step: V2, a: Shape, b: Shape) -> SweepResult {
  let mut result = SweepResult {
    t: 1.0,
    normal: V2::ZERO,
  };

  match (a, b) {
    (Shape::Point(_), Shape::Point(_)) => {
      // point shapes can't really collide with each other; they're too precise
      return result;
    }
    (Shape::Point(a), Shape::Rect(b)) => {
      sweep_point_v_aabb(&mut result, step, a, b);
      return result;
    }
    (Shape::Point(a), Shape::Circle(b)) => {
      sweep_point_v_circle(&mut result, step, a, b);
      return result;
    }

    (Shape::Circle(a), Shape::Circle(b)) => {
      let p: V2 = a.center;
      let c: Circle = Circle {
        center: b.center,
        r: a.r + b.r,
      };
      sweep_point_v_circle(&mut result, step, p, c);
      return result;
    }
    (Shape::Circle(a), Shape::Rect(b)) => {
      // TODO I might be able to exclude the "back" corner
      let corners: [V2; 4] = [
        v2(b.max_x, b.max_y),
        v2(b.min_x, b.max_y),
        v2(b.max_x, b.min_y),
        v2(b.min_x, b.min_y),
      ];
      for i in 0..4 {
        let corner: V2 = corners[i];
        let corner_c: Circle = Circle {
          center: corner,
          r: a.r,
        };
        sweep_point_v_circle(&mut result, step, a.center, corner_c);
      }

      // expand aabb on each axis
      {
        let aabb_expanded_x = Rect {
          min_x: b.min_x - a.r,
          max_x: b.max_x + a.r,
          min_y: b.min_y,
          max_y: b.max_y,
        };
        sweep_point_v_aabb(&mut result, step, a.center, aabb_expanded_x);
      }
      {
        let aabb_expanded_y = Rect {
          min_x: b.min_x,
          max_x: b.max_x,
          min_y: b.min_y - a.r,
          max_y: b.max_y + a.r,
        };
        sweep_point_v_aabb(&mut result, step, a.center, aabb_expanded_y);
      }

      return result;
    }

    (Shape::Rect(a), Shape::Rect(b)) => {
      // thanks to
      // https://www.gamedev.net/articles/programming/general-and-gameplay-programming/swept-aabb-collision-detection-and-response-r3084/

      // check broad first
      {
        // create a bounding box around the swept volume to do
        // a broad phase check first
        let broad_a = Rect {
          min_x: if step.x > 0.0 {
            a.min_x
          } else {
            a.min_x + step.x
          },
          max_x: if step.x < 0.0 {
            a.max_x
          } else {
            a.max_x + step.x
          },
          min_y: if step.y > 0.0 {
            a.min_y
          } else {
            a.min_y + step.y
          },
          max_y: if step.y < 0.0 {
            a.max_y
          } else {
            a.max_y + step.y
          },
        };
        if !test_aabb_v_aabb(broad_a, b) {
          return result;
        }
      }

      let x_inv_entry: f32;
      let y_inv_entry: f32;
      let x_inv_exit: f32;
      let y_inv_exit: f32;

      // find the distance between the objects on the near and far sides for
      // both x and y
      if step.x > 0.0 {
        x_inv_entry = b.min_x - (a.max_x);
        x_inv_exit = (b.max_x) - a.min_x;
      } else {
        x_inv_entry = (b.max_x) - a.min_x;
        x_inv_exit = b.min_x - (a.max_x);
      }

      if step.y > 0.0 {
        y_inv_entry = b.min_y - (a.max_y);
        y_inv_exit = (b.max_y) - a.min_y;
      } else {
        y_inv_entry = (b.max_y) - a.min_y;
        y_inv_exit = b.min_y - (a.max_y);
      }

      // find time of collision and time of leaving for each axis (if statement
      // is to prevent divide by zero)
      let x_entry: f32;
      let y_entry: f32;
      let x_exit: f32;
      let y_exit: f32;

      if step.x == 0.0 {
        x_entry = f32::MIN;
        x_exit = f32::MAX;
      } else {
        x_entry = x_inv_entry / step.x;
        x_exit = x_inv_exit / step.x;
      }

      if step.y == 0.0 {
        y_entry = f32::MIN;
        y_exit = f32::MAX;
      } else {
        y_entry = y_inv_entry / step.y;
        y_exit = y_inv_exit / step.y;
      }

      // find the earliest/latest times of collision
      let entry_time = max(x_entry, y_entry);
      let exit_time = min(x_exit, y_exit);

      if entry_time > result.t {
        return result; // an earlier collistion is already found
      }

      // if there was no collision
      if entry_time > exit_time
        || (x_entry < 0.0 && y_entry < 0.0)
        || x_entry > 1.0
        || y_entry > 1.0
      {
        return result;
      }
      result.t = entry_time;

      // calculate normal of collided surface
      if x_entry > y_entry {
        if x_inv_entry < 0.0 {
          result.normal.x = 1.0;
          result.normal.y = 0.0;
        } else {
          result.normal.x = -1.0;
          result.normal.y = 0.0;
        }
      } else {
        if y_inv_entry < 0.0 {
          result.normal.x = 0.0;
          result.normal.y = 1.0;
        } else {
          result.normal.x = 0.0;
          result.normal.y = -1.0;
        }
      }

      // return the time of collision
      return result;
    }
    (_, _) => {
      // reversed test cases handled here to reduce the above matches;
      // TODO compiler won't catch unhandled cases here and missing a case will
      // result in an infinite loop. Is there a better way?
      let mut result = sweep(step * -1.0, b, a);
      result.normal = result.normal * -1.0;
      return result;
    }
  }
}

pub fn rand_in_shape(shape: Shape) -> V2 {
  match shape {
    Shape::Point(p) => p,
    Shape::Rect(r) => {
      let x = lerpf(r.min_x, r.max_x, rand::random());
      let y = lerpf(r.min_y, r.max_y, rand::random());
      return v2(x, y);
    }
    Shape::Circle(c) => {
      let random_arc: f32 = rand::random();
      let a: f32 = random_arc * TAU;
      let r = c.r * f32::sqrt(rand::random());

      // If you need it in Cartesian coordinates
      let x = r * f32::cos(a);
      let y = r * f32::sin(a);
      return c.center + v2(x, y);
    }
  }
}