summaryrefslogtreecommitdiff
path: root/2d/_collisions/polygon_polygon.html
diff options
context:
space:
mode:
Diffstat (limited to '2d/_collisions/polygon_polygon.html')
-rw-r--r--2d/_collisions/polygon_polygon.html60
1 files changed, 56 insertions, 4 deletions
diff --git a/2d/_collisions/polygon_polygon.html b/2d/_collisions/polygon_polygon.html
index 5d360af..4c4698a 100644
--- a/2d/_collisions/polygon_polygon.html
+++ b/2d/_collisions/polygon_polygon.html
@@ -4,7 +4,7 @@
<meta charset="utf-8">
<link rel="stylesheet" href="/index.css">
<title>Physics for Games</title>
- <link rel="shortcut icon" href="favicon/favicon.ico" type="image/x-icon">
+ <link rel="shortcut icon" href="/favicon/favicon.ico" type="image/x-icon">
<meta name="description" content="A place to learn all about real-time physics simulations through descriptions, code snippets, and example programs all written in C++ and OpenGL.">
<meta name="og:description" content="A place to learn all about real-time physics simulations through descriptions, code snippets, and example programs all written in C++ and OpenGL.">
</head>
@@ -159,13 +159,65 @@
<li>If the projections overlap for each edge of both shapes, the shapes are intersecting. Return true.</li>
</ol>
- And that is all there is to <i>finding</i> the intersection between two convex polygons.
- </p>
+ And that is all there is to <i>finding</i> the intersection between two convex polygons. Here is the code snippet that does this if you are interested:
+
+ <pre><code><span class="code_keyword">Vector2</span> getProjection(Vector2* vertices, <span class="code_keyword">int</span> numVertices, <span class="code_keyword">Vector2</span> axis) {
+ <span class="code_comment">// Find the min and max vertex projections</span>
+ <span class="code_keyword">float32</span> min = axis.dot(vertices[0]);
+ <span class="code_keyword">float32</span> max = min;
+
+ for (int v = 1; v < numVertices; v++) {
+ <span class="code_keyword">float32</span> d = axis.dot(vertices[v]);
+
+ if (d < min) {
+ min = d;
+ } else if (d > max) {
+ max = d;
+ }
+ }
+
+ <span class="code_keyword">return</span> <span class="code_keyword">Vector2</span> { min, max };
+}
+
+<span class="code_keyword">bool</span> projectionsOverlap(Vector2 first, <span class="code_keyword">Vector2</span> second) {
+ <span class="code_keyword">return</span> first.x <= second.y && second.x <= first.y;
+}
+
+<span class="code_keyword">bool</span> doIntersect(ConvexPolygon* first, ConvexPolygon* second) {
+ IntersectionResult ir;
+
+ <span class="code_comment">// Check agaisnt the edges of the first polygon</span>
+ for (int i = 0; i < first->numVertices; i++) {
+ <span class="code_keyword">Vector2</span> normal = first->edges[i].normal;
+
+ <span class="code_keyword">Vector2</span> firstProj = getProjection(first->transformedVertices, first->numVertices, normal);
+ <span class="code_keyword">Vector2</span> secondProj = getProjection(second->transformedVertices, second->numVertices, normal);
+
+ if (!projectionsOverlap(firstProj, secondProj)) {
+ <span class="code_keyword">return</span> false;
+ }
+ }
+
+ <span class="code_comment">// Check against the edges of the second polygon</span>
+ for (int i = 0; i < second->numVertices; i++) {
+ <span class="code_keyword">Vector2</span> normal = second->edges[i].normal;
+
+ <span class="code_keyword">Vector2</span> firstProj = getProjection(first->transformedVertices, first->numVertices, normal);
+ <span class="code_keyword">Vector2</span> secondProj = getProjection(second->transformedVertices, second->numVertices, normal);
+
+ if (!projectionsOverlap(firstProj, secondProj)) {
+ <span class="code_keyword">return</span> false;
+ }
+ }
+
+ <span class="code_keyword">return</span> true;
+}
+</code></pre> </p>
</section>
<section>
<h2>SAT Collision Resolution</h2>
<p>
- Now that we know our objects have intersecting, we want to be able to send them tumbling away from each other to simulate a collision. To do this, we will need to find the following things:
+ Now that we know our objects have intersected, we want to be able to send them tumbling away from each other to simulate a collision. To do this, we will need to find the following things:
<ul>
<li><b>Collision Normal</b>: in what direction, point towards object <b>A</b>, did the polygons intersect</li>
<li><b>Point of Application</b>: at what point on each object did the objects first intersect</li>