Debian Codegraph @

src/cpCollision.c

raw symbols SCIP
1 /* Copyright (c) 2013 Scott Lembcke and Howling Moon Software
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 * SOFTWARE.
20 */
21
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "chipmunk/chipmunk_private.h"cxx . . $ `<file>/include/chipmunk/chipmunk_private.h`/
26 #include "chipmunk/cpRobust.h"cxx . . $ `<file>/include/chipmunk/cpRobust.h`/
27
28 #if DEBUG && 0
29 #include "ChipmunkDemo.h"
30 #define DRAW_ALL 0
31 #define DRAW_GJK (0 || DRAW_ALL)
32 #define DRAW_EPA (0 || DRAW_ALL)
33 #define DRAW_CLOSEST (0 || DRAW_ALL)
34 #define DRAW_CLIP (0 || DRAW_ALL)
35
36 #define PRINT_LOG 0
37 #endif
38
39 #define MAX_GJK_ITERATIONScxx . . $ `src/cpCollision.c:39:9`! 30
40 #define MAX_EPA_ITERATIONScxx . . $ `src/cpCollision.c:40:9`! 30
41 #define WARN_GJK_ITERATIONScxx . . $ `src/cpCollision.c:41:9`! 20
42 #define WARN_EPA_ITERATIONScxx . . $ `src/cpCollision.c:42:9`! 20
43
44 static inline void
45 cpCollisionInfoPushContactcxx . . $ cpCollisionInfoPushContact(50f3b5096699c213).(struct cpCollisionInfocxx . . $ cpCollisionInfo# *info, cpVectcxx . . $ cpVect# p1, cpVectcxx . . $ cpVect# p2, cpHashValuecxx . . $ cpHashValue# hash)
46 {
47 cpAssertSoftcxx . . $ `include/chipmunk/chipmunk.h:51:10`!(info->count <= CP_MAX_CONTACTS_PER_ARBITER, "Internal error: Tried to push too many contacts.");
48
49 struct cpContactcxx . . $ cpContact# *con = &info->arrcxx . . $ cpCollisionInfo#arr.[info->countcxx . . $ cpCollisionInfo#count.];
50 con->r1cxx . . $ cpContact#r1. = p1;
51 con->r2cxx . . $ cpContact#r2. = p2;
52 con->hashcxx . . $ cpContact#hash. = hash;
53
54 info->countcxx . . $ cpCollisionInfo#count.++;
55 }
56
57 //MARK: Support Points and Edges:
58
59 // Support points are the maximal points on a shape's perimeter along a certain axis.
60 // The GJK and EPA algorithms use support points to iteratively sample the surface of the two shapes' minkowski difference.
61
62 static inline int
63 PolySupportPointIndexcxx . . $ PolySupportPointIndex(b1a7e4b2c3798bf8).(const int count, const struct cpSplittingPlanecxx . . $ cpSplittingPlane# *planes, const cpVectcxx . . $ cpVect# n)
64 {
65 cpFloatcxx . . $ cpFloat# max = -INFINITYcxx . . $ `<external>/11263917310783833665/math.h:93:12`!;
66 int index = 0;
67
68 for(int i=0; i<count; i++){
69 cpVectcxx . . $ cpVect# v = planes[i].v0cxx . . $ cpSplittingPlane#v0.;
70 cpFloatcxx . . $ cpFloat# d = cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(v, n);
71 if(d > max){
72 max = d;
73 index = i;
74 }
75 }
76
77 return index;
78 }
79
80 struct SupportPointcxx . . $ SupportPoint# {
81 cpVectcxx . . $ cpVect# pcxx . . $ SupportPoint#p.;
82 // Save an index of the point so it can be cheaply looked up as a starting point for the next frame.
83 cpCollisionIDcxx . . $ cpCollisionID# indexcxx . . $ SupportPoint#index.;
84 };
85
86 static inline struct SupportPointcxx . . $ SupportPoint#
87 SupportPointNewcxx . . $ SupportPointNew(a5c89c510356d8c5).(cpVectcxx . . $ cpVect# p, cpCollisionIDcxx . . $ cpCollisionID# index)
88 {
89 struct SupportPointcxx . . $ SupportPoint# point = {p, index};
90 return point;
91 }
92
93 typedef struct SupportPointcxx . . $ SupportPoint# (*SupportPointFunccxx . . $ SupportPointFunc#)(const cpShapecxx . . $ cpShape# *shape, const cpVectcxx . . $ cpVect# n);
94
95 static inline struct SupportPointcxx . . $ SupportPoint#
96 CircleSupportPointcxx . . $ CircleSupportPoint(4888c039b288898e).(const cpCircleShapecxx . . $ cpCircleShape# *circle, const cpVectcxx . . $ cpVect# n)
97 {
98 return SupportPointNewcxx . . $ SupportPointNew(a5c89c510356d8c5).(circle->tccxx . . $ cpCircleShape#tc., 0);
99 }
100
101 static inline struct SupportPointcxx . . $ SupportPoint#
102 SegmentSupportPointcxx . . $ SegmentSupportPoint(2f0dea3f8e488fca).(const cpSegmentShapecxx . . $ cpSegmentShape# *seg, const cpVectcxx . . $ cpVect# n)
103 {
104 if(cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(seg->tacxx . . $ cpSegmentShape#ta., n) > cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(seg->tbcxx . . $ cpSegmentShape#tb., n)){
105 return SupportPointNewcxx . . $ SupportPointNew(a5c89c510356d8c5).(seg->tacxx . . $ cpSegmentShape#ta., 0);
106 } else {
107 return SupportPointNewcxx . . $ SupportPointNew(a5c89c510356d8c5).(seg->tbcxx . . $ cpSegmentShape#tb., 1);
108 }
109 }
110
111 static inline struct SupportPointcxx . . $ SupportPoint#
112 PolySupportPointcxx . . $ PolySupportPoint(25638014a02645e7).(const cpPolyShapecxx . . $ cpPolyShape# *poly, const cpVectcxx . . $ cpVect# n)
113 {
114 const struct cpSplittingPlanecxx . . $ cpSplittingPlane# *planes = poly->planescxx . . $ cpPolyShape#planes.;
115 int i = PolySupportPointIndexcxx . . $ PolySupportPointIndex(b1a7e4b2c3798bf8).(poly->countcxx . . $ cpPolyShape#count., planes, n);
116 return SupportPointNewcxx . . $ SupportPointNew(a5c89c510356d8c5).(planes[i].v0cxx . . $ cpSplittingPlane#v0., i);
117 }
118
119 // A point on the surface of two shape's minkowski difference.
120 struct MinkowskiPointcxx . . $ MinkowskiPoint# {
121 // Cache the two original support points.
122 cpVectcxx . . $ cpVect# acxx . . $ MinkowskiPoint#a., bcxx . . $ MinkowskiPoint#b.;
123 // b - a
124 cpVectcxx . . $ cpVect# abcxx . . $ MinkowskiPoint#ab.;
125 // Concatenate the two support point indexes.
126 cpCollisionIDcxx . . $ cpCollisionID# idcxx . . $ MinkowskiPoint#id.;
127 };
128
129 static inline struct MinkowskiPointcxx . . $ MinkowskiPoint#
130 MinkowskiPointNewcxx . . $ MinkowskiPointNew(a219ec22ef7dfcc2).(const struct SupportPointcxx . . $ SupportPoint# a, const struct SupportPointcxx . . $ SupportPoint# b)
131 {
132 struct MinkowskiPointcxx . . $ MinkowskiPoint# point = {a.pcxx . . $ SupportPoint#p., b.pcxx . . $ SupportPoint#p., cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(b.pcxx . . $ SupportPoint#p., a.pcxx . . $ SupportPoint#p.), (a.indexcxx . . $ SupportPoint#index. & 0xFF)<<8 | (b.indexcxx . . $ SupportPoint#index. & 0xFF)};
133 return point;
134 }
135
136 struct SupportContextcxx . . $ SupportContext# {
137 const cpShapecxx . . $ cpShape# *shape1cxx . . $ SupportContext#shape1., *shape2cxx . . $ SupportContext#shape2.;
138 SupportPointFunccxx . . $ SupportPointFunc# func1cxx . . $ SupportContext#func1., func2cxx . . $ SupportContext#func2.;
139 };
140
141 // Calculate the maximal point on the minkowski difference of two shapes along a particular axis.
142 static inline struct MinkowskiPointcxx . . $ MinkowskiPoint#
143 Supportcxx . . $ Support(5dcda928b03a0587).(const struct SupportContextcxx . . $ SupportContext# *ctx, const cpVectcxx . . $ cpVect# n)
144 {
145 struct SupportPointcxx . . $ SupportPoint# a = ctx->func1cxx . . $ SupportContext#func1.(ctx->shape1cxx . . $ SupportContext#shape1., cpvnegcxx . . $ cpvneg(e06142a62109ae08).(n));
146 struct SupportPointcxx . . $ SupportPoint# b = ctx->func2cxx . . $ SupportContext#func2.(ctx->shape2cxx . . $ SupportContext#shape2., n);
147 return MinkowskiPointNewcxx . . $ MinkowskiPointNew(a219ec22ef7dfcc2).(a, b);
148 }
149
150 struct EdgePointcxx . . $ EdgePoint# {
151 cpVectcxx . . $ cpVect# pcxx . . $ EdgePoint#p.;
152 // Keep a hash value for Chipmunk's collision hashing mechanism.
153 cpHashValuecxx . . $ cpHashValue# hashcxx . . $ EdgePoint#hash.;
154 };
155
156 // Support edges are the edges of a polygon or segment shape that are in contact.
157 struct Edgecxx . . $ Edge# {
158 struct EdgePointcxx . . $ EdgePoint# acxx . . $ Edge#a., bcxx . . $ Edge#b.;
159 cpFloatcxx . . $ cpFloat# rcxx . . $ Edge#r.;
160 cpVectcxx . . $ cpVect# ncxx . . $ Edge#n.;
161 };
162
163 static struct Edgecxx . . $ Edge#
164 SupportEdgeForPolycxx . . $ SupportEdgeForPoly(428407c579fd435).(const cpPolyShapecxx . . $ cpPolyShape# *poly, const cpVectcxx . . $ cpVect# n)
165 {
166 int count = poly->countcxx . . $ cpPolyShape#count.;
167 int i1 = PolySupportPointIndexcxx . . $ PolySupportPointIndex(b1a7e4b2c3798bf8).(poly->countcxx . . $ cpPolyShape#count., poly->planescxx . . $ cpPolyShape#planes., n);
168
169 // TODO: get rid of mod eventually, very expensive on ARM
170 int i0 = (i1 - 1 + count)%count;
171 int i2 = (i1 + 1)%count;
172
173 const struct cpSplittingPlanecxx . . $ cpSplittingPlane# *planes = poly->planescxx . . $ cpPolyShape#planes.;
174 cpHashValuecxx . . $ cpHashValue# hashid = poly->shapecxx . . $ cpPolyShape#shape..hashidcxx . . $ cpShape#hashid.;
175 if(cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(n, planes[i1].ncxx . . $ cpSplittingPlane#n.) > cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(n, planes[i2].ncxx . . $ cpSplittingPlane#n.)){
176 struct Edgecxx . . $ Edge# edge = {{planes[i0].v0cxx . . $ cpSplittingPlane#v0., CP_HASH_PAIRcxx . . $ `include/chipmunk/chipmunk_private.h:29:9`!(hashid, i0)}, {planes[i1].v0cxx . . $ cpSplittingPlane#v0., CP_HASH_PAIRcxx . . $ `include/chipmunk/chipmunk_private.h:29:9`!(hashid, i1)}, poly->rcxx . . $ cpPolyShape#r., planes[i1].ncxx . . $ cpSplittingPlane#n.};
177 return edge;
178 } else {
179 struct Edgecxx . . $ Edge# edge = {{planes[i1].v0cxx . . $ cpSplittingPlane#v0., CP_HASH_PAIRcxx . . $ `include/chipmunk/chipmunk_private.h:29:9`!(hashid, i1)}, {planes[i2].v0cxx . . $ cpSplittingPlane#v0., CP_HASH_PAIRcxx . . $ `include/chipmunk/chipmunk_private.h:29:9`!(hashid, i2)}, poly->rcxx . . $ cpPolyShape#r., planes[i2].ncxx . . $ cpSplittingPlane#n.};
180 return edge;
181 }
182 }
183
184 static struct Edgecxx . . $ Edge#
185 SupportEdgeForSegmentcxx . . $ SupportEdgeForSegment(5f9fea087ac45bd5).(const cpSegmentShapecxx . . $ cpSegmentShape# *seg, const cpVectcxx . . $ cpVect# n)
186 {
187 cpHashValuecxx . . $ cpHashValue# hashid = seg->shapecxx . . $ cpSegmentShape#shape..hashidcxx . . $ cpShape#hashid.;
188 if(cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(seg->tncxx . . $ cpSegmentShape#tn., n) > 0.0){
189 struct Edgecxx . . $ Edge# edge = {{seg->tacxx . . $ cpSegmentShape#ta., CP_HASH_PAIRcxx . . $ `include/chipmunk/chipmunk_private.h:29:9`!(hashid, 0)}, {seg->tbcxx . . $ cpSegmentShape#tb., CP_HASH_PAIRcxx . . $ `include/chipmunk/chipmunk_private.h:29:9`!(hashid, 1)}, seg->rcxx . . $ cpSegmentShape#r., seg->tncxx . . $ cpSegmentShape#tn.};
190 return edge;
191 } else {
192 struct Edgecxx . . $ Edge# edge = {{seg->tbcxx . . $ cpSegmentShape#tb., CP_HASH_PAIRcxx . . $ `include/chipmunk/chipmunk_private.h:29:9`!(hashid, 1)}, {seg->tacxx . . $ cpSegmentShape#ta., CP_HASH_PAIRcxx . . $ `include/chipmunk/chipmunk_private.h:29:9`!(hashid, 0)}, seg->rcxx . . $ cpSegmentShape#r., cpvnegcxx . . $ cpvneg(e06142a62109ae08).(seg->tncxx . . $ cpSegmentShape#tn.)};
193 return edge;
194 }
195 }
196
197 // Find the closest p(t) to (0, 0) where p(t) = a*(1-t)/2 + b*(1+t)/2
198 // The range for t is [-1, 1] to avoid floating point issues if the parameters are swapped.
199 static inline cpFloatcxx . . $ cpFloat#
200 ClosestTcxx . . $ ClosestT(7d57c1c4dd559d53).(const cpVectcxx . . $ cpVect# a, const cpVectcxx . . $ cpVect# b)
201 {
202 cpVectcxx . . $ cpVect# delta = cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(b, a);
203 return -cpfclampcxx . . $ cpfclamp(7678bba071383ab).(cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(delta, cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(a, b))/cpvlengthsqcxx . . $ cpvlengthsq(c4ef53c170268e01).(delta), -1.0f, 1.0f);
204 }
205
206 // Basically the same as cpvlerp(), except t = [-1, 1]
207 static inline cpVectcxx . . $ cpVect#
208 LerpTcxx . . $ LerpT(12b05c0b9acd6d4b).(const cpVectcxx . . $ cpVect# a, const cpVectcxx . . $ cpVect# b, const cpFloatcxx . . $ cpFloat# t)
209 {
210 cpFloatcxx . . $ cpFloat# ht = 0.5f*t;
211 return cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(a, 0.5f - ht), cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(b, 0.5f + ht));
212 }
213
214 // Closest points on the surface of two shapes.
215 struct ClosestPointscxx . . $ ClosestPoints# {
216 // Surface points in absolute coordinates.
217 cpVectcxx . . $ cpVect# acxx . . $ ClosestPoints#a., bcxx . . $ ClosestPoints#b.;
218 // Minimum separating axis of the two shapes.
219 cpVectcxx . . $ cpVect# ncxx . . $ ClosestPoints#n.;
220 // Signed distance between the points.
221 cpFloatcxx . . $ cpFloat# dcxx . . $ ClosestPoints#d.;
222 // Concatenation of the id's of the minkoski points.
223 cpCollisionIDcxx . . $ cpCollisionID# idcxx . . $ ClosestPoints#id.;
224 };
225
226 // Calculate the closest points on two shapes given the closest edge on their minkowski difference to (0, 0)
227 static inline struct ClosestPointscxx . . $ ClosestPoints#
228 ClosestPointsNewcxx . . $ ClosestPointsNew(3ec4dfd8bd87ce0c).(const struct MinkowskiPointcxx . . $ MinkowskiPoint# v0, const struct MinkowskiPointcxx . . $ MinkowskiPoint# v1)
229 {
230 // Find the closest p(t) on the minkowski difference to (0, 0)
231 cpFloatcxx . . $ cpFloat# t = ClosestTcxx . . $ ClosestT(7d57c1c4dd559d53).(v0.abcxx . . $ MinkowskiPoint#ab., v1.abcxx . . $ MinkowskiPoint#ab.);
232 cpVectcxx . . $ cpVect# p = LerpTcxx . . $ LerpT(12b05c0b9acd6d4b).(v0.abcxx . . $ MinkowskiPoint#ab., v1.abcxx . . $ MinkowskiPoint#ab., t);
233
234 // Interpolate the original support points using the same 't' value as above.
235 // This gives you the closest surface points in absolute coordinates. NEAT!
236 cpVectcxx . . $ cpVect# pa = LerpTcxx . . $ LerpT(12b05c0b9acd6d4b).(v0.acxx . . $ MinkowskiPoint#a., v1.acxx . . $ MinkowskiPoint#a., t);
237 cpVectcxx . . $ cpVect# pb = LerpTcxx . . $ LerpT(12b05c0b9acd6d4b).(v0.bcxx . . $ MinkowskiPoint#b., v1.bcxx . . $ MinkowskiPoint#b., t);
238 cpCollisionIDcxx . . $ cpCollisionID# id = (v0.idcxx . . $ MinkowskiPoint#id. & 0xFFFF)<<16 | (v1.idcxx . . $ MinkowskiPoint#id. & 0xFFFF);
239
240 // First try calculating the MSA from the minkowski difference edge.
241 // This gives us a nice, accurate MSA when the surfaces are close together.
242 cpVectcxx . . $ cpVect# delta = cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(v1.abcxx . . $ MinkowskiPoint#ab., v0.abcxx . . $ MinkowskiPoint#ab.);
243 cpVectcxx . . $ cpVect# n = cpvnormalizecxx . . $ cpvnormalize(e06142a62109ae08).(cpvrperpcxx . . $ cpvrperp(e06142a62109ae08).(delta));
244 cpFloatcxx . . $ cpFloat# d = cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(n, p);
245
246 if(d <= 0.0f || (-1.0f < t && t < 1.0f)){
247 // If the shapes are overlapping, or we have a regular vertex/edge collision, we are done.
248 struct ClosestPointscxx . . $ ClosestPoints# points = {pa, pb, n, d, id};
249 return points;
250 } else {
251 // Vertex/vertex collisions need special treatment since the MSA won't be shared with an axis of the minkowski difference.
252 cpFloatcxx . . $ cpFloat# d2 = cpvlengthcxx . . $ cpvlength(c4ef53c170268e01).(p);
253 cpVectcxx . . $ cpVect# n2 = cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(p, 1.0f/(d2 + CPFLOAT_MINcxx . . $ `include/chipmunk/chipmunk_types.h:78:10`!));
254
255 struct ClosestPointscxx . . $ ClosestPoints# points = {pa, pb, n2, d2, id};
256 return points;
257 }
258 }
259
260 //MARK: EPA Functions
261
262 static inline cpFloatcxx . . $ cpFloat#
263 ClosestDistcxx . . $ ClosestDist(7d57c1c4dd559d53).(const cpVectcxx . . $ cpVect# v0,const cpVectcxx . . $ cpVect# v1)
264 {
265 return cpvlengthsqcxx . . $ cpvlengthsq(c4ef53c170268e01).(LerpTcxx . . $ LerpT(12b05c0b9acd6d4b).(v0, v1, ClosestTcxx . . $ ClosestT(7d57c1c4dd559d53).(v0, v1)));
266 }
267
268 // Recursive implementation of the EPA loop.
269 // Each recursion adds a point to the convex hull until it's known that we have the closest point on the surface.
270 static struct ClosestPointscxx . . $ ClosestPoints#
271 EPARecursecxx . . $ EPARecurse(22f916dc53773f42).(const struct SupportContextcxx . . $ SupportContext# *ctx, const int count, const struct MinkowskiPointcxx . . $ MinkowskiPoint# *hull, const int iteration)
272 {
273 int mini = 0;
274 cpFloatcxx . . $ cpFloat# minDist = INFINITYcxx . . $ `<external>/11263917310783833665/math.h:93:12`!;
275
276 // TODO: precalculate this when building the hull and save a step.
277 // Find the closest segment hull[i] and hull[i + 1] to (0, 0)
278 for(int j=0, i=count-1; j<count; i=j, j++){
279 cpFloatcxx . . $ cpFloat# d = ClosestDistcxx . . $ ClosestDist(7d57c1c4dd559d53).(hull[i].abcxx . . $ MinkowskiPoint#ab., hull[j].abcxx . . $ MinkowskiPoint#ab.);
280 if(d < minDist){
281 minDist = d;
282 mini = i;
283 }
284 }
285
286 struct MinkowskiPointcxx . . $ MinkowskiPoint# v0 = hull[mini];
287 struct MinkowskiPointcxx . . $ MinkowskiPoint# v1 = hull[(mini + 1)%count];
288 cpAssertSoftcxx . . $ `include/chipmunk/chipmunk.h:51:10`!(!cpveql(v0.ab, v1.ab), "Internal Error: EPA vertexes are the same (%d and %d)", mini, (mini + 1)%count);
289
290 // Check if there is a point on the minkowski difference beyond this edge.
291 struct MinkowskiPointcxx . . $ MinkowskiPoint# p = Supportcxx . . $ Support(5dcda928b03a0587).(ctx, cpvperpcxx . . $ cpvperp(e06142a62109ae08).(cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(v1.abcxx . . $ MinkowskiPoint#ab., v0.abcxx . . $ MinkowskiPoint#ab.)));
292
293 #if DRAW_EPA
294 cpVect verts[count];
295 for(int i=0; i<count; i++) verts[i] = hull[i].ab;
296
297 ChipmunkDebugDrawPolygon(count, verts, 0.0, RGBAColor(1, 1, 0, 1), RGBAColor(1, 1, 0, 0.25));
298 ChipmunkDebugDrawSegment(v0.ab, v1.ab, RGBAColor(1, 0, 0, 1));
299
300 ChipmunkDebugDrawDot(5, p.ab, LAColor(1, 1));
301 #endif
302
303 // The usual exit condition is a duplicated vertex.
304 // Much faster to check the ids than to check the signed area.
305 cpBoolcxx . . $ cpBool# duplicate = (p.idcxx . . $ MinkowskiPoint#id. == v0.idcxx . . $ MinkowskiPoint#id. || p.idcxx . . $ MinkowskiPoint#id. == v1.idcxx . . $ MinkowskiPoint#id.);
306
307 if(!duplicate && cpCheckPointGreatercxx . . $ cpCheckPointGreater(265b97300a610b72).(v0.abcxx . . $ MinkowskiPoint#ab., v1.abcxx . . $ MinkowskiPoint#ab., p.abcxx . . $ MinkowskiPoint#ab.) && iteration < MAX_EPA_ITERATIONScxx . . $ `src/cpCollision.c:40:9`!){
308 // Rebuild the convex hull by inserting p.
309 struct MinkowskiPointcxx . . $ MinkowskiPoint# *hull2 = (struct MinkowskiPointcxx . . $ MinkowskiPoint# *)allocacxx . . $ `<external>/11612401604547820122/alloca.h:35:10`!((count + 1)*sizeof(struct MinkowskiPoint));
310 int count2 = 1;
311 hull2[0] = p;
312
313 for(int i=0; i<count; i++){
314 int index = (mini + 1 + i)%count;
315
316 cpVectcxx . . $ cpVect# h0 = hull2[count2 - 1].abcxx . . $ MinkowskiPoint#ab.;
317 cpVectcxx . . $ cpVect# h1 = hull[index].abcxx . . $ MinkowskiPoint#ab.;
318 cpVectcxx . . $ cpVect# h2 = (i + 1 < count ? hull[(index + 1)%count] : p).abcxx . . $ MinkowskiPoint#ab.;
319
320 if(cpCheckPointGreatercxx . . $ cpCheckPointGreater(265b97300a610b72).(h0, h2, h1)){
321 hull2[count2] = hull[index];
322 count2++;
323 }
324 }
325
326 return EPARecursecxx . . $ EPARecurse(22f916dc53773f42).(ctx, count2, hull2, iteration + 1);
327 } else {
328 // Could not find a new point to insert, so we have found the closest edge of the minkowski difference.
329 cpAssertWarncxx . . $ `include/chipmunk/chipmunk.h:50:10`!(iteration < WARN_EPA_ITERATIONS, "High EPA iterations: %d", iteration);
330 return ClosestPointsNewcxx . . $ ClosestPointsNew(3ec4dfd8bd87ce0c).(v0, v1);
331 }
332 }
333
334 // Find the closest points on the surface of two overlapping shapes using the EPA algorithm.
335 // EPA is called from GJK when two shapes overlap.
336 // This is a moderately expensive step! Avoid it by adding radii to your shapes so their inner polygons won't overlap.
337 static struct ClosestPointscxx . . $ ClosestPoints#
338 EPAcxx . . $ EPA(156c9d3a215953e2).(const struct SupportContextcxx . . $ SupportContext# *ctx, const struct MinkowskiPointcxx . . $ MinkowskiPoint# v0, const struct MinkowskiPointcxx . . $ MinkowskiPoint# v1, const struct MinkowskiPointcxx . . $ MinkowskiPoint# v2)
339 {
340 // TODO: allocate a NxM array here and do an in place convex hull reduction in EPARecurse?
341 struct MinkowskiPointcxx . . $ MinkowskiPoint# hull[3] = {v0, v1, v2};
342 return EPARecursecxx . . $ EPARecurse(22f916dc53773f42).(ctx, 3, hull, 1);
343 }
344
345 //MARK: GJK Functions.
346
347 // Recursive implementation of the GJK loop.
348 static inline struct ClosestPointscxx . . $ ClosestPoints#
349 GJKRecursecxx . . $ GJKRecurse(963884cb5eafac21).(const struct SupportContextcxx . . $ SupportContext# *ctx, const struct MinkowskiPointcxx . . $ MinkowskiPoint# v0, const struct MinkowskiPointcxx . . $ MinkowskiPoint# v1, const int iteration)
350 {
351 if(iteration > MAX_GJK_ITERATIONScxx . . $ `src/cpCollision.c:39:9`!){
352 cpAssertWarncxx . . $ `include/chipmunk/chipmunk.h:50:10`!(iteration < WARN_GJK_ITERATIONS, "High GJK iterations: %d", iteration);
353 return ClosestPointsNewcxx . . $ ClosestPointsNew(3ec4dfd8bd87ce0c).(v0, v1);
354 }
355
356 if(cpCheckPointGreatercxx . . $ cpCheckPointGreater(265b97300a610b72).(v1.abcxx . . $ MinkowskiPoint#ab., v0.abcxx . . $ MinkowskiPoint#ab., cpvzerocxx . . $ cpvzero.)){
357 // Origin is behind axis. Flip and try again.
358 return GJKRecursecxx . . $ GJKRecurse(963884cb5eafac21).(ctx, v1, v0, iteration);
359 } else {
360 cpFloatcxx . . $ cpFloat# t = ClosestTcxx . . $ ClosestT(7d57c1c4dd559d53).(v0.abcxx . . $ MinkowskiPoint#ab., v1.abcxx . . $ MinkowskiPoint#ab.);
361 cpVectcxx . . $ cpVect# n = (-1.0f < t && t < 1.0f ? cpvperpcxx . . $ cpvperp(e06142a62109ae08).(cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(v1.abcxx . . $ MinkowskiPoint#ab., v0.abcxx . . $ MinkowskiPoint#ab.)) : cpvnegcxx . . $ cpvneg(e06142a62109ae08).(LerpTcxx . . $ LerpT(12b05c0b9acd6d4b).(v0.abcxx . . $ MinkowskiPoint#ab., v1.abcxx . . $ MinkowskiPoint#ab., t)));
362 struct MinkowskiPointcxx . . $ MinkowskiPoint# p = Supportcxx . . $ Support(5dcda928b03a0587).(ctx, n);
363
364 #if DRAW_GJK
365 ChipmunkDebugDrawSegment(v0.ab, v1.ab, RGBAColor(1, 1, 1, 1));
366 cpVect c = cpvlerp(v0.ab, v1.ab, 0.5);
367 ChipmunkDebugDrawSegment(c, cpvadd(c, cpvmult(cpvnormalize(n), 5.0)), RGBAColor(1, 0, 0, 1));
368
369 ChipmunkDebugDrawDot(5.0, p.ab, LAColor(1, 1));
370 #endif
371
372 if(cpCheckPointGreatercxx . . $ cpCheckPointGreater(265b97300a610b72).(p.abcxx . . $ MinkowskiPoint#ab., v0.abcxx . . $ MinkowskiPoint#ab., cpvzerocxx . . $ cpvzero.) && cpCheckPointGreatercxx . . $ cpCheckPointGreater(265b97300a610b72).(v1.abcxx . . $ MinkowskiPoint#ab., p.abcxx . . $ MinkowskiPoint#ab., cpvzerocxx . . $ cpvzero.)){
373 // The triangle v0, p, v1 contains the origin. Use EPA to find the MSA.
374 cpAssertWarncxx . . $ `include/chipmunk/chipmunk.h:50:10`!(iteration < WARN_GJK_ITERATIONS, "High GJK->EPA iterations: %d", iteration);
375 return EPAcxx . . $ EPA(156c9d3a215953e2).(ctx, v0, p, v1);
376 } else {
377 if(cpCheckAxiscxx . . $ cpCheckAxis(6a44657b48cbf71e).(v0.abcxx . . $ MinkowskiPoint#ab., v1.abcxx . . $ MinkowskiPoint#ab., p.abcxx . . $ MinkowskiPoint#ab., n)){
378 // The edge v0, v1 that we already have is the closest to (0, 0) since p was not closer.
379 cpAssertWarncxx . . $ `include/chipmunk/chipmunk.h:50:10`!(iteration < WARN_GJK_ITERATIONS, "High GJK iterations: %d", iteration);
380 return ClosestPointsNewcxx . . $ ClosestPointsNew(3ec4dfd8bd87ce0c).(v0, v1);
381 } else {
382 // p was closer to the origin than our existing edge.
383 // Need to figure out which existing point to drop.
384 if(ClosestDistcxx . . $ ClosestDist(7d57c1c4dd559d53).(v0.abcxx . . $ MinkowskiPoint#ab., p.abcxx . . $ MinkowskiPoint#ab.) < ClosestDistcxx . . $ ClosestDist(7d57c1c4dd559d53).(p.abcxx . . $ MinkowskiPoint#ab., v1.abcxx . . $ MinkowskiPoint#ab.)){
385 return GJKRecursecxx . . $ GJKRecurse(963884cb5eafac21).(ctx, v0, p, iteration + 1);
386 } else {
387 return GJKRecursecxx . . $ GJKRecurse(963884cb5eafac21).(ctx, p, v1, iteration + 1);
388 }
389 }
390 }
391 }
392 }
393
394 // Get a SupportPoint from a cached shape and index.
395 static struct SupportPointcxx . . $ SupportPoint#
396 ShapePointcxx . . $ ShapePoint(d3f0c8756ae2b7a1).(const cpShapecxx . . $ cpShape# *shape, const int i)
397 {
398 switch(shape->klasscxx . . $ cpShape#klass.->typecxx . . $ cpShapeClass#type.){
399 case CP_CIRCLE_SHAPEcxx . . $ CP_CIRCLE_SHAPE.: {
400 return SupportPointNewcxx . . $ SupportPointNew(a5c89c510356d8c5).(((cpCircleShapecxx . . $ cpCircleShape# *)shape)->tccxx . . $ cpCircleShape#tc., 0);
401 } case CP_SEGMENT_SHAPEcxx . . $ CP_SEGMENT_SHAPE.: {
402 cpSegmentShapecxx . . $ cpSegmentShape# *seg = (cpSegmentShapecxx . . $ cpSegmentShape# *)shape;
403 return SupportPointNewcxx . . $ SupportPointNew(a5c89c510356d8c5).(i == 0 ? seg->tacxx . . $ cpSegmentShape#ta. : seg->tbcxx . . $ cpSegmentShape#tb., i);
404 } case CP_POLY_SHAPEcxx . . $ CP_POLY_SHAPE.: {
405 cpPolyShapecxx . . $ cpPolyShape# *poly = (cpPolyShapecxx . . $ cpPolyShape# *)shape;
406 // Poly shapes may change vertex count.
407 int index = (i < poly->countcxx . . $ cpPolyShape#count. ? i : 0);
408 return SupportPointNewcxx . . $ SupportPointNew(a5c89c510356d8c5).(poly->planescxx . . $ cpPolyShape#planes.[index].v0cxx . . $ cpSplittingPlane#v0., index);
409 } default: {
410 return SupportPointNewcxx . . $ SupportPointNew(a5c89c510356d8c5).(cpvzerocxx . . $ cpvzero., 0);
411 }
412 }
413 }
414
415 // Find the closest points between two shapes using the GJK algorithm.
416 static struct ClosestPointscxx . . $ ClosestPoints#
417 GJKcxx . . $ GJK(af16ae11255c80f5).(const struct SupportContextcxx . . $ SupportContext# *ctx, cpCollisionIDcxx . . $ cpCollisionID# *id)
418 {
419 #if DRAW_GJK || DRAW_EPA
420 int count1 = 1;
421 int count2 = 1;
422
423 switch(ctx->shape1->klass->type){
424 case CP_SEGMENT_SHAPE: count1 = 2; break;
425 case CP_POLY_SHAPE: count1 = ((cpPolyShape *)ctx->shape1)->count; break;
426 default: break;
427 }
428
429 switch(ctx->shape2->klass->type){
430 case CP_SEGMENT_SHAPE: count1 = 2; break;
431 case CP_POLY_SHAPE: count2 = ((cpPolyShape *)ctx->shape2)->count; break;
432 default: break;
433 }
434
435
436 // draw the minkowski difference origin
437 cpVect origin = cpvzero;
438 ChipmunkDebugDrawDot(5.0, origin, RGBAColor(1,0,0,1));
439
440 int mdiffCount = count1*count2;
441 cpVect *mdiffVerts = alloca(mdiffCount*sizeof(cpVect));
442
443 for(int i=0; i<count1; i++){
444 for(int j=0; j<count2; j++){
445 cpVect v = cpvsub(ShapePoint(ctx->shape2, j).p, ShapePoint(ctx->shape1, i).p);
446 mdiffVerts[i*count2 + j] = v;
447 ChipmunkDebugDrawDot(2.0, v, RGBAColor(1, 0, 0, 1));
448 }
449 }
450
451 cpVect *hullVerts = alloca(mdiffCount*sizeof(cpVect));
452 int hullCount = cpConvexHull(mdiffCount, mdiffVerts, hullVerts, NULL, 0.0);
453
454 ChipmunkDebugDrawPolygon(hullCount, hullVerts, 0.0, RGBAColor(1, 0, 0, 1), RGBAColor(1, 0, 0, 0.25));
455 #endif
456
457 struct MinkowskiPointcxx . . $ MinkowskiPoint# v0, v1;
458 if(*id){
459 // Use the minkowski points from the last frame as a starting point using the cached indexes.
460 v0 = MinkowskiPointNewcxx . . $ MinkowskiPointNew(a219ec22ef7dfcc2).(ShapePointcxx . . $ ShapePoint(d3f0c8756ae2b7a1).(ctx->shape1cxx . . $ SupportContext#shape1., (*id>>24)&0xFF), ShapePointcxx . . $ ShapePoint(d3f0c8756ae2b7a1).(ctx->shape2cxx . . $ SupportContext#shape2., (*id>>16)&0xFF));
461 v1 = MinkowskiPointNewcxx . . $ MinkowskiPointNew(a219ec22ef7dfcc2).(ShapePointcxx . . $ ShapePoint(d3f0c8756ae2b7a1).(ctx->shape1cxx . . $ SupportContext#shape1., (*id>> 8)&0xFF), ShapePointcxx . . $ ShapePoint(d3f0c8756ae2b7a1).(ctx->shape2cxx . . $ SupportContext#shape2., (*id )&0xFF));
462 } else {
463 // No cached indexes, use the shapes' bounding box centers as a guess for a starting axis.
464 cpVectcxx . . $ cpVect# axis = cpvperpcxx . . $ cpvperp(e06142a62109ae08).(cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(cpBBCentercxx . . $ cpBBCenter(f04c757da25a6138).(ctx->shape1cxx . . $ SupportContext#shape1.->bbcxx . . $ cpShape#bb.), cpBBCentercxx . . $ cpBBCenter(f04c757da25a6138).(ctx->shape2cxx . . $ SupportContext#shape2.->bbcxx . . $ cpShape#bb.)));
465 v0 = Supportcxx . . $ Support(5dcda928b03a0587).(ctx, axis);
466 v1 = Supportcxx . . $ Support(5dcda928b03a0587).(ctx, cpvnegcxx . . $ cpvneg(e06142a62109ae08).(axis));
467 }
468
469 struct ClosestPointscxx . . $ ClosestPoints# points = GJKRecursecxx . . $ GJKRecurse(963884cb5eafac21).(ctx, v0, v1, 1);
470 *id = points.idcxx . . $ ClosestPoints#id.;
471 return points;
472 }
473
474 //MARK: Contact Clipping
475
476 // Given two support edges, find contact point pairs on their surfaces.
477 static inline void
478 ContactPointscxx . . $ ContactPoints(ece8e4539e0196f4).(const struct Edgecxx . . $ Edge# e1, const struct Edgecxx . . $ Edge# e2, const struct ClosestPointscxx . . $ ClosestPoints# points, struct cpCollisionInfocxx . . $ cpCollisionInfo# *info)
479 {
480 cpFloatcxx . . $ cpFloat# mindist = e1.rcxx . . $ Edge#r. + e2.rcxx . . $ Edge#r.;
481 if(points.dcxx . . $ ClosestPoints#d. <= mindist){
482 #ifdef DRAW_CLIP
483 ChipmunkDebugDrawFatSegment(e1.a.p, e1.b.p, e1.r, RGBAColor(0, 1, 0, 1), LAColor(0, 0));
484 ChipmunkDebugDrawFatSegment(e2.a.p, e2.b.p, e2.r, RGBAColor(1, 0, 0, 1), LAColor(0, 0));
485 #endif
486 cpVectcxx . . $ cpVect# n = info->ncxx . . $ cpCollisionInfo#n. = points.ncxx . . $ ClosestPoints#n.;
487
488 // Distances along the axis parallel to n
489 cpFloatcxx . . $ cpFloat# d_e1_a = cpvcrosscxx . . $ cpvcross(7d57c1c4dd559d53).(e1.acxx . . $ Edge#a..pcxx . . $ EdgePoint#p., n);
490 cpFloatcxx . . $ cpFloat# d_e1_b = cpvcrosscxx . . $ cpvcross(7d57c1c4dd559d53).(e1.bcxx . . $ Edge#b..pcxx . . $ EdgePoint#p., n);
491 cpFloatcxx . . $ cpFloat# d_e2_a = cpvcrosscxx . . $ cpvcross(7d57c1c4dd559d53).(e2.acxx . . $ Edge#a..pcxx . . $ EdgePoint#p., n);
492 cpFloatcxx . . $ cpFloat# d_e2_b = cpvcrosscxx . . $ cpvcross(7d57c1c4dd559d53).(e2.bcxx . . $ Edge#b..pcxx . . $ EdgePoint#p., n);
493
494 // TODO + min isn't a complete fix.
495 cpFloatcxx . . $ cpFloat# e1_denom = 1.0f/(d_e1_b - d_e1_a + CPFLOAT_MINcxx . . $ `include/chipmunk/chipmunk_types.h:78:10`!);
496 cpFloatcxx . . $ cpFloat# e2_denom = 1.0f/(d_e2_b - d_e2_a + CPFLOAT_MINcxx . . $ `include/chipmunk/chipmunk_types.h:78:10`!);
497
498 // Project the endpoints of the two edges onto the opposing edge, clamping them as necessary.
499 // Compare the projected points to the collision normal to see if the shapes overlap there.
500 {
501 cpVectcxx . . $ cpVect# p1 = cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(n, e1.rcxx . . $ Edge#r.), cpvlerpcxx . . $ cpvlerp(12b05c0b9acd6d4b).(e1.acxx . . $ Edge#a..pcxx . . $ EdgePoint#p., e1.bcxx . . $ Edge#b..pcxx . . $ EdgePoint#p., cpfclamp01cxx . . $ cpfclamp01(447c67c6676f75c3).((d_e2_b - d_e1_a)*e1_denom)));
502 cpVectcxx . . $ cpVect# p2 = cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(n, -e2.rcxx . . $ Edge#r.), cpvlerpcxx . . $ cpvlerp(12b05c0b9acd6d4b).(e2.acxx . . $ Edge#a..pcxx . . $ EdgePoint#p., e2.bcxx . . $ Edge#b..pcxx . . $ EdgePoint#p., cpfclamp01cxx . . $ cpfclamp01(447c67c6676f75c3).((d_e1_a - d_e2_a)*e2_denom)));
503 cpFloatcxx . . $ cpFloat# dist = cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(p2, p1), n);
504 if(dist <= 0.0f){
505 cpHashValuecxx . . $ cpHashValue# hash_1a2b = CP_HASH_PAIRcxx . . $ `include/chipmunk/chipmunk_private.h:29:9`!(e1.a.hash, e2.b.hash);
506 cpCollisionInfoPushContactcxx . . $ cpCollisionInfoPushContact(50f3b5096699c213).(info, p1, p2, hash_1a2b);
507 }
508 }{
509 cpVectcxx . . $ cpVect# p1 = cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(n, e1.rcxx . . $ Edge#r.), cpvlerpcxx . . $ cpvlerp(12b05c0b9acd6d4b).(e1.acxx . . $ Edge#a..pcxx . . $ EdgePoint#p., e1.bcxx . . $ Edge#b..pcxx . . $ EdgePoint#p., cpfclamp01cxx . . $ cpfclamp01(447c67c6676f75c3).((d_e2_a - d_e1_a)*e1_denom)));
510 cpVectcxx . . $ cpVect# p2 = cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(n, -e2.rcxx . . $ Edge#r.), cpvlerpcxx . . $ cpvlerp(12b05c0b9acd6d4b).(e2.acxx . . $ Edge#a..pcxx . . $ EdgePoint#p., e2.bcxx . . $ Edge#b..pcxx . . $ EdgePoint#p., cpfclamp01cxx . . $ cpfclamp01(447c67c6676f75c3).((d_e1_b - d_e2_a)*e2_denom)));
511 cpFloatcxx . . $ cpFloat# dist = cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(p2, p1), n);
512 if(dist <= 0.0f){
513 cpHashValuecxx . . $ cpHashValue# hash_1b2a = CP_HASH_PAIRcxx . . $ `include/chipmunk/chipmunk_private.h:29:9`!(e1.b.hash, e2.a.hash);
514 cpCollisionInfoPushContactcxx . . $ cpCollisionInfoPushContact(50f3b5096699c213).(info, p1, p2, hash_1b2a);
515 }
516 }
517 }
518 }
519
520 //MARK: Collision Functions
521
522 typedef void (*CollisionFunccxx . . $ CollisionFunc#)(const cpShapecxx . . $ cpShape# *a, const cpShapecxx . . $ cpShape# *b, struct cpCollisionInfocxx . . $ cpCollisionInfo# *info);
523
524 // Collide circle shapes.
525 static void
526 CircleToCirclecxx . . $ CircleToCircle(7f3ccef8fc51a5ed).(const cpCircleShapecxx . . $ cpCircleShape# *c1, const cpCircleShapecxx . . $ cpCircleShape# *c2, struct cpCollisionInfocxx . . $ cpCollisionInfo# *info)
527 {
528 cpFloatcxx . . $ cpFloat# mindist = c1->rcxx . . $ cpCircleShape#r. + c2->rcxx . . $ cpCircleShape#r.;
529 cpVectcxx . . $ cpVect# delta = cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(c2->tccxx . . $ cpCircleShape#tc., c1->tccxx . . $ cpCircleShape#tc.);
530 cpFloatcxx . . $ cpFloat# distsq = cpvlengthsqcxx . . $ cpvlengthsq(c4ef53c170268e01).(delta);
531
532 if(distsq < mindist*mindist){
533 cpFloatcxx . . $ cpFloat# dist = cpfsqrtcxx . . $ `include/chipmunk/chipmunk_types.h:68:10`!(distsq);
534 cpVectcxx . . $ cpVect# n = info->ncxx . . $ cpCollisionInfo#n. = (dist ? cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(delta, 1.0f/dist) : cpvcxx . . $ cpv(3142d57d978b2176).(1.0f, 0.0f));
535 cpCollisionInfoPushContactcxx . . $ cpCollisionInfoPushContact(50f3b5096699c213).(info, cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(c1->tccxx . . $ cpCircleShape#tc., cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(n, c1->rcxx . . $ cpCircleShape#r.)), cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(c2->tccxx . . $ cpCircleShape#tc., cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(n, -c2->rcxx . . $ cpCircleShape#r.)), 0);
536 }
537 }
538
539 static void
540 CircleToSegmentcxx . . $ CircleToSegment(13c025ed2687a07a).(const cpCircleShapecxx . . $ cpCircleShape# *circle, const cpSegmentShapecxx . . $ cpSegmentShape# *segment, struct cpCollisionInfocxx . . $ cpCollisionInfo# *info)
541 {
542 cpVectcxx . . $ cpVect# seg_a = segment->tacxx . . $ cpSegmentShape#ta.;
543 cpVectcxx . . $ cpVect# seg_b = segment->tbcxx . . $ cpSegmentShape#tb.;
544 cpVectcxx . . $ cpVect# center = circle->tccxx . . $ cpCircleShape#tc.;
545
546 // Find the closest point on the segment to the circle.
547 cpVectcxx . . $ cpVect# seg_delta = cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(seg_b, seg_a);
548 cpFloatcxx . . $ cpFloat# closest_t = cpfclamp01cxx . . $ cpfclamp01(447c67c6676f75c3).(cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(seg_delta, cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(center, seg_a))/cpvlengthsqcxx . . $ cpvlengthsq(c4ef53c170268e01).(seg_delta));
549 cpVectcxx . . $ cpVect# closest = cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(seg_a, cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(seg_delta, closest_t));
550
551 // Compare the radii of the two shapes to see if they are colliding.
552 cpFloatcxx . . $ cpFloat# mindist = circle->rcxx . . $ cpCircleShape#r. + segment->rcxx . . $ cpSegmentShape#r.;
553 cpVectcxx . . $ cpVect# delta = cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(closest, center);
554 cpFloatcxx . . $ cpFloat# distsq = cpvlengthsqcxx . . $ cpvlengthsq(c4ef53c170268e01).(delta);
555 if(distsq < mindist*mindist){
556 cpFloatcxx . . $ cpFloat# dist = cpfsqrtcxx . . $ `include/chipmunk/chipmunk_types.h:68:10`!(distsq);
557 // Handle coincident shapes as gracefully as possible.
558 cpVectcxx . . $ cpVect# n = info->ncxx . . $ cpCollisionInfo#n. = (dist ? cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(delta, 1.0f/dist) : segment->tncxx . . $ cpSegmentShape#tn.);
559
560 // Reject endcap collisions if tangents are provided.
561 cpVectcxx . . $ cpVect# rot = cpBodyGetRotationcxx . . $ cpBodyGetRotation(2e8d436bcfa5d74b).(segment->shapecxx . . $ cpSegmentShape#shape..bodycxx . . $ cpShape#body.);
562 if(
563 (closest_t != 0.0f || cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(n, cpvrotatecxx . . $ cpvrotate(47eb3f105675e23c).(segment->a_tangentcxx . . $ cpSegmentShape#a_tangent., rot)) >= 0.0) &&
564 (closest_t != 1.0f || cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(n, cpvrotatecxx . . $ cpvrotate(47eb3f105675e23c).(segment->b_tangentcxx . . $ cpSegmentShape#b_tangent., rot)) >= 0.0)
565 ){
566 cpCollisionInfoPushContactcxx . . $ cpCollisionInfoPushContact(50f3b5096699c213).(info, cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(center, cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(n, circle->rcxx . . $ cpCircleShape#r.)), cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(closest, cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(n, -segment->rcxx . . $ cpSegmentShape#r.)), 0);
567 }
568 }
569 }
570
571 static void
572 SegmentToSegmentcxx . . $ SegmentToSegment(15c5e00f6045a82b).(const cpSegmentShapecxx . . $ cpSegmentShape# *seg1, const cpSegmentShapecxx . . $ cpSegmentShape# *seg2, struct cpCollisionInfocxx . . $ cpCollisionInfo# *info)
573 {
574 struct SupportContextcxx . . $ SupportContext# context = {(cpShapecxx . . $ cpShape# *)seg1, (cpShapecxx . . $ cpShape# *)seg2, (SupportPointFunccxx . . $ SupportPointFunc#)SegmentSupportPointcxx . . $ SegmentSupportPoint(2f0dea3f8e488fca)., (SupportPointFunccxx . . $ SupportPointFunc#)SegmentSupportPointcxx . . $ SegmentSupportPoint(2f0dea3f8e488fca).};
575 struct ClosestPointscxx . . $ ClosestPoints# points = GJKcxx . . $ GJK(af16ae11255c80f5).(&context, &info->idcxx . . $ cpCollisionInfo#id.);
576
577 #if DRAW_CLOSEST
578 #if PRINT_LOG
579 // ChipmunkDemoPrintString("Distance: %.2f\n", points.d);
580 #endif
581
582 ChipmunkDebugDrawDot(6.0, points.a, RGBAColor(1, 1, 1, 1));
583 ChipmunkDebugDrawDot(6.0, points.b, RGBAColor(1, 1, 1, 1));
584 ChipmunkDebugDrawSegment(points.a, points.b, RGBAColor(1, 1, 1, 1));
585 ChipmunkDebugDrawSegment(points.a, cpvadd(points.a, cpvmult(points.n, 10.0)), RGBAColor(1, 0, 0, 1));
586 #endif
587
588 cpVectcxx . . $ cpVect# n = points.ncxx . . $ ClosestPoints#n.;
589 cpVectcxx . . $ cpVect# rot1 = cpBodyGetRotationcxx . . $ cpBodyGetRotation(2e8d436bcfa5d74b).(seg1->shapecxx . . $ cpSegmentShape#shape..bodycxx . . $ cpShape#body.);
590 cpVectcxx . . $ cpVect# rot2 = cpBodyGetRotationcxx . . $ cpBodyGetRotation(2e8d436bcfa5d74b).(seg2->shapecxx . . $ cpSegmentShape#shape..bodycxx . . $ cpShape#body.);
591
592 // If the closest points are nearer than the sum of the radii...
593 if(
594 points.dcxx . . $ ClosestPoints#d. <= (seg1->rcxx . . $ cpSegmentShape#r. + seg2->rcxx . . $ cpSegmentShape#r.) && (
595 // Reject endcap collisions if tangents are provided.
596 (!cpveqlcxx . . $ cpveql(c42fe4e993c6be46).(points.acxx . . $ ClosestPoints#a., seg1->tacxx . . $ cpSegmentShape#ta.) || cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(n, cpvrotatecxx . . $ cpvrotate(47eb3f105675e23c).(seg1->a_tangentcxx . . $ cpSegmentShape#a_tangent., rot1)) <= 0.0) &&
597 (!cpveqlcxx . . $ cpveql(c42fe4e993c6be46).(points.acxx . . $ ClosestPoints#a., seg1->tbcxx . . $ cpSegmentShape#tb.) || cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(n, cpvrotatecxx . . $ cpvrotate(47eb3f105675e23c).(seg1->b_tangentcxx . . $ cpSegmentShape#b_tangent., rot1)) <= 0.0) &&
598 (!cpveqlcxx . . $ cpveql(c42fe4e993c6be46).(points.bcxx . . $ ClosestPoints#b., seg2->tacxx . . $ cpSegmentShape#ta.) || cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(n, cpvrotatecxx . . $ cpvrotate(47eb3f105675e23c).(seg2->a_tangentcxx . . $ cpSegmentShape#a_tangent., rot2)) >= 0.0) &&
599 (!cpveqlcxx . . $ cpveql(c42fe4e993c6be46).(points.bcxx . . $ ClosestPoints#b., seg2->tbcxx . . $ cpSegmentShape#tb.) || cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(n, cpvrotatecxx . . $ cpvrotate(47eb3f105675e23c).(seg2->b_tangentcxx . . $ cpSegmentShape#b_tangent., rot2)) >= 0.0)
600 )
601 ){
602 ContactPointscxx . . $ ContactPoints(ece8e4539e0196f4).(SupportEdgeForSegmentcxx . . $ SupportEdgeForSegment(5f9fea087ac45bd5).(seg1, n), SupportEdgeForSegmentcxx . . $ SupportEdgeForSegment(5f9fea087ac45bd5).(seg2, cpvnegcxx . . $ cpvneg(e06142a62109ae08).(n)), points, info);
603 }
604 }
605
606 static void
607 PolyToPolycxx . . $ PolyToPoly(7ed07d74a5b08f5a).(const cpPolyShapecxx . . $ cpPolyShape# *poly1, const cpPolyShapecxx . . $ cpPolyShape# *poly2, struct cpCollisionInfocxx . . $ cpCollisionInfo# *info)
608 {
609 struct SupportContextcxx . . $ SupportContext# context = {(cpShapecxx . . $ cpShape# *)poly1, (cpShapecxx . . $ cpShape# *)poly2, (SupportPointFunccxx . . $ SupportPointFunc#)PolySupportPointcxx . . $ PolySupportPoint(25638014a02645e7)., (SupportPointFunccxx . . $ SupportPointFunc#)PolySupportPointcxx . . $ PolySupportPoint(25638014a02645e7).};
610 struct ClosestPointscxx . . $ ClosestPoints# points = GJKcxx . . $ GJK(af16ae11255c80f5).(&context, &info->idcxx . . $ cpCollisionInfo#id.);
611
612 #if DRAW_CLOSEST
613 #if PRINT_LOG
614 // ChipmunkDemoPrintString("Distance: %.2f\n", points.d);
615 #endif
616
617 ChipmunkDebugDrawDot(3.0, points.a, RGBAColor(1, 1, 1, 1));
618 ChipmunkDebugDrawDot(3.0, points.b, RGBAColor(1, 1, 1, 1));
619 ChipmunkDebugDrawSegment(points.a, points.b, RGBAColor(1, 1, 1, 1));
620 ChipmunkDebugDrawSegment(points.a, cpvadd(points.a, cpvmult(points.n, 10.0)), RGBAColor(1, 0, 0, 1));
621 #endif
622
623 // If the closest points are nearer than the sum of the radii...
624 if(points.dcxx . . $ ClosestPoints#d. - poly1->rcxx . . $ cpPolyShape#r. - poly2->rcxx . . $ cpPolyShape#r. <= 0.0){
625 ContactPointscxx . . $ ContactPoints(ece8e4539e0196f4).(SupportEdgeForPolycxx . . $ SupportEdgeForPoly(428407c579fd435).(poly1, points.ncxx . . $ ClosestPoints#n.), SupportEdgeForPolycxx . . $ SupportEdgeForPoly(428407c579fd435).(poly2, cpvnegcxx . . $ cpvneg(e06142a62109ae08).(points.ncxx . . $ ClosestPoints#n.)), points, info);
626 }
627 }
628
629 static void
630 SegmentToPolycxx . . $ SegmentToPoly(e1c9485c82a14300).(const cpSegmentShapecxx . . $ cpSegmentShape# *seg, const cpPolyShapecxx . . $ cpPolyShape# *poly, struct cpCollisionInfocxx . . $ cpCollisionInfo# *info)
631 {
632 struct SupportContextcxx . . $ SupportContext# context = {(cpShapecxx . . $ cpShape# *)seg, (cpShapecxx . . $ cpShape# *)poly, (SupportPointFunccxx . . $ SupportPointFunc#)SegmentSupportPointcxx . . $ SegmentSupportPoint(2f0dea3f8e488fca)., (SupportPointFunccxx . . $ SupportPointFunc#)PolySupportPointcxx . . $ PolySupportPoint(25638014a02645e7).};
633 struct ClosestPointscxx . . $ ClosestPoints# points = GJKcxx . . $ GJK(af16ae11255c80f5).(&context, &info->idcxx . . $ cpCollisionInfo#id.);
634
635 #if DRAW_CLOSEST
636 #if PRINT_LOG
637 // ChipmunkDemoPrintString("Distance: %.2f\n", points.d);
638 #endif
639
640 ChipmunkDebugDrawDot(3.0, points.a, RGBAColor(1, 1, 1, 1));
641 ChipmunkDebugDrawDot(3.0, points.b, RGBAColor(1, 1, 1, 1));
642 ChipmunkDebugDrawSegment(points.a, points.b, RGBAColor(1, 1, 1, 1));
643 ChipmunkDebugDrawSegment(points.a, cpvadd(points.a, cpvmult(points.n, 10.0)), RGBAColor(1, 0, 0, 1));
644 #endif
645
646 cpVectcxx . . $ cpVect# n = points.ncxx . . $ ClosestPoints#n.;
647 cpVectcxx . . $ cpVect# rot = cpBodyGetRotationcxx . . $ cpBodyGetRotation(2e8d436bcfa5d74b).(seg->shapecxx . . $ cpSegmentShape#shape..bodycxx . . $ cpShape#body.);
648
649 if(
650 // If the closest points are nearer than the sum of the radii...
651 points.dcxx . . $ ClosestPoints#d. - seg->rcxx . . $ cpSegmentShape#r. - poly->rcxx . . $ cpPolyShape#r. <= 0.0 && (
652 // Reject endcap collisions if tangents are provided.
653 (!cpveqlcxx . . $ cpveql(c42fe4e993c6be46).(points.acxx . . $ ClosestPoints#a., seg->tacxx . . $ cpSegmentShape#ta.) || cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(n, cpvrotatecxx . . $ cpvrotate(47eb3f105675e23c).(seg->a_tangentcxx . . $ cpSegmentShape#a_tangent., rot)) <= 0.0) &&
654 (!cpveqlcxx . . $ cpveql(c42fe4e993c6be46).(points.acxx . . $ ClosestPoints#a., seg->tbcxx . . $ cpSegmentShape#tb.) || cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(n, cpvrotatecxx . . $ cpvrotate(47eb3f105675e23c).(seg->b_tangentcxx . . $ cpSegmentShape#b_tangent., rot)) <= 0.0)
655 )
656 ){
657 ContactPointscxx . . $ ContactPoints(ece8e4539e0196f4).(SupportEdgeForSegmentcxx . . $ SupportEdgeForSegment(5f9fea087ac45bd5).(seg, n), SupportEdgeForPolycxx . . $ SupportEdgeForPoly(428407c579fd435).(poly, cpvnegcxx . . $ cpvneg(e06142a62109ae08).(n)), points, info);
658 }
659 }
660
661 static void
662 CircleToPolycxx . . $ CircleToPoly(78160177ba70bd84).(const cpCircleShapecxx . . $ cpCircleShape# *circle, const cpPolyShapecxx . . $ cpPolyShape# *poly, struct cpCollisionInfocxx . . $ cpCollisionInfo# *info)
663 {
664 struct SupportContextcxx . . $ SupportContext# context = {(cpShapecxx . . $ cpShape# *)circle, (cpShapecxx . . $ cpShape# *)poly, (SupportPointFunccxx . . $ SupportPointFunc#)CircleSupportPointcxx . . $ CircleSupportPoint(4888c039b288898e)., (SupportPointFunccxx . . $ SupportPointFunc#)PolySupportPointcxx . . $ PolySupportPoint(25638014a02645e7).};
665 struct ClosestPointscxx . . $ ClosestPoints# points = GJKcxx . . $ GJK(af16ae11255c80f5).(&context, &info->idcxx . . $ cpCollisionInfo#id.);
666
667 #if DRAW_CLOSEST
668 ChipmunkDebugDrawDot(3.0, points.a, RGBAColor(1, 1, 1, 1));
669 ChipmunkDebugDrawDot(3.0, points.b, RGBAColor(1, 1, 1, 1));
670 ChipmunkDebugDrawSegment(points.a, points.b, RGBAColor(1, 1, 1, 1));
671 ChipmunkDebugDrawSegment(points.a, cpvadd(points.a, cpvmult(points.n, 10.0)), RGBAColor(1, 0, 0, 1));
672 #endif
673
674 // If the closest points are nearer than the sum of the radii...
675 if(points.dcxx . . $ ClosestPoints#d. <= circle->rcxx . . $ cpCircleShape#r. + poly->rcxx . . $ cpPolyShape#r.){
676 cpVectcxx . . $ cpVect# n = info->ncxx . . $ cpCollisionInfo#n. = points.ncxx . . $ ClosestPoints#n.;
677 cpCollisionInfoPushContactcxx . . $ cpCollisionInfoPushContact(50f3b5096699c213).(info, cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(points.acxx . . $ ClosestPoints#a., cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(n, circle->rcxx . . $ cpCircleShape#r.)), cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(points.bcxx . . $ ClosestPoints#b., cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(n, poly->rcxx . . $ cpPolyShape#r.)), 0);
678 }
679 }
680
681 static void
682 CollisionErrorcxx . . $ CollisionError(b2269012d39ea966).(const cpShapecxx . . $ cpShape# *circle, const cpShapecxx . . $ cpShape# *poly, struct cpCollisionInfocxx . . $ cpCollisionInfo# *info)
683 {
684 cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpFalsecxx . . $ `include/chipmunk/chipmunk_types.h:188:10`!, "Internal Error: Shape types are not sorted.");
685 }
686
687
688 static const CollisionFunccxx . . $ CollisionFunc# BuiltinCollisionFuncscxx . . $ BuiltinCollisionFuncs.[9] = {
689 (CollisionFunccxx . . $ CollisionFunc#)CircleToCirclecxx . . $ CircleToCircle(7f3ccef8fc51a5ed).,
690 CollisionErrorcxx . . $ CollisionError(b2269012d39ea966).,
691 CollisionErrorcxx . . $ CollisionError(b2269012d39ea966).,
692 (CollisionFunccxx . . $ CollisionFunc#)CircleToSegmentcxx . . $ CircleToSegment(13c025ed2687a07a).,
693 (CollisionFunccxx . . $ CollisionFunc#)SegmentToSegmentcxx . . $ SegmentToSegment(15c5e00f6045a82b).,
694 CollisionErrorcxx . . $ CollisionError(b2269012d39ea966).,
695 (CollisionFunccxx . . $ CollisionFunc#)CircleToPolycxx . . $ CircleToPoly(78160177ba70bd84).,
696 (CollisionFunccxx . . $ CollisionFunc#)SegmentToPolycxx . . $ SegmentToPoly(e1c9485c82a14300).,
697 (CollisionFunccxx . . $ CollisionFunc#)PolyToPolycxx . . $ PolyToPoly(7ed07d74a5b08f5a).,
698 };
699 static const CollisionFunccxx . . $ CollisionFunc# *CollisionFuncscxx . . $ CollisionFuncs. = BuiltinCollisionFuncscxx . . $ BuiltinCollisionFuncs.;
700
701 struct cpCollisionInfocxx . . $ cpCollisionInfo#
702 cpCollidecxx . . $ cpCollide(f8bb8df86a1fa46f).(const cpShapecxx . . $ cpShape# *a, const cpShapecxx . . $ cpShape# *b, cpCollisionIDcxx . . $ cpCollisionID# id, struct cpContactcxx . . $ cpContact# *contacts)
703 {
704 struct cpCollisionInfocxx . . $ cpCollisionInfo# info = {a, b, id, cpvzerocxx . . $ cpvzero., 0, contacts};
705
706 // Make sure the shape types are in order.
707 if(a->klasscxx . . $ cpShape#klass.->typecxx . . $ cpShapeClass#type. > b->klasscxx . . $ cpShape#klass.->typecxx . . $ cpShapeClass#type.){
708 info.acxx . . $ cpCollisionInfo#a. = b;
709 info.bcxx . . $ cpCollisionInfo#b. = a;
710 }
711
712 CollisionFuncscxx . . $ CollisionFuncs.[info.acxx . . $ cpCollisionInfo#a.->klasscxx . . $ cpShape#klass.->typecxx . . $ cpShapeClass#type. + info.bcxx . . $ cpCollisionInfo#b.->klasscxx . . $ cpShape#klass.->typecxx . . $ cpShapeClass#type.*CP_NUM_SHAPEScxx . . $ CP_NUM_SHAPES.](info.acxx . . $ cpCollisionInfo#a., info.bcxx . . $ cpCollisionInfo#b., &info);
713
714 // if(0){
715 // for(int i=0; i<info.count; i++){
716 // cpVect r1 = info.arr[i].r1;
717 // cpVect r2 = info.arr[i].r2;
718 // cpVect mid = cpvlerp(r1, r2, 0.5f);
719 //
720 // ChipmunkDebugDrawSegment(r1, mid, RGBAColor(1, 0, 0, 1));
721 // ChipmunkDebugDrawSegment(r2, mid, RGBAColor(0, 0, 1, 1));
722 // }
723 // }
724
725 return info;
726 }