| 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 |
#include "minkowski_hex.h" |
| 29 |
|
| 30 |
|
| 31 |
static bool RectangleLineIntersect( |
| 32 |
const struct vec2 rectPos, const struct vec2i rectSize, |
| 33 |
const struct vec2 lineStart, const struct vec2 lineEnd, float *s, |
| 34 |
struct vec2 *normal); |
| 35 |
bool MinkowskiHexCollide( |
| 36 |
const struct vec2 posA, const struct vec2 velA, const struct vec2i sizeA, |
| 37 |
const struct vec2 posB, const struct vec2 velB, const struct vec2i sizeB, |
| 38 |
struct vec2 *colA, struct vec2 *colB, struct vec2 *normal) |
| 39 |
{ |
| 40 |
|
| 41 |
const struct vec2i sizeC = svec2i_add(sizeA, sizeB); |
| 42 |
|
| 43 |
|
| 44 |
const struct vec2 velBA = svec2_subtract(velB, velA); |
| 45 |
|
| 46 |
|
| 47 |
float s; |
| 48 |
if (!RectangleLineIntersect( |
| 49 |
posA, sizeC, posB, svec2_add(posB, velBA), &s, normal)) |
| 50 |
{ |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
if (s == 0) |
| 56 |
{ |
| 57 |
*colA = posA; |
| 58 |
*colB = posB; |
| 59 |
} |
| 60 |
else |
| 61 |
{ |
| 62 |
|
| 63 |
*colA = svec2_add(posA, svec2_scale(velA, s)); |
| 64 |
*colB = svec2_add(posB, svec2_scale(velB, s)); |
| 65 |
} |
| 66 |
|
| 67 |
return true; |
| 68 |
} |
| 69 |
static bool LinesIntersect( |
| 70 |
const struct vec2 p1Start, const struct vec2 p1End, |
| 71 |
const struct vec2 p2Start, const struct vec2 p2End, |
| 72 |
float *s); |
| 73 |
static bool RectangleLineIntersect( |
| 74 |
const struct vec2 rectPos, const struct vec2i rectSize, |
| 75 |
const struct vec2 lineStart, const struct vec2 lineEnd, float *s, |
| 76 |
struct vec2 *normal) |
| 77 |
{ |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
const float left = rectPos.x - rectSize.x / 2; |
| 82 |
const float right = left + rectSize.x; |
| 83 |
const float top = rectPos.y - rectSize.y / 2; |
| 84 |
const float bottom = top + rectSize.y; |
| 85 |
const struct vec2 topLeft = svec2(left, top); |
| 86 |
const struct vec2 topRight = svec2(right, top); |
| 87 |
const struct vec2 bottomRight = svec2(right, bottom); |
| 88 |
const struct vec2 bottomLeft = svec2(left, bottom); |
| 89 |
|
| 90 |
|
| 91 |
if (lineStart.x > left && lineStart.x < right && |
| 92 |
lineStart.y > top && lineStart.y < bottom) |
| 93 |
{ |
| 94 |
*s = 0; |
| 95 |
*normal = svec2_zero(); |
| 96 |
return true; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
*s = -1; |
| 101 |
float sPart; |
| 102 |
#define _CHECK_MIN_DISTANCE(_p1, _p2, _n) \ |
| 103 |
if (LinesIntersect(_p1, _p2, lineStart, lineEnd, &sPart) &&\ |
| 104 |
(*s < 0 || sPart < *s))\ |
| 105 |
{\ |
| 106 |
*s = sPart;\ |
| 107 |
*normal = _n;\ |
| 108 |
} |
| 109 |
_CHECK_MIN_DISTANCE(topLeft, topRight, svec2(0, 1)); |
| 110 |
_CHECK_MIN_DISTANCE(topRight, bottomRight, svec2(-1, 0)); |
| 111 |
_CHECK_MIN_DISTANCE(bottomLeft, bottomRight, svec2(0, -1)); |
| 112 |
_CHECK_MIN_DISTANCE(topLeft, bottomLeft, svec2(1, 0)); |
| 113 |
|
| 114 |
return *s >= 0 && *s <= 1; |
| 115 |
} |
| 116 |
static bool LinesIntersect( |
| 117 |
const struct vec2 p1Start, const struct vec2 p1End, |
| 118 |
const struct vec2 p2Start, const struct vec2 p2End, |
| 119 |
float *s) |
| 120 |
{ |
| 121 |
|
| 122 |
|
| 123 |
const struct vec2 v1 = svec2_subtract(p1End, p1Start); |
| 124 |
const struct vec2 v2 = svec2_subtract(p2End, p2Start); |
| 125 |
|
| 126 |
const float divisor = -v2.x * v1.y + v1.x * v2.y; |
| 127 |
if (divisor == 0) |
| 128 |
{ |
| 129 |
|
| 130 |
return false; |
| 131 |
} |
| 132 |
const struct vec2 p12Start = svec2_subtract(p1Start, p2Start); |
| 133 |
*s = (float)(-v1.y * p12Start.x + v1.x * p12Start.y) / divisor; |
| 134 |
const float t = (float)(v2.x * p12Start.y - v2.y * p12Start.x) / divisor; |
| 135 |
|
| 136 |
return *s >= 0 && *s <= 1 && t >= 0 && t <= 1; |
| 137 |
} |