summaryrefslogtreecommitdiff
path: root/2d/_collisions/polygon_polygon.html.content
diff options
context:
space:
mode:
Diffstat (limited to '2d/_collisions/polygon_polygon.html.content')
-rw-r--r--2d/_collisions/polygon_polygon.html.content61
1 files changed, 42 insertions, 19 deletions
diff --git a/2d/_collisions/polygon_polygon.html.content b/2d/_collisions/polygon_polygon.html.content
index 4f99d71..f2a9253 100644
--- a/2d/_collisions/polygon_polygon.html.content
+++ b/2d/_collisions/polygon_polygon.html.content
@@ -20,9 +20,21 @@
<p>
The Separating Axis Theorem (SAT) provides a way to find the intersection between any <i>n</i>-sided <a href='https://ianqvist.blogspot.com/2009/09/convex-polygon-based-collision.html'>convex</a> polygon or circle. In this tutorial, I will explain how this theorem works, and how you can use it to both detect and resolve collisions in your simulation.
</p>
+
+ <ul>
+ <li><a href='#explanation_of_separating_axis_theorem'>Explanation of Separating Axis Theorem</a></li>
+ <li><a href='#algorithm_for_finding_intersection'>Algorithm for Finding the Intersection</a></li>
+ <li><a href='#collision_resolution'>SAT Collision Resolution</a></li>
+ <ul>
+ <li><a href='#intersecting_edge'>Finding the Intersecting Edge</a></li>
+ <li><a href='#intersecting_point'>Finding the Intersecting Point</a></li>
+ </ul>
+ <li><a href='#algorithm_for_collision_resolution'>Algorithm for Collision Resolution</a></li>
+ <li><a href='#live_example'>Live Example</a></li>
+ </ul>
</section>
<section>
- <h2>Explanation of Separating Axis Theorem</h2>
+ <h2 id="#explanation_of_separating_axis_theorem">Explanation of Separating Axis Theorem</h2>
<p>
SAT makes use of vector projection to figure out whether or not two concave polygons are intersecting. I believe the best way to describe it is to show you the instance where two polygons do <b>not</b> intersect.
@@ -95,7 +107,7 @@
</p>
</section>
<section>
- <h2>Algorithm for Finding the Intersection</h2>
+ <h2 id='algorithm_for_finding_intersection'>Algorithm for Finding the Intersection</h2>
<p>
Given two polygons <b>A</b> and <b>B</b>:
@@ -113,7 +125,7 @@
</p>
</section>
<section>
- <h2>SAT Collision Resolution</h2>
+ <h2 id='collision_resolution'>SAT Collision Resolution</h2>
<p>
Now that we know that our objects have intersected, we want to send them tumbling away from one another in order to simulate a collision. To do this, we will need to find the following things:
<ul>
@@ -122,10 +134,7 @@
<li><b>Relative Velocity</b>: easily found by taking the difference between the two velocities.
</ul>
- To find these values, we must first find both the shape and the edge that caused the intersection in the first place. To do this, we will think about what we know so far and try to arrive at some intutitive understanding of it. Keep in mind that I am not a proof-minded person, so you will not be finding that here.
- <br/>
-
- <h3>Finding the Intersecting Edge</h3>
+ <h3 id='intersecting_edge'>Finding the Intersecting Edge</h3>
We can already figure out that the following two triangles intersect one another:
<br />
<br />
@@ -136,7 +145,9 @@
<br/><br/>
- We know that <b>A</b> can only intersect <b>B</b> if: (1) a vertex from <b>A</b> is inside of <b>B</b>, (2) an edge of <b>A</b> flatly intersects an edge of <b>B</b>, or (3) a vertex of <b>A</b> overlaps exactly a vertex of <b>B</b>. Honestly, for our purposes, scenarios 2 and 3 are quite unlikely, but we can explore them a bit just to see how we might resolve them. We will start with the first case, since it is more likely. We will start by drawing the axis defined by the leftmost edge of <b>B</b> with both polygons projected onto it:
+ We know that <b>A</b> can only intersect <b>B</b> if: (1) a vertex from <b>A</b> is inside of <b>B</b>, (2) an edge of <b>A</b> flatly intersects an edge of <b>B</b>, or (3) a vertex of <b>A</b> overlaps exactly a vertex of <b>B</b>. For our purposes, scenario 3 is quite unlikely, so we can ignore it for now. We will start with the first case, since it is the most likely.
+ <br /> <br />
+ The following drawing shows both of our objects projected onto the axis defined by object <b>B</b>'s leftmost edge:
<br /> <br />
@@ -145,22 +156,34 @@
</div>
<br/><br/>
- This is a poorly drawn picture, but you should be able to see that the bit in green represents the intersection between the projections of the two polygons. If we were to repeat this same exercise for every edge here, we'll begin to see something very interesting. And, if we take new shapes and continue this stategy, we can begin to come to a very elegant conclusion:
- <br/>
- <br/>
-
- <i>The intersecting edge will be the one where the projection of triangle <b>A</b> overlaps with the projection of triangle <b>B</b> the <b>least</b></i>!
-
- <br /> <br />
- I'm sure someone more inclined to proving mathematical truities would love to describe this to you, but, for all intents and purposes, this intutitive understanding is good enough for us. We just want to make games, anyhow.
- <br />
- <h3>Finding which point causes the intersection</h3>
+ If you were to repeat this exercise of projecting the shapes onto every axis of both shapes, you will begin to unconver an interesting truth: <i>The intersecting edge will be the one where the projection of triangle <b>A</b> overlaps with the projection of triangle <b>B</b> the <b>least</b></i>! This should be unsurprising if you consider it. If our simulation is running quickly enough, the point of object <b>A</b> that is penetrating the edge of object <b>B</b> will just <i>barely</i> be inside of <b>B</b>. At the same time, it <i>should</i> be the only point that is currently inside of B. Hence, the overlap of the projection on that axis will be from that edge of <b>B</b> to that point on <b>A</b>. Knowing that this edge caused the intersection, we now have the collision normal.
+ <br />
+ <h3 id='intersecting_point'>Finding which point causes the intersection</h3>
+ This part is made easy by the fact that we know which edge of object <b>B</b> caused the intersection in the first place. We know, then, that the intersecting point on object <b>A</b> must be the point that is currently inside of <b>B</b>. Simply put, this point will be the vertex of <b>A</b> that was projected onto that axis that is <i>closest</i> to the edge of <b>B</b>. This will be true so long as the simulation is properly fast and the object is not infinitesmley small (in which case, we're dealing with particle physics, and you shouldn't be using this tutorial).
+ </p>
+ </section>
+ <section>
+ <h2 id='algorithm_for_collision_resolution'>Algorithm for Collision Resolution</h2>
+
+ <p>
+
+ Given two polygons <b>A</b> and <b>B</b>:
+ <ol>
+ <li>Find the intersecton as before</li>
+ <li>While finding the intersection, keep track of the axis that the projection on object <b>A</b> overlapped with the corresponding projection on object <b>B</b> the least. Remember the edge that this that held this axis.</li>
+ <li>Using this axis, find out which point on the other object inside of the edge by comparing the distances from the two points on the edge to the two points projected onto the axis created by that edge.</li>
+ <li>Use the calcualted edge to find the normal and the overlap point to find the point of application.</li>
+ </ol>
+
+ And that is it! Here is the code for this algorithm:
+
+ #SNIPPET polygon_polygon/snippet2.cpp
</p>
</section>
<section>
- <h2>
+ <h2 id='live_example'>
Live Example of Intersection Detection
</h2>
<div class="opengl_canvas_container">