b2_collide_polygon.cpp
Go to the documentation of this file.
1 // MIT License
2 
3 // Copyright (c) 2019 Erin Catto
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #include "box2d/b2_collision.h"
24 #include "box2d/b2_polygon_shape.h"
25 
26 // Find the max separation between poly1 and poly2 using edge normals from poly1.
27 static float b2FindMaxSeparation(int32* edgeIndex,
28  const b2PolygonShape* poly1, const b2Transform& xf1,
29  const b2PolygonShape* poly2, const b2Transform& xf2)
30 {
31  int32 count1 = poly1->m_count;
32  int32 count2 = poly2->m_count;
33  const b2Vec2* n1s = poly1->m_normals;
34  const b2Vec2* v1s = poly1->m_vertices;
35  const b2Vec2* v2s = poly2->m_vertices;
36  b2Transform xf = b2MulT(xf2, xf1);
37 
38  int32 bestIndex = 0;
39  float maxSeparation = -b2_maxFloat;
40  for (int32 i = 0; i < count1; ++i)
41  {
42  // Get poly1 normal in frame2.
43  b2Vec2 n = b2Mul(xf.q, n1s[i]);
44  b2Vec2 v1 = b2Mul(xf, v1s[i]);
45 
46  // Find deepest point for normal i.
47  float si = b2_maxFloat;
48  for (int32 j = 0; j < count2; ++j)
49  {
50  float sij = b2Dot(n, v2s[j] - v1);
51  if (sij < si)
52  {
53  si = sij;
54  }
55  }
56 
57  if (si > maxSeparation)
58  {
59  maxSeparation = si;
60  bestIndex = i;
61  }
62  }
63 
64  *edgeIndex = bestIndex;
65  return maxSeparation;
66 }
67 
69  const b2PolygonShape* poly1, const b2Transform& xf1, int32 edge1,
70  const b2PolygonShape* poly2, const b2Transform& xf2)
71 {
72  const b2Vec2* normals1 = poly1->m_normals;
73 
74  int32 count2 = poly2->m_count;
75  const b2Vec2* vertices2 = poly2->m_vertices;
76  const b2Vec2* normals2 = poly2->m_normals;
77 
78  b2Assert(0 <= edge1 && edge1 < poly1->m_count);
79 
80  // Get the normal of the reference edge in poly2's frame.
81  b2Vec2 normal1 = b2MulT(xf2.q, b2Mul(xf1.q, normals1[edge1]));
82 
83  // Find the incident edge on poly2.
84  int32 index = 0;
85  float minDot = b2_maxFloat;
86  for (int32 i = 0; i < count2; ++i)
87  {
88  float dot = b2Dot(normal1, normals2[i]);
89  if (dot < minDot)
90  {
91  minDot = dot;
92  index = i;
93  }
94  }
95 
96  // Build the clip vertices for the incident edge.
97  int32 i1 = index;
98  int32 i2 = i1 + 1 < count2 ? i1 + 1 : 0;
99 
100  c[0].v = b2Mul(xf2, vertices2[i1]);
101  c[0].id.cf.indexA = (uint8)edge1;
102  c[0].id.cf.indexB = (uint8)i1;
105 
106  c[1].v = b2Mul(xf2, vertices2[i2]);
107  c[1].id.cf.indexA = (uint8)edge1;
108  c[1].id.cf.indexB = (uint8)i2;
111 }
112 
113 // Find edge normal of max separation on A - return if separating axis is found
114 // Find edge normal of max separation on B - return if separation axis is found
115 // Choose reference edge as min(minA, minB)
116 // Find incident edge
117 // Clip
118 
119 // The normal points from 1 to 2
121  const b2PolygonShape* polyA, const b2Transform& xfA,
122  const b2PolygonShape* polyB, const b2Transform& xfB)
123 {
124  manifold->pointCount = 0;
125  float totalRadius = polyA->m_radius + polyB->m_radius;
126 
127  int32 edgeA = 0;
128  float separationA = b2FindMaxSeparation(&edgeA, polyA, xfA, polyB, xfB);
129  if (separationA > totalRadius)
130  return;
131 
132  int32 edgeB = 0;
133  float separationB = b2FindMaxSeparation(&edgeB, polyB, xfB, polyA, xfA);
134  if (separationB > totalRadius)
135  return;
136 
137  const b2PolygonShape* poly1; // reference polygon
138  const b2PolygonShape* poly2; // incident polygon
139  b2Transform xf1, xf2;
140  int32 edge1; // reference edge
141  uint8 flip;
142  const float k_tol = 0.1f * b2_linearSlop;
143 
144  if (separationB > separationA + k_tol)
145  {
146  poly1 = polyB;
147  poly2 = polyA;
148  xf1 = xfB;
149  xf2 = xfA;
150  edge1 = edgeB;
151  manifold->type = b2Manifold::e_faceB;
152  flip = 1;
153  }
154  else
155  {
156  poly1 = polyA;
157  poly2 = polyB;
158  xf1 = xfA;
159  xf2 = xfB;
160  edge1 = edgeA;
161  manifold->type = b2Manifold::e_faceA;
162  flip = 0;
163  }
164 
165  b2ClipVertex incidentEdge[2];
166  b2FindIncidentEdge(incidentEdge, poly1, xf1, edge1, poly2, xf2);
167 
168  int32 count1 = poly1->m_count;
169  const b2Vec2* vertices1 = poly1->m_vertices;
170 
171  int32 iv1 = edge1;
172  int32 iv2 = edge1 + 1 < count1 ? edge1 + 1 : 0;
173 
174  b2Vec2 v11 = vertices1[iv1];
175  b2Vec2 v12 = vertices1[iv2];
176 
177  b2Vec2 localTangent = v12 - v11;
178  localTangent.Normalize();
179 
180  b2Vec2 localNormal = b2Cross(localTangent, 1.0f);
181  b2Vec2 planePoint = 0.5f * (v11 + v12);
182 
183  b2Vec2 tangent = b2Mul(xf1.q, localTangent);
184  b2Vec2 normal = b2Cross(tangent, 1.0f);
185 
186  v11 = b2Mul(xf1, v11);
187  v12 = b2Mul(xf1, v12);
188 
189  // Face offset.
190  float frontOffset = b2Dot(normal, v11);
191 
192  // Side offsets, extended by polytope skin thickness.
193  float sideOffset1 = -b2Dot(tangent, v11) + totalRadius;
194  float sideOffset2 = b2Dot(tangent, v12) + totalRadius;
195 
196  // Clip incident edge against extruded edge1 side edges.
197  b2ClipVertex clipPoints1[2];
198  b2ClipVertex clipPoints2[2];
199  int np;
200 
201  // Clip to box side 1
202  np = b2ClipSegmentToLine(clipPoints1, incidentEdge, -tangent, sideOffset1, iv1);
203 
204  if (np < 2)
205  return;
206 
207  // Clip to negative box side 1
208  np = b2ClipSegmentToLine(clipPoints2, clipPoints1, tangent, sideOffset2, iv2);
209 
210  if (np < 2)
211  {
212  return;
213  }
214 
215  // Now clipPoints2 contains the clipped points.
216  manifold->localNormal = localNormal;
217  manifold->localPoint = planePoint;
218 
219  int32 pointCount = 0;
220  for (int32 i = 0; i < b2_maxManifoldPoints; ++i)
221  {
222  float separation = b2Dot(normal, clipPoints2[i].v) - frontOffset;
223 
224  if (separation <= totalRadius)
225  {
226  b2ManifoldPoint* cp = manifold->points + pointCount;
227  cp->localPoint = b2MulT(xf2, clipPoints2[i].v);
228  cp->id = clipPoints2[i].id;
229  if (flip)
230  {
231  // Swap features
232  b2ContactFeature cf = cp->id.cf;
233  cp->id.cf.indexA = cf.indexB;
234  cp->id.cf.indexB = cf.indexA;
235  cp->id.cf.typeA = cf.typeB;
236  cp->id.cf.typeB = cf.typeA;
237  }
238  ++pointCount;
239  }
240  }
241 
242  manifold->pointCount = pointCount;
243 }
uint8 indexB
Feature index on shapeB.
Definition: b2_collision.h:53
uint8 typeA
The feature type on shapeA.
Definition: b2_collision.h:54
b2Vec2 b2Mul(const b2Mat22 &A, const b2Vec2 &v)
Definition: b2_math.h:422
Used for computing contact manifolds.
Definition: b2_collision.h:146
float b2Dot(const b2Vec2 &a, const b2Vec2 &b)
Perform the dot product on two vectors.
Definition: b2_math.h:395
b2Vec2 localNormal
not use for Type::e_points
Definition: b2_collision.h:109
b2Rot q
Definition: b2_math.h:361
f
b2ContactID id
uniquely identifies a contact point between two shapes
Definition: b2_collision.h:80
uint8 typeB
The feature type on shapeB.
Definition: b2_collision.h:55
b2ContactFeature cf
Definition: b2_collision.h:61
#define b2_maxManifoldPoints
Definition: b2_common.h:51
static void b2FindIncidentEdge(b2ClipVertex c[2], const b2PolygonShape *poly1, const b2Transform &xf1, int32 edge1, const b2PolygonShape *poly2, const b2Transform &xf2)
#define b2_linearSlop
Definition: b2_common.h:65
A 2D column vector.
Definition: b2_math.h:41
doubleAcc dot(const VectorAcc &lhs, const VectorAcc &rhs)
b2ContactID id
Definition: b2_collision.h:149
signed int int32
Definition: b2_types.h:28
B2_API int32 b2ClipSegmentToLine(b2ClipVertex vOut[2], const b2ClipVertex vIn[2], const b2Vec2 &normal, float offset, int32 vertexIndexA)
Clipping for contact manifolds.
float m_radius
Definition: b2_shape.h:102
float b2Cross(const b2Vec2 &a, const b2Vec2 &b)
Perform the cross product on two vectors. In 2D this produces a scalar.
Definition: b2_math.h:401
b2Vec2 m_vertices[b2_maxPolygonVertices]
int32 pointCount
the number of manifold points
Definition: b2_collision.h:112
b2Vec2 localPoint
usage depends on manifold type
Definition: b2_collision.h:77
uint8 indexA
Feature index on shapeA.
Definition: b2_collision.h:52
b2Vec2 localPoint
usage depends on manifold type
Definition: b2_collision.h:110
b2ManifoldPoint points[b2_maxManifoldPoints]
the points of contact
Definition: b2_collision.h:108
unsigned char uint8
Definition: b2_types.h:29
static float b2FindMaxSeparation(int32 *edgeIndex, const b2PolygonShape *poly1, const b2Transform &xf1, const b2PolygonShape *poly2, const b2Transform &xf2)
b2Vec2 b2MulT(const b2Mat22 &A, const b2Vec2 &v)
Definition: b2_math.h:429
float Normalize()
Convert this vector into a unit vector. Returns the length.
Definition: b2_math.h:102
#define b2_maxFloat
Definition: b2_common.h:39
void b2CollidePolygons(b2Manifold *manifold, const b2PolygonShape *polyA, const b2Transform &xfA, const b2PolygonShape *polyB, const b2Transform &xfB)
Compute the collision manifold between two polygons.
#define b2Assert(A)
Definition: b2_common.h:37
b2Vec2 m_normals[b2_maxPolygonVertices]


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:19