Debian Codegraph @

src/cdogs/collision/minkowski_hex.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2017, Cong Xu
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12 Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
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 // Find rectangle C by combining A and B
41 const struct vec2i sizeC = svec2i_add(sizeA, sizeB);
42
43 // Subtract velA from vB
44 const struct vec2 velBA = svec2_subtract(velB, velA);
45
46 // Find the intersection between velBA and the rectangle C centered on posA
47 float s;
48 if (!RectangleLineIntersect(
49 posA, sizeC, posB, svec2_add(posB, velBA), &s, normal))
50 {
51 return false;
52 }
53
54 // If intersection is at the start, it means we were overlapping already
55 if (s == 0)
56 {
57 *colA = posA;
58 *colB = posB;
59 }
60 else
61 {
62 // Find the actual intersection points based on the result
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 // Find the closest point at which a line intersects a rectangle
79 // Do this by finding intersections between the line and all four sides of
80 // the rectangle, and returning the closest one
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 // Border case: check if line start is entirely within rectangle
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 // Find closest of four intersections
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 // Return whether two line segments intersect, and at which fraction
122 // http://stackoverflow.com/a/1968345/2038264
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 // Lines are parallel/collinear, just assume no collision
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 }