box2d-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. data/.gitignore +17 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE.txt +22 -0
  4. data/README.md +29 -0
  5. data/Rakefile +1 -0
  6. data/box2d-rails.gemspec +20 -0
  7. data/lib/box2d-rails.rb +8 -0
  8. data/lib/box2d-rails/version.rb +5 -0
  9. data/vendor/assets/javascripts/box2d/collision/ClipVertex.js +35 -0
  10. data/vendor/assets/javascripts/box2d/collision/Features.js +61 -0
  11. data/vendor/assets/javascripts/box2d/collision/b2AABB.js +45 -0
  12. data/vendor/assets/javascripts/box2d/collision/b2Bound.js +43 -0
  13. data/vendor/assets/javascripts/box2d/collision/b2BoundValues.js +31 -0
  14. data/vendor/assets/javascripts/box2d/collision/b2BroadPhase.js +898 -0
  15. data/vendor/assets/javascripts/box2d/collision/b2BufferedPair.js +26 -0
  16. data/vendor/assets/javascripts/box2d/collision/b2Collision.js +738 -0
  17. data/vendor/assets/javascripts/box2d/collision/b2ContactID.js +52 -0
  18. data/vendor/assets/javascripts/box2d/collision/b2ContactPoint.js +35 -0
  19. data/vendor/assets/javascripts/box2d/collision/b2Distance.js +333 -0
  20. data/vendor/assets/javascripts/box2d/collision/b2Manifold.js +34 -0
  21. data/vendor/assets/javascripts/box2d/collision/b2OBB.js +34 -0
  22. data/vendor/assets/javascripts/box2d/collision/b2Pair.js +60 -0
  23. data/vendor/assets/javascripts/box2d/collision/b2PairCallback.js +34 -0
  24. data/vendor/assets/javascripts/box2d/collision/b2PairManager.js +386 -0
  25. data/vendor/assets/javascripts/box2d/collision/b2Proxy.js +40 -0
  26. data/vendor/assets/javascripts/box2d/collision/shapes/b2BoxDef.js +49 -0
  27. data/vendor/assets/javascripts/box2d/collision/shapes/b2CircleDef.js +49 -0
  28. data/vendor/assets/javascripts/box2d/collision/shapes/b2CircleShape.js +198 -0
  29. data/vendor/assets/javascripts/box2d/collision/shapes/b2MassData.js +36 -0
  30. data/vendor/assets/javascripts/box2d/collision/shapes/b2PolyDef.js +58 -0
  31. data/vendor/assets/javascripts/box2d/collision/shapes/b2PolyShape.js +492 -0
  32. data/vendor/assets/javascripts/box2d/collision/shapes/b2Shape.js +339 -0
  33. data/vendor/assets/javascripts/box2d/collision/shapes/b2ShapeDef.js +109 -0
  34. data/vendor/assets/javascripts/box2d/common/b2Settings.js +72 -0
  35. data/vendor/assets/javascripts/box2d/common/math/b2Mat22.js +130 -0
  36. data/vendor/assets/javascripts/box2d/common/math/b2Math.js +218 -0
  37. data/vendor/assets/javascripts/box2d/common/math/b2Vec2.js +131 -0
  38. data/vendor/assets/javascripts/box2d/dynamics/b2Body.js +469 -0
  39. data/vendor/assets/javascripts/box2d/dynamics/b2BodyDef.js +69 -0
  40. data/vendor/assets/javascripts/box2d/dynamics/b2CollisionFilter.js +42 -0
  41. data/vendor/assets/javascripts/box2d/dynamics/b2ContactManager.js +337 -0
  42. data/vendor/assets/javascripts/box2d/dynamics/b2Island.js +331 -0
  43. data/vendor/assets/javascripts/box2d/dynamics/b2TimeStep.js +27 -0
  44. data/vendor/assets/javascripts/box2d/dynamics/b2World.js +522 -0
  45. data/vendor/assets/javascripts/box2d/dynamics/b2WorldListener.js +52 -0
  46. data/vendor/assets/javascripts/box2d/dynamics/contacts/b2CircleContact.js +102 -0
  47. data/vendor/assets/javascripts/box2d/dynamics/contacts/b2Conservative.js +228 -0
  48. data/vendor/assets/javascripts/box2d/dynamics/contacts/b2Contact.js +201 -0
  49. data/vendor/assets/javascripts/box2d/dynamics/contacts/b2ContactConstraint.js +45 -0
  50. data/vendor/assets/javascripts/box2d/dynamics/contacts/b2ContactConstraintPoint.js +40 -0
  51. data/vendor/assets/javascripts/box2d/dynamics/contacts/b2ContactNode.js +33 -0
  52. data/vendor/assets/javascripts/box2d/dynamics/contacts/b2ContactRegister.js +30 -0
  53. data/vendor/assets/javascripts/box2d/dynamics/contacts/b2ContactSolver.js +537 -0
  54. data/vendor/assets/javascripts/box2d/dynamics/contacts/b2NullContact.js +65 -0
  55. data/vendor/assets/javascripts/box2d/dynamics/contacts/b2PolyAndCircleContact.js +103 -0
  56. data/vendor/assets/javascripts/box2d/dynamics/contacts/b2PolyContact.js +163 -0
  57. data/vendor/assets/javascripts/box2d/dynamics/joints/b2DistanceJoint.js +264 -0
  58. data/vendor/assets/javascripts/box2d/dynamics/joints/b2DistanceJointDef.js +49 -0
  59. data/vendor/assets/javascripts/box2d/dynamics/joints/b2GearJoint.js +307 -0
  60. data/vendor/assets/javascripts/box2d/dynamics/joints/b2GearJointDef.js +50 -0
  61. data/vendor/assets/javascripts/box2d/dynamics/joints/b2Jacobian.js +49 -0
  62. data/vendor/assets/javascripts/box2d/dynamics/joints/b2Joint.js +200 -0
  63. data/vendor/assets/javascripts/box2d/dynamics/joints/b2JointDef.js +40 -0
  64. data/vendor/assets/javascripts/box2d/dynamics/joints/b2JointNode.js +33 -0
  65. data/vendor/assets/javascripts/box2d/dynamics/joints/b2MouseJoint.js +234 -0
  66. data/vendor/assets/javascripts/box2d/dynamics/joints/b2MouseJointDef.js +53 -0
  67. data/vendor/assets/javascripts/box2d/dynamics/joints/b2PrismaticJoint.js +676 -0
  68. data/vendor/assets/javascripts/box2d/dynamics/joints/b2PrismaticJointDef.js +56 -0
  69. data/vendor/assets/javascripts/box2d/dynamics/joints/b2PulleyJoint.js +618 -0
  70. data/vendor/assets/javascripts/box2d/dynamics/joints/b2PulleyJointDef.js +70 -0
  71. data/vendor/assets/javascripts/box2d/dynamics/joints/b2RevoluteJoint.js +491 -0
  72. data/vendor/assets/javascripts/box2d/dynamics/joints/b2RevoluteJointDef.js +55 -0
  73. metadata +133 -0
@@ -0,0 +1,26 @@
1
+ /*
2
+ * Copyright (c) 2006-2007 Erin Catto http:
3
+ *
4
+ * This software is provided 'as-is', without any express or implied
5
+ * warranty. In no event will the authors be held liable for any damages
6
+ * arising from the use of this software.
7
+ * Permission is granted to anyone to use this software for any purpose,
8
+ * including commercial applications, and to alter it and redistribute it
9
+ * freely, subject to the following restrictions:
10
+ * 1. The origin of this software must not be misrepresented; you must not
11
+ * claim that you wrote the original software. If you use this software
12
+ * in a product, an acknowledgment in the product documentation would be
13
+ * appreciated but is not required.
14
+ * 2. Altered source versions must be plainly marked, and must not be
15
+ * misrepresented the original software.
16
+ * 3. This notice may not be removed or altered from any source distribution.
17
+ */
18
+
19
+
20
+
21
+ var b2BufferedPair = Class.create();
22
+ b2BufferedPair.prototype = {
23
+ proxyId1: 0,
24
+ proxyId2: 0,
25
+
26
+ initialize: function() {}}
@@ -0,0 +1,738 @@
1
+ /*
2
+ * Copyright (c) 2006-2007 Erin Catto http:
3
+ *
4
+ * This software is provided 'as-is', without any express or implied
5
+ * warranty. In no event will the authors be held liable for any damages
6
+ * arising from the use of this software.
7
+ * Permission is granted to anyone to use this software for any purpose,
8
+ * including commercial applications, and to alter it and redistribute it
9
+ * freely, subject to the following restrictions:
10
+ * 1. The origin of this software must not be misrepresented; you must not
11
+ * claim that you wrote the original software. If you use this software
12
+ * in a product, an acknowledgment in the product documentation would be
13
+ * appreciated but is not required.
14
+ * 2. Altered source versions must be plainly marked, and must not be
15
+ * misrepresented the original software.
16
+ * 3. This notice may not be removed or altered from any source distribution.
17
+ */
18
+
19
+
20
+
21
+
22
+ var b2Collision = Class.create();
23
+ b2Collision.prototype = {
24
+
25
+ // Null feature
26
+
27
+
28
+
29
+
30
+ // Find the separation between poly1 and poly2 for a give edge normal on poly1.
31
+
32
+
33
+
34
+
35
+ // Find the max separation between poly1 and poly2 using edge normals
36
+ // from poly1.
37
+
38
+
39
+
40
+
41
+
42
+
43
+
44
+ // Find edge normal of max separation on A - return if separating axis is found
45
+ // Find edge normal of max separation on B - return if separation axis is found
46
+ // Choose reference edge(minA, minB)
47
+ // Find incident edge
48
+ // Clip
49
+ // The normal points from 1 to 2
50
+
51
+
52
+
53
+
54
+
55
+
56
+
57
+
58
+
59
+
60
+
61
+
62
+
63
+
64
+
65
+ initialize: function() {}}
66
+ b2Collision.b2_nullFeature = 0x000000ff;
67
+ b2Collision.ClipSegmentToLine = function(vOut, vIn, normal, offset)
68
+ {
69
+ // Start with no output points
70
+ var numOut = 0;
71
+
72
+ var vIn0 = vIn[0].v;
73
+ var vIn1 = vIn[1].v;
74
+
75
+ // Calculate the distance of end points to the line
76
+ var distance0 = b2Math.b2Dot(normal, vIn[0].v) - offset;
77
+ var distance1 = b2Math.b2Dot(normal, vIn[1].v) - offset;
78
+
79
+ // If the points are behind the plane
80
+ if (distance0 <= 0.0) vOut[numOut++] = vIn[0];
81
+ if (distance1 <= 0.0) vOut[numOut++] = vIn[1];
82
+
83
+ // If the points are on different sides of the plane
84
+ if (distance0 * distance1 < 0.0)
85
+ {
86
+ // Find intersection point of edge and plane
87
+ var interp = distance0 / (distance0 - distance1);
88
+ // expanded for performance
89
+ var tVec = vOut[numOut].v;
90
+ tVec.x = vIn0.x + interp * (vIn1.x - vIn0.x);
91
+ tVec.y = vIn0.y + interp * (vIn1.y - vIn0.y);
92
+ if (distance0 > 0.0)
93
+ {
94
+ vOut[numOut].id = vIn[0].id;
95
+ }
96
+ else
97
+ {
98
+ vOut[numOut].id = vIn[1].id;
99
+ }
100
+ ++numOut;
101
+ }
102
+
103
+ return numOut;
104
+ };
105
+ b2Collision.EdgeSeparation = function(poly1, edge1, poly2)
106
+ {
107
+ var vert1s = poly1.m_vertices;
108
+ var count2 = poly2.m_vertexCount;
109
+ var vert2s = poly2.m_vertices;
110
+
111
+ // Convert normal from into poly2's frame.
112
+ //b2Settings.b2Assert(edge1 < poly1.m_vertexCount);
113
+
114
+ //var normal = b2Math.b2MulMV(poly1.m_R, poly1->m_normals[edge1]);
115
+ var normalX = poly1.m_normals[edge1].x;
116
+ var normalY = poly1.m_normals[edge1].y;
117
+ var tX = normalX;
118
+ var tMat = poly1.m_R;
119
+ normalX = tMat.col1.x * tX + tMat.col2.x * normalY;
120
+ normalY = tMat.col1.y * tX + tMat.col2.y * normalY;
121
+ // ^^^^^^^ normal.MulM(poly1.m_R);
122
+
123
+ //var normalLocal2 = b2Math.b2MulTMV(poly2.m_R, normal);
124
+ var normalLocal2X = normalX;
125
+ var normalLocal2Y = normalY;
126
+ tMat = poly2.m_R;
127
+ tX = normalLocal2X * tMat.col1.x + normalLocal2Y * tMat.col1.y;
128
+ normalLocal2Y = normalLocal2X * tMat.col2.x + normalLocal2Y * tMat.col2.y;
129
+ normalLocal2X = tX;
130
+ // ^^^^^ normalLocal2.MulTM(poly2.m_R);
131
+
132
+ // Find support vertex on poly2 for -normal.
133
+ var vertexIndex2 = 0;
134
+ var minDot = Number.MAX_VALUE;
135
+ for (var i = 0; i < count2; ++i)
136
+ {
137
+ //var dot = b2Math.b2Dot(vert2s[i], normalLocal2);
138
+ var tVec = vert2s[i];
139
+ var dot = tVec.x * normalLocal2X + tVec.y * normalLocal2Y;
140
+ if (dot < minDot)
141
+ {
142
+ minDot = dot;
143
+ vertexIndex2 = i;
144
+ }
145
+ }
146
+
147
+ //b2Vec2 v1 = poly1->m_position + b2Mul(poly1->m_R, vert1s[edge1]);
148
+ tMat = poly1.m_R;
149
+ var v1X = poly1.m_position.x + (tMat.col1.x * vert1s[edge1].x + tMat.col2.x * vert1s[edge1].y)
150
+ var v1Y = poly1.m_position.y + (tMat.col1.y * vert1s[edge1].x + tMat.col2.y * vert1s[edge1].y)
151
+
152
+ //b2Vec2 v2 = poly2->m_position + b2Mul(poly2->m_R, vert2s[vertexIndex2]);
153
+ tMat = poly2.m_R;
154
+ var v2X = poly2.m_position.x + (tMat.col1.x * vert2s[vertexIndex2].x + tMat.col2.x * vert2s[vertexIndex2].y)
155
+ var v2Y = poly2.m_position.y + (tMat.col1.y * vert2s[vertexIndex2].x + tMat.col2.y * vert2s[vertexIndex2].y)
156
+
157
+ //var separation = b2Math.b2Dot( b2Math.SubtractVV( v2, v1 ) , normal);
158
+ v2X -= v1X;
159
+ v2Y -= v1Y;
160
+ //var separation = b2Math.b2Dot( v2 , normal);
161
+ var separation = v2X * normalX + v2Y * normalY;
162
+ return separation;
163
+ };
164
+ b2Collision.FindMaxSeparation = function(edgeIndex /*int ptr*/, poly1, poly2, conservative)
165
+ {
166
+ var count1 = poly1.m_vertexCount;
167
+
168
+ // Vector pointing from the origin of poly1 to the origin of poly2.
169
+ //var d = b2Math.SubtractVV( poly2.m_position, poly1.m_position );
170
+ var dX = poly2.m_position.x - poly1.m_position.x;
171
+ var dY = poly2.m_position.y - poly1.m_position.y;
172
+
173
+ //var dLocal1 = b2Math.b2MulTMV(poly1.m_R, d);
174
+ var dLocal1X = (dX * poly1.m_R.col1.x + dY * poly1.m_R.col1.y);
175
+ var dLocal1Y = (dX * poly1.m_R.col2.x + dY * poly1.m_R.col2.y);
176
+
177
+ // Get support vertex hint for our search
178
+ var edge = 0;
179
+ var maxDot = -Number.MAX_VALUE;
180
+ for (var i = 0; i < count1; ++i)
181
+ {
182
+ //var dot = b2Math.b2Dot(poly.m_normals[i], dLocal1);
183
+ var dot = (poly1.m_normals[i].x * dLocal1X + poly1.m_normals[i].y * dLocal1Y);
184
+ if (dot > maxDot)
185
+ {
186
+ maxDot = dot;
187
+ edge = i;
188
+ }
189
+ }
190
+
191
+ // Get the separation for the edge normal.
192
+ var s = b2Collision.EdgeSeparation(poly1, edge, poly2);
193
+ if (s > 0.0 && conservative == false)
194
+ {
195
+ return s;
196
+ }
197
+
198
+ // Check the separation for the neighboring edges.
199
+ var prevEdge = edge - 1 >= 0 ? edge - 1 : count1 - 1;
200
+ var sPrev = b2Collision.EdgeSeparation(poly1, prevEdge, poly2);
201
+ if (sPrev > 0.0 && conservative == false)
202
+ {
203
+ return sPrev;
204
+ }
205
+
206
+ var nextEdge = edge + 1 < count1 ? edge + 1 : 0;
207
+ var sNext = b2Collision.EdgeSeparation(poly1, nextEdge, poly2);
208
+ if (sNext > 0.0 && conservative == false)
209
+ {
210
+ return sNext;
211
+ }
212
+
213
+ // Find the best edge and the search direction.
214
+ var bestEdge = 0;
215
+ var bestSeparation;
216
+ var increment = 0;
217
+ if (sPrev > s && sPrev > sNext)
218
+ {
219
+ increment = -1;
220
+ bestEdge = prevEdge;
221
+ bestSeparation = sPrev;
222
+ }
223
+ else if (sNext > s)
224
+ {
225
+ increment = 1;
226
+ bestEdge = nextEdge;
227
+ bestSeparation = sNext;
228
+ }
229
+ else
230
+ {
231
+ // pointer out
232
+ edgeIndex[0] = edge;
233
+ return s;
234
+ }
235
+
236
+ while (true)
237
+ {
238
+
239
+ if (increment == -1)
240
+ edge = bestEdge - 1 >= 0 ? bestEdge - 1 : count1 - 1;
241
+ else
242
+ edge = bestEdge + 1 < count1 ? bestEdge + 1 : 0;
243
+
244
+ s = b2Collision.EdgeSeparation(poly1, edge, poly2);
245
+ if (s > 0.0 && conservative == false)
246
+ {
247
+ return s;
248
+ }
249
+
250
+ if (s > bestSeparation)
251
+ {
252
+ bestEdge = edge;
253
+ bestSeparation = s;
254
+ }
255
+ else
256
+ {
257
+ break;
258
+ }
259
+ }
260
+
261
+ // pointer out
262
+ edgeIndex[0] = bestEdge;
263
+ return bestSeparation;
264
+ };
265
+ b2Collision.FindIncidentEdge = function(c, poly1, edge1, poly2)
266
+ {
267
+ var count1 = poly1.m_vertexCount;
268
+ var vert1s = poly1.m_vertices;
269
+ var count2 = poly2.m_vertexCount;
270
+ var vert2s = poly2.m_vertices;
271
+
272
+ // Get the vertices associated with edge1.
273
+ var vertex11 = edge1;
274
+ var vertex12 = edge1 + 1 == count1 ? 0 : edge1 + 1;
275
+
276
+ // Get the normal of edge1.
277
+ var tVec = vert1s[vertex12];
278
+ //var normal1Local1 = b2Math.b2CrossVF( b2Math.SubtractVV( vert1s[vertex12], vert1s[vertex11] ), 1.0);
279
+ var normal1Local1X = tVec.x;
280
+ var normal1Local1Y = tVec.y;
281
+ tVec = vert1s[vertex11];
282
+ normal1Local1X -= tVec.x;
283
+ normal1Local1Y -= tVec.y;
284
+ var tX = normal1Local1X;
285
+ normal1Local1X = normal1Local1Y;
286
+ normal1Local1Y = -tX;
287
+ // ^^^^ normal1Local1.CrossVF(1.0);
288
+
289
+ var invLength = 1.0 / Math.sqrt(normal1Local1X*normal1Local1X + normal1Local1Y*normal1Local1Y);
290
+ normal1Local1X *= invLength;
291
+ normal1Local1Y *= invLength;
292
+ // ^^^^normal1Local1.Normalize();
293
+ //var normal1 = b2Math.b2MulMV(poly1.m_R, normal1Local1);
294
+ var normal1X = normal1Local1X;
295
+ var normal1Y = normal1Local1Y;
296
+
297
+ tX = normal1X;
298
+ var tMat = poly1.m_R;
299
+ normal1X = tMat.col1.x * tX + tMat.col2.x * normal1Y;
300
+ normal1Y = tMat.col1.y * tX + tMat.col2.y * normal1Y;
301
+ // ^^^^ normal1.MulM(poly1.m_R);
302
+
303
+ //var normal1Local2 = b2Math.b2MulTMV(poly2.m_R, normal1);
304
+ var normal1Local2X = normal1X;
305
+ var normal1Local2Y = normal1Y;
306
+ tMat = poly2.m_R;
307
+ tX = normal1Local2X * tMat.col1.x + normal1Local2Y * tMat.col1.y;
308
+ normal1Local2Y = normal1Local2X * tMat.col2.x + normal1Local2Y * tMat.col2.y;
309
+ normal1Local2X = tX;
310
+ // ^^^^ normal1Local2.MulTM(poly2.m_R);
311
+
312
+ // Find the incident edge on poly2.
313
+ var vertex21 = 0;
314
+ var vertex22 = 0;
315
+ var minDot = Number.MAX_VALUE;
316
+ for (var i = 0; i < count2; ++i)
317
+ {
318
+ var i1 = i;
319
+ var i2 = i + 1 < count2 ? i + 1 : 0;
320
+
321
+ //var normal2Local2 = b2Math.b2CrossVF( b2Math.SubtractVV( vert2s[i2], vert2s[i1] ), 1.0);
322
+ tVec = vert2s[i2];
323
+ var normal2Local2X = tVec.x;
324
+ var normal2Local2Y = tVec.y;
325
+ tVec = vert2s[i1];
326
+ normal2Local2X -= tVec.x;
327
+ normal2Local2Y -= tVec.y;
328
+ tX = normal2Local2X;
329
+ normal2Local2X = normal2Local2Y;
330
+ normal2Local2Y = -tX;
331
+ // ^^^^ normal2Local2.CrossVF(1.0);
332
+
333
+ invLength = 1.0 / Math.sqrt(normal2Local2X*normal2Local2X + normal2Local2Y*normal2Local2Y);
334
+ normal2Local2X *= invLength;
335
+ normal2Local2Y *= invLength;
336
+ // ^^^^ normal2Local2.Normalize();
337
+
338
+ //var dot = b2Math.b2Dot(normal2Local2, normal1Local2);
339
+ var dot = normal2Local2X * normal1Local2X + normal2Local2Y * normal1Local2Y;
340
+ if (dot < minDot)
341
+ {
342
+ minDot = dot;
343
+ vertex21 = i1;
344
+ vertex22 = i2;
345
+ }
346
+ }
347
+
348
+ var tClip;
349
+ // Build the clip vertices for the incident edge.
350
+ tClip = c[0];
351
+ //tClip.v = b2Math.AddVV(poly2.m_position, b2Math.b2MulMV(poly2.m_R, vert2s[vertex21]));
352
+ tVec = tClip.v;
353
+ tVec.SetV(vert2s[vertex21]);
354
+ tVec.MulM(poly2.m_R);
355
+ tVec.Add(poly2.m_position);
356
+
357
+ tClip.id.features.referenceFace = edge1;
358
+ tClip.id.features.incidentEdge = vertex21;
359
+ tClip.id.features.incidentVertex = vertex21;
360
+
361
+ tClip = c[1];
362
+ //tClip.v = b2Math.AddVV(poly2.m_position, b2Math.b2MulMV(poly2.m_R, vert2s[vertex22]));
363
+ tVec = tClip.v;
364
+ tVec.SetV(vert2s[vertex22]);
365
+ tVec.MulM(poly2.m_R);
366
+ tVec.Add(poly2.m_position);
367
+ tClip.id.features.referenceFace = edge1;
368
+ tClip.id.features.incidentEdge = vertex21;
369
+ tClip.id.features.incidentVertex = vertex22;
370
+ };
371
+ b2Collision.b2CollidePolyTempVec = new b2Vec2();
372
+ b2Collision.b2CollidePoly = function(manifold, polyA, polyB, conservative)
373
+ {
374
+ manifold.pointCount = 0;
375
+
376
+ var edgeA = 0;
377
+ var edgeAOut = [edgeA];
378
+ var separationA = b2Collision.FindMaxSeparation(edgeAOut, polyA, polyB, conservative);
379
+ edgeA = edgeAOut[0];
380
+ if (separationA > 0.0 && conservative == false)
381
+ return;
382
+
383
+ var edgeB = 0;
384
+ var edgeBOut = [edgeB];
385
+ var separationB = b2Collision.FindMaxSeparation(edgeBOut, polyB, polyA, conservative);
386
+ edgeB = edgeBOut[0];
387
+ if (separationB > 0.0 && conservative == false)
388
+ return;
389
+
390
+ var poly1;
391
+ var poly2;
392
+ var edge1 = 0;
393
+ var flip = 0;
394
+ var k_relativeTol = 0.98;
395
+ var k_absoluteTol = 0.001;
396
+
397
+ // TODO_ERIN use "radius" of poly for absolute tolerance.
398
+ if (separationB > k_relativeTol * separationA + k_absoluteTol)
399
+ {
400
+ poly1 = polyB;
401
+ poly2 = polyA;
402
+ edge1 = edgeB;
403
+ flip = 1;
404
+ }
405
+ else
406
+ {
407
+ poly1 = polyA;
408
+ poly2 = polyB;
409
+ edge1 = edgeA;
410
+ flip = 0;
411
+ }
412
+
413
+ var incidentEdge = [new ClipVertex(), new ClipVertex()];
414
+ b2Collision.FindIncidentEdge(incidentEdge, poly1, edge1, poly2);
415
+
416
+ var count1 = poly1.m_vertexCount;
417
+ var vert1s = poly1.m_vertices;
418
+
419
+ var v11 = vert1s[edge1];
420
+ var v12 = edge1 + 1 < count1 ? vert1s[edge1+1] : vert1s[0];
421
+
422
+ //var dv = b2Math.SubtractVV(v12, v11);
423
+ var dvX = v12.x - v11.x;
424
+ var dvY = v12.y - v11.y;
425
+
426
+ //var sideNormal = b2Math.b2MulMV(poly1.m_R, b2Math.SubtractVV(v12, v11));
427
+ var sideNormalX = v12.x - v11.x;
428
+ var sideNormalY = v12.y - v11.y;
429
+
430
+ var tX = sideNormalX;
431
+ var tMat = poly1.m_R;
432
+ sideNormalX = tMat.col1.x * tX + tMat.col2.x * sideNormalY;
433
+ sideNormalY = tMat.col1.y * tX + tMat.col2.y * sideNormalY;
434
+ // ^^^^ sideNormal.MulM(poly1.m_R);
435
+
436
+ var invLength = 1.0 / Math.sqrt(sideNormalX*sideNormalX + sideNormalY*sideNormalY);
437
+ sideNormalX *= invLength;
438
+ sideNormalY *= invLength;
439
+ // ^^^^ sideNormal.Normalize();
440
+
441
+ //var frontNormal = b2Math.b2CrossVF(sideNormal, 1.0);
442
+ var frontNormalX = sideNormalX;
443
+ var frontNormalY = sideNormalY;
444
+ tX = frontNormalX;
445
+ frontNormalX = frontNormalY;
446
+ frontNormalY = -tX;
447
+ // ^^^^ frontNormal.CrossVF(1.0);
448
+
449
+ // Expanded for performance
450
+ //v11 = b2Math.AddVV(poly1.m_position, b2Math.b2MulMV(poly1.m_R, v11));
451
+ var v11X = v11.x;
452
+ var v11Y = v11.y;
453
+ tX = v11X;
454
+ tMat = poly1.m_R;
455
+ v11X = tMat.col1.x * tX + tMat.col2.x * v11Y;
456
+ v11Y = tMat.col1.y * tX + tMat.col2.y * v11Y;
457
+ // ^^^^ v11.MulM(poly1.m_R);
458
+ v11X += poly1.m_position.x;
459
+ v11Y += poly1.m_position.y;
460
+ //v12 = b2Math.AddVV(poly1.m_position, b2Math.b2MulMV(poly1.m_R, v12));
461
+ var v12X = v12.x;
462
+ var v12Y = v12.y;
463
+ tX = v12X;
464
+ tMat = poly1.m_R;
465
+ v12X = tMat.col1.x * tX + tMat.col2.x * v12Y;
466
+ v12Y = tMat.col1.y * tX + tMat.col2.y * v12Y;
467
+ // ^^^^ v12.MulM(poly1.m_R);
468
+ v12X += poly1.m_position.x;
469
+ v12Y += poly1.m_position.y;
470
+
471
+ //var frontOffset = b2Math.b2Dot(frontNormal, v11);
472
+ var frontOffset = frontNormalX * v11X + frontNormalY * v11Y;
473
+ //var sideOffset1 = -b2Math.b2Dot(sideNormal, v11);
474
+ var sideOffset1 = -(sideNormalX * v11X + sideNormalY * v11Y);
475
+ //var sideOffset2 = b2Math.b2Dot(sideNormal, v12);
476
+ var sideOffset2 = sideNormalX * v12X + sideNormalY * v12Y;
477
+
478
+ // Clip incident edge against extruded edge1 side edges.
479
+ var clipPoints1 = [new ClipVertex(), new ClipVertex()];
480
+ var clipPoints2 = [new ClipVertex(), new ClipVertex()];
481
+
482
+ var np = 0;
483
+
484
+ // Clip to box side 1
485
+ b2Collision.b2CollidePolyTempVec.Set(-sideNormalX, -sideNormalY);
486
+ np = b2Collision.ClipSegmentToLine(clipPoints1, incidentEdge, b2Collision.b2CollidePolyTempVec, sideOffset1);
487
+
488
+ if (np < 2)
489
+ return;
490
+
491
+ // Clip to negative box side 1
492
+ b2Collision.b2CollidePolyTempVec.Set(sideNormalX, sideNormalY);
493
+ np = b2Collision.ClipSegmentToLine(clipPoints2, clipPoints1, b2Collision.b2CollidePolyTempVec, sideOffset2);
494
+
495
+ if (np < 2)
496
+ return;
497
+
498
+ // Now clipPoints2 contains the clipped points.
499
+ if (flip){
500
+ manifold.normal.Set(-frontNormalX, -frontNormalY);
501
+ }
502
+ else{
503
+ manifold.normal.Set(frontNormalX, frontNormalY);
504
+ }
505
+ // ^^^^ manifold.normal = flip ? frontNormal.Negative() : frontNormal;
506
+
507
+ var pointCount = 0;
508
+ for (var i = 0; i < b2Settings.b2_maxManifoldPoints; ++i)
509
+ {
510
+ //var separation = b2Math.b2Dot(frontNormal, clipPoints2[i].v) - frontOffset;
511
+ var tVec = clipPoints2[i].v;
512
+ var separation = (frontNormalX * tVec.x + frontNormalY * tVec.y) - frontOffset;
513
+
514
+ if (separation <= 0.0 || conservative == true)
515
+ {
516
+ var cp = manifold.points[ pointCount ];
517
+ cp.separation = separation;
518
+ cp.position.SetV( clipPoints2[i].v );
519
+ cp.id.Set( clipPoints2[i].id );
520
+ cp.id.features.flip = flip;
521
+ ++pointCount;
522
+ }
523
+ }
524
+
525
+ manifold.pointCount = pointCount;
526
+ };
527
+ b2Collision.b2CollideCircle = function(manifold, circle1, circle2, conservative)
528
+ {
529
+ manifold.pointCount = 0;
530
+
531
+ //var d = b2Math.SubtractVV(circle2.m_position, circle1.m_position);
532
+ var dX = circle2.m_position.x - circle1.m_position.x;
533
+ var dY = circle2.m_position.y - circle1.m_position.y;
534
+ //var distSqr = b2Math.b2Dot(d, d);
535
+ var distSqr = dX * dX + dY * dY;
536
+ var radiusSum = circle1.m_radius + circle2.m_radius;
537
+ if (distSqr > radiusSum * radiusSum && conservative == false)
538
+ {
539
+ return;
540
+ }
541
+
542
+ var separation;
543
+ if (distSqr < Number.MIN_VALUE)
544
+ {
545
+ separation = -radiusSum;
546
+ manifold.normal.Set(0.0, 1.0);
547
+ }
548
+ else
549
+ {
550
+ var dist = Math.sqrt(distSqr);
551
+ separation = dist - radiusSum;
552
+ var a = 1.0 / dist;
553
+ manifold.normal.x = a * dX;
554
+ manifold.normal.y = a * dY;
555
+ }
556
+
557
+ manifold.pointCount = 1;
558
+ var tPoint = manifold.points[0];
559
+ tPoint.id.set_key(0);
560
+ tPoint.separation = separation;
561
+ //tPoint.position = b2Math.SubtractVV(circle2.m_position, b2Math.MulFV(circle2.m_radius, manifold.normal));
562
+ tPoint.position.x = circle2.m_position.x - (circle2.m_radius * manifold.normal.x);
563
+ tPoint.position.y = circle2.m_position.y - (circle2.m_radius * manifold.normal.y);
564
+ };
565
+ b2Collision.b2CollidePolyAndCircle = function(manifold, poly, circle, conservative)
566
+ {
567
+ manifold.pointCount = 0;
568
+ var tPoint;
569
+
570
+ var dX;
571
+ var dY;
572
+
573
+ // Compute circle position in the frame of the polygon.
574
+ //var xLocal = b2Math.b2MulTMV(poly.m_R, b2Math.SubtractVV(circle.m_position, poly.m_position));
575
+ var xLocalX = circle.m_position.x - poly.m_position.x;
576
+ var xLocalY = circle.m_position.y - poly.m_position.y;
577
+ var tMat = poly.m_R;
578
+ var tX = xLocalX * tMat.col1.x + xLocalY * tMat.col1.y;
579
+ xLocalY = xLocalX * tMat.col2.x + xLocalY * tMat.col2.y;
580
+ xLocalX = tX;
581
+
582
+ var dist;
583
+
584
+ // Find the min separating edge.
585
+ var normalIndex = 0;
586
+ var separation = -Number.MAX_VALUE;
587
+ var radius = circle.m_radius;
588
+ for (var i = 0; i < poly.m_vertexCount; ++i)
589
+ {
590
+ //var s = b2Math.b2Dot(poly.m_normals[i], b2Math.SubtractVV(xLocal, poly.m_vertices[i]));
591
+ var s = poly.m_normals[i].x * (xLocalX-poly.m_vertices[i].x) + poly.m_normals[i].y * (xLocalY-poly.m_vertices[i].y);
592
+ if (s > radius)
593
+ {
594
+ // Early out.
595
+ return;
596
+ }
597
+
598
+ if (s > separation)
599
+ {
600
+ separation = s;
601
+ normalIndex = i;
602
+ }
603
+ }
604
+
605
+ // If the center is inside the polygon ...
606
+ if (separation < Number.MIN_VALUE)
607
+ {
608
+ manifold.pointCount = 1;
609
+ //manifold.normal = b2Math.b2MulMV(poly.m_R, poly.m_normals[normalIndex]);
610
+ var tVec = poly.m_normals[normalIndex];
611
+ manifold.normal.x = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
612
+ manifold.normal.y = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
613
+
614
+ tPoint = manifold.points[0];
615
+ tPoint.id.features.incidentEdge = normalIndex;
616
+ tPoint.id.features.incidentVertex = b2Collision.b2_nullFeature;
617
+ tPoint.id.features.referenceFace = b2Collision.b2_nullFeature;
618
+ tPoint.id.features.flip = 0;
619
+ tPoint.position.x = circle.m_position.x - radius * manifold.normal.x;
620
+ tPoint.position.y = circle.m_position.y - radius * manifold.normal.y;
621
+ //tPoint.position = b2Math.SubtractVV(circle.m_position , b2Math.MulFV(radius , manifold.normal));
622
+ tPoint.separation = separation - radius;
623
+ return;
624
+ }
625
+
626
+ // Project the circle center onto the edge segment.
627
+ var vertIndex1 = normalIndex;
628
+ var vertIndex2 = vertIndex1 + 1 < poly.m_vertexCount ? vertIndex1 + 1 : 0;
629
+ //var e = b2Math.SubtractVV(poly.m_vertices[vertIndex2] , poly.m_vertices[vertIndex1]);
630
+ var eX = poly.m_vertices[vertIndex2].x - poly.m_vertices[vertIndex1].x;
631
+ var eY = poly.m_vertices[vertIndex2].y - poly.m_vertices[vertIndex1].y;
632
+ //var length = e.Normalize();
633
+ var length = Math.sqrt(eX*eX + eY*eY);
634
+ eX /= length;
635
+ eY /= length;
636
+
637
+ // If the edge length is zero ...
638
+ if (length < Number.MIN_VALUE)
639
+ {
640
+ //d = b2Math.SubtractVV(xLocal , poly.m_vertices[vertIndex1]);
641
+ dX = xLocalX - poly.m_vertices[vertIndex1].x;
642
+ dY = xLocalY - poly.m_vertices[vertIndex1].y;
643
+ //dist = d.Normalize();
644
+ dist = Math.sqrt(dX*dX + dY*dY);
645
+ dX /= dist;
646
+ dY /= dist;
647
+ if (dist > radius)
648
+ {
649
+ return;
650
+ }
651
+
652
+ manifold.pointCount = 1;
653
+ //manifold.normal = b2Math.b2MulMV(poly.m_R, d);
654
+ manifold.normal.Set(tMat.col1.x * dX + tMat.col2.x * dY, tMat.col1.y * dX + tMat.col2.y * dY);
655
+ tPoint = manifold.points[0];
656
+ tPoint.id.features.incidentEdge = b2Collision.b2_nullFeature;
657
+ tPoint.id.features.incidentVertex = vertIndex1;
658
+ tPoint.id.features.referenceFace = b2Collision.b2_nullFeature;
659
+ tPoint.id.features.flip = 0;
660
+ //tPoint.position = b2Math.SubtractVV(circle.m_position , b2Math.MulFV(radius , manifold.normal));
661
+ tPoint.position.x = circle.m_position.x - radius * manifold.normal.x;
662
+ tPoint.position.y = circle.m_position.y - radius * manifold.normal.y;
663
+ tPoint.separation = dist - radius;
664
+ return;
665
+ }
666
+
667
+ // Project the center onto the edge.
668
+ //var u = b2Math.b2Dot(b2Math.SubtractVV(xLocal , poly.m_vertices[vertIndex1]) , e);
669
+ var u = (xLocalX-poly.m_vertices[vertIndex1].x) * eX + (xLocalY-poly.m_vertices[vertIndex1].y) * eY;
670
+
671
+ tPoint = manifold.points[0];
672
+ tPoint.id.features.incidentEdge = b2Collision.b2_nullFeature;
673
+ tPoint.id.features.incidentVertex = b2Collision.b2_nullFeature;
674
+ tPoint.id.features.referenceFace = b2Collision.b2_nullFeature;
675
+ tPoint.id.features.flip = 0;
676
+
677
+ var pX, pY;
678
+ if (u <= 0.0)
679
+ {
680
+ pX = poly.m_vertices[vertIndex1].x;
681
+ pY = poly.m_vertices[vertIndex1].y;
682
+ tPoint.id.features.incidentVertex = vertIndex1;
683
+ }
684
+ else if (u >= length)
685
+ {
686
+ pX = poly.m_vertices[vertIndex2].x;
687
+ pY = poly.m_vertices[vertIndex2].y;
688
+ tPoint.id.features.incidentVertex = vertIndex2;
689
+ }
690
+ else
691
+ {
692
+ //p = b2Math.AddVV(poly.m_vertices[vertIndex1] , b2Math.MulFV(u, e));
693
+ pX = eX * u + poly.m_vertices[vertIndex1].x;
694
+ pY = eY * u + poly.m_vertices[vertIndex1].y;
695
+ tPoint.id.features.incidentEdge = vertIndex1;
696
+ }
697
+
698
+ //d = b2Math.SubtractVV(xLocal , p);
699
+ dX = xLocalX - pX;
700
+ dY = xLocalY - pY;
701
+ //dist = d.Normalize();
702
+ dist = Math.sqrt(dX*dX + dY*dY);
703
+ dX /= dist;
704
+ dY /= dist;
705
+ if (dist > radius)
706
+ {
707
+ return;
708
+ }
709
+
710
+ manifold.pointCount = 1;
711
+ //manifold.normal = b2Math.b2MulMV(poly.m_R, d);
712
+ manifold.normal.Set(tMat.col1.x * dX + tMat.col2.x * dY, tMat.col1.y * dX + tMat.col2.y * dY);
713
+ //tPoint.position = b2Math.SubtractVV(circle.m_position , b2Math.MulFV(radius , manifold.normal));
714
+ tPoint.position.x = circle.m_position.x - radius * manifold.normal.x;
715
+ tPoint.position.y = circle.m_position.y - radius * manifold.normal.y;
716
+ tPoint.separation = dist - radius;
717
+ };
718
+ b2Collision.b2TestOverlap = function(a, b)
719
+ {
720
+ var t1 = b.minVertex;
721
+ var t2 = a.maxVertex;
722
+ //d1 = b2Math.SubtractVV(b.minVertex, a.maxVertex);
723
+ var d1X = t1.x - t2.x;
724
+ var d1Y = t1.y - t2.y;
725
+ //d2 = b2Math.SubtractVV(a.minVertex, b.maxVertex);
726
+ t1 = a.minVertex;
727
+ t2 = b.maxVertex;
728
+ var d2X = t1.x - t2.x;
729
+ var d2Y = t1.y - t2.y;
730
+
731
+ if (d1X > 0.0 || d1Y > 0.0)
732
+ return false;
733
+
734
+ if (d2X > 0.0 || d2Y > 0.0)
735
+ return false;
736
+
737
+ return true;
738
+ };