Download - Game Engineering: 2D
![Page 1: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/1.jpg)
Game Engineering: 2DCS420-2010F-09
2D IntersectionDavid Galles
Department of Computer Science
University of San Francisco
![Page 2: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/2.jpg)
09-0: Spirte collision
Given:Two 2D square images
(some of the pixels are transparent – not partof the actual image)
Two points (upper-left corner of each image)
How can we determine if the two images intersect?
![Page 3: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/3.jpg)
09-1: Spirte collision
Brute force:For each point in image A:
If the point is not transparent, comapre it tothe corresponding point in image BNeed to account for translation difference!
![Page 4: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/4.jpg)
09-2: Spirte collision
Sprite A at position (x1, y1), width w1, height h1
Sprite B at position (x2, y2), width w2, height h2
How do we determine if there is intersection be-
tween Sprite A and Sprite B, assuming a 2D array of
transparancy values? Brute force is OK!
![Page 5: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/5.jpg)
09-3: Spirte collision
Sprite A at position (x1, y1), width w1, height h1
Sprite B at position (x2, y2), width w2, height h2
deltax = x2 - x1; deltay = y2 - y1
for (int x = 0; x < w1; x++)
for (y = 0; y < h1; y++)
if (x + deltax > 0 && x + deltax < w2 &&
y + deltay > 0 && y + deltay h2 &&
point at T1[x][y] not tranparent &&
point at T2[x + deltax][y + deltay] not tranparent)
collision!
![Page 6: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/6.jpg)
09-4: Checking Transparency
Texture2D texture;
Color[] data = new Color[texture.Width * texture.Height];
texture.GetData(data);
if (data[x + y*texture.width].A > 0)
{
// point at texture[x,y] not completely
// transpanent
}
![Page 7: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/7.jpg)
09-5: Spirte collision
Of course, this is incredibly wasteful ...
We can be more efficient by looking at boundingregions
Simple shapes that contain the sprite
Start with bounding boxes
![Page 8: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/8.jpg)
09-6: AABB
Axis-Aligned Bounding BoxA Bounding box is a box that completelycontains your spriteAn Axis aligned bounding box has its sidesaligned with the global x and y axesWhat would we need to store in memory torepresent the axis aligned bounding box for anobject?
![Page 9: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/9.jpg)
09-7: AABB
To represent an axis-aligned bounding box, weneed two points (4 floats)
Minimum x, minimum yMaximum x, maximum y
![Page 10: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/10.jpg)
09-8: AABB
There are other ways to represent an AABBMinimum x,y and vector from min to max pointcenter of the AABB and vector to the maximumpoint... etc
![Page 11: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/11.jpg)
09-9: AABB Translation
We have an AABB for a modelHow should the AABB change if the modeltranslates?
![Page 12: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/12.jpg)
09-10: AABB Translation
We have an AABB for a modelHow should the AABB change if the modeltranslates?
Just translate the minimum and maximumpoint in the AABB
![Page 13: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/13.jpg)
09-11: AABB Rotation
We have an AABB for a modelHow should the AABB change if the modelrotates?
![Page 14: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/14.jpg)
09-12: AABB Rotation
We have an AABB for a modelHow should the AABB change if the modelrotates?
Can we just rotate the two minimum andmaxiumum points? Why or why not?
![Page 15: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/15.jpg)
09-13: AABB Rotation
We have an AABB for a modelHow should the AABB change if the modelrotates?
Rotate each of the 4 points in the AABB (whyall 4)?Construct a new AABB that contains thesepoints
![Page 16: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/16.jpg)
09-14: AABB Rotation
![Page 17: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/17.jpg)
09-15: AABB Rotation
![Page 18: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/18.jpg)
09-16: AABB Rotation
Smallest possible AABB after rotation?
![Page 19: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/19.jpg)
09-17: AABB Rotation
Smallest possible AABB after rotation?Go through all rotated points, find max / min
Middle ground – rotate original AABB (examples)
![Page 20: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/20.jpg)
09-18: AABB Intersection
Given two AABBs, how can we determine if theyintersect?
![Page 21: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/21.jpg)
09-19: AABB Intersection
![Page 22: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/22.jpg)
09-20: AABB Intersection
Two 2D AABBs don’t intesect if we can create aline such that one AABB is one one side of the line,and the other is on the other side of the line
We only need to test 4 lines – the 4 sides of one ofthe boxes
![Page 23: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/23.jpg)
09-21: AABB Intersection
![Page 24: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/24.jpg)
09-22: AABB Intersection
![Page 25: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/25.jpg)
09-23: AABB Intersection
Given two 2D AABBs B1 and B2, they don’tintersect if:
B1.x.max < B2.x.min
B1.x.min > B2.x.max
B1.y.max < B2.y.min
B1.y.min > B2.y.max
![Page 26: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/26.jpg)
09-24: AABB Intersection
Once we know that our two AABBs intersect, we’dlike to find the actual area of overlap
Given that we know that the two AABBs overlap,how can we find the intersection?
![Page 27: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/27.jpg)
09-25: AABB Intersection
Upper left of the intersection AABB is:
Lower right of the intersection AABB is:
![Page 28: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/28.jpg)
09-26: AABB Intersection
Upper left of the intersection AABB is:Max of the mins of the two AABBs
Lower right of the intersection AABB is:Min of the maxes of the two AABBs
![Page 29: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/29.jpg)
09-27: AABB Intersection
AABB1 is (x1min, y1min), (x1max, y1max)
AABB2 is (x2min, y2min), (x2max, y2max)
AABB of intersection (assuming that they intersect)is:
![Page 30: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/30.jpg)
09-28: AABB Intersection
AABB1 is (x1min, y1min), (x1max, y1max)
AABB2 is (x2min, y2min), (x2max, y2max)
AABB of intersection (assuming that they intersect)is:
(max(x1min, x2min),max(y1min, y2min),(min(x1max, x2max),min(y1max, y2max),
![Page 31: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/31.jpg)
09-29: AABB Intersection
Sanity Check
![Page 32: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/32.jpg)
09-30: AABB Intersection
Once we have the area of the AABB intersection,we could use that area to do a pixel-by-pixel checkof the objects – iterate over the intersection regioninstead of over one of the boxes
![Page 33: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/33.jpg)
09-31: AABB Intersection
Let’s say that we were using AABBs only forintersection (our world objects are nice squaresthat don’t rotate)
Once we know that two AABBs intersect, we wantto push them apart so that they no longer intersect
We need a vector to translate one of the squaresso that they no longer intersect
Minimum Translation Distance vector, or MTDvector
![Page 34: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/34.jpg)
09-32: AABB MTD
What is the MTD for two AAABBs that intersect?
![Page 35: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/35.jpg)
09-33: AABB MTD
![Page 36: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/36.jpg)
09-34: AABB MTD
What is the MTD for two AAABBs that intersect?Minimum dimension of AABB intersection
![Page 37: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/37.jpg)
09-35: OOBB Defintion
Axis-aligned bounding boxes are good as a firststep
Computing insersections is fastActual intersection => AABB intersection
AABBs are not great for final collision detection –not accurate enough
We can do a little better with oriented boundingboxes
![Page 38: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/38.jpg)
09-36: OOBB Defintion
We will define an OOBB using 4 pointsPoints of each corner of the box
Our techniques will work for any convex polygon,not just boxes
![Page 39: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/39.jpg)
09-37: OOBB Intersection
We can define intersection of OOBB similarly tointersection of AABB:
If a line can be inserted between two OOBBs,such that one object is on one side of the line,and the other object is on the other side of theline, the boxes do not intersectIf no such line exists, then the boxes dointersect
![Page 40: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/40.jpg)
09-38: OOBB Intersection
Let’s look at another way doing the same thing ...Pick any line you like (axis of separation)Project both boxes onto this axisIf the projections don’t overlap, no intersection
![Page 41: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/41.jpg)
09-39: OOBB Intersection
![Page 42: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/42.jpg)
09-40: OOBB Intersection
![Page 43: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/43.jpg)
09-41: OOBB Intersection
![Page 44: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/44.jpg)
09-42: OOBB Intersection
If there is some axis of separation with no overlap,no intersection
However, if there is an axis of separation withoverlap, may not be any intersection
Need to pick the correct one!
![Page 45: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/45.jpg)
09-43: OOBB Intersection
![Page 46: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/46.jpg)
09-44: OOBB Intersection
There are a infinite number of axes to try!
Can’t try them all!
If there is an axis of separation, then it must be ...
![Page 47: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/47.jpg)
09-45: OOBB Intersection
There are a infinite number of axes to try!
Can’t try them all!
If there is an axis of separation, then it must bePerpendicular to one of the edgesThere are only 4 possibilities if we are workingwith OOBBs
![Page 48: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/48.jpg)
09-46: OOBB Intersection
![Page 49: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/49.jpg)
09-47: OOBB Intersection
![Page 50: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/50.jpg)
09-48: OOBB Intersection
![Page 51: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/51.jpg)
09-49: OOBB Intersection
![Page 52: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/52.jpg)
09-50: OOBB Intersection
Pick an axis of separation (will need to try 4 in all)
Project boxes onto axis
See if there is overlap
If there is no overlap on any axis, we can stop – nointersection
![Page 53: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/53.jpg)
09-51: OOBB Intersection
How do we project a box onto our axis?Project each point onto axisFind the maximum and minimum values
![Page 54: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/54.jpg)
09-52: OOBB Intersection
Box: p1, p2, p3, p4
Axis: [x,y] (normalized)
![Page 55: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/55.jpg)
09-53: OOBB Intersection
Box: p1, p2, p3, p4
Axis: [x,y] (normalized)
p1
![Page 56: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/56.jpg)
09-54: OOBB Intersection
Box: p1, p2, p3, p4
Axis: [x,y] (normalized)
p1Θ A * p1 = ||A|| ||p1|| cos Θ = ||p1|| cos θ = length of projection
A
![Page 57: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/57.jpg)
09-55: OOBB Intersection
Given a box p1, p2, p3, p4, what are the two axes ofintersection?
![Page 58: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/58.jpg)
09-56: OOBB Intersection
Given a box p1, p2, p3, p4, what are the two axes ofintersection?
Perpendicular to an edgeVector parallel to an edge: v = p2 − p1
Vector perpendicular to v = [vx, vy]?
![Page 59: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/59.jpg)
09-57: OOBB Intersection
Given a box p1, p2, p3, p4, what are the two axes ofintersection?
Perpendicular to an edgeVector parallel to an edge: v = p2 − p1
Vector perpendicular to v = [vx, vy]?[−vy, vx]
Axis of intersection: [−vy,vx]
||[−vy ,vx]||
![Page 60: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/60.jpg)
09-58: OOBB Intersection
Given two boxes (p11, p12, p13, p14) and(p21, p22, p23, p24)
v‖ = p12 − p11
a =[−v‖
y,v‖
x]
||[−v‖y,v‖
x]||
Calculate a · p11, a · p12,a · p13, a · p14, storeminimum and maximum valuesCalculate a · p21, a · p22, a · p23, a · p24, storeminimum and maximum valuesCheck for overlapRepeat for p13 − p12, p22 − p21, p23 − p22
![Page 61: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/61.jpg)
09-59: OOBB MTD
How can we calculate the Minimum TranslationDistance vector?
![Page 62: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/62.jpg)
09-60: OOBB MTD
How can we calculate the Minimum TranslationDistance vector?
Find the separating axis with the minimumoverlapVector is along the separating axis, lengthequal to the overlap
![Page 63: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/63.jpg)
09-61: OOBB MTD
We’ve already calculated the separating axisvector, and the amount of offset – multiply theoffset size (scalar) by separating axis vector(already normalized)
![Page 64: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/64.jpg)
09-62: General Intersection
This method works for any convex surface:For axes of separation, try vector perpendicularto each edge
![Page 65: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/65.jpg)
09-63: General Intersection
![Page 66: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/66.jpg)
09-64: General Intersection
![Page 67: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/67.jpg)
09-65: General Intersection
![Page 68: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/68.jpg)
09-66: General Intersection
Only works for convex objects, not concave
![Page 69: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/69.jpg)
09-67: General Intersection
![Page 70: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/70.jpg)
09-68: General Intersection
![Page 71: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/71.jpg)
09-69: General Intersection
![Page 72: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/72.jpg)
09-70: More Intersection!
Bounding CirclesCircle represented as a center point c = [cx, cy]and a radius r
Given c1, r1, c2, r2. How can we determine if twocircles have intersected?
![Page 73: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/73.jpg)
09-71: Bounding Circles
Given c1, r1, c2, r2. How can we determine if twocircles have intersected?
||c1 − c2|| < r1 + r2
||c1 − c2||2 < (r1 + r2)
2
What is the Minimum Translation Distance (MTD)vector?
![Page 74: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/74.jpg)
09-72: Bounding Circles
Given c1, r1, c2, r2. How can we determine if twocircles have intersected?
||c1 − c2|| < r1 + r2
What is the Minimum Translation Distance (MTD)vector?
c1−c2||c1−c2||
(r1 + r2 − ||c1 − c2||)
![Page 75: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/75.jpg)
09-73: Line Intersection
Given two linesA1x + B1y = C1
A2x + B2y = C2
How do we determine their point of intersection?
![Page 76: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/76.jpg)
09-74: Line Intersection
Solve for y in equation 1
A1x + B1y = C1
y =C1 − A1x
B1
Substitute for y in equation 2:
A2x + B2
C1 − A1x
B1
= C2
x(A2 −B2A1
B1
) = C2 −B2C1
B1
x =C2B1 − B2C1
A2B1 − B2A1
![Page 77: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/77.jpg)
09-75: Line Intersection
x =C2B1 − B2C1
A2B1 − B2A1
Do the same thing for y: (solve for x in equation 1,and then substitute into equation 2)
y =C1A2 − A1C2
A2B1 − B2A1
What does it mean if A2B1 − B2A1 = 0?
![Page 78: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/78.jpg)
09-76: Line Segment Intersection
Given two segments: p11, p12, p21, p22, how do wedetermine if they intersect?
![Page 79: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/79.jpg)
09-77: Line Segment Intersection
Given two segments: p11, p12, p21, p22, how do wedetermine if they intersect?
Given two points (x1, y1), (x2, y2), we cancalculate A,B,C:
A = y2 − y1
B = x1 − x2
C = Ax1 + By1 (or C = Ax2 + By2)
Once we have A1, A2, B1, B2, C1, C2, we candetermine where the two lines defined by the twosegments intersect
What next?
![Page 80: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/80.jpg)
09-78: Line Segment Intersection
Given two segments p11, p12, p21, p22, and the pointx, y where the lines defined by each segmentintersect:
Check to see if x, y is within the range of eachsegment
x >= p11xand x <= p12x
(or x >= p12xand
x <= p11x)
y >= p11yand y <= p12y
(or y >= p12yand
y <= p11y)
x >= p21xand x <= p22x
(or x >= p22xand
x <= p21x)
y >= p21yand y <= p22y
(or y >= p22yand
y <= p21y)
![Page 81: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/81.jpg)
09-79: Closest Point on Line
Given a point p and a line L (defined by two pointsp1, p2), how can we determine the closest point to palong L?
![Page 82: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/82.jpg)
09-80: Closest Point on Line
![Page 83: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/83.jpg)
09-81: Closest Point on Line
![Page 84: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/84.jpg)
09-82: Closest Point on Line
Given a point p and a line L (defined by two pointsp1, p2), how can we determine the closest point to palong L?
Create a line L⊥ that is perpendicular to L andgoes through p
Find the intersection of L and L⊥
![Page 85: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/85.jpg)
09-83: Closest Point on Line
L : A1x + B1y = C
L⊥ : −B1x + A1y + C2
C2 = −B1px + A1py
(plug the known value of the point into theformula, and solve for C2)
![Page 86: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/86.jpg)
09-84: Closest Point on Segment
Given a line segment defined by points p1, p2 and apoint p, how can we find the point closest to palong the segment?
![Page 87: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/87.jpg)
09-85: Closest Point on Segment
Given a line segment defined by points p1, p2 and apoint p, how can we find the point closest to palong the segment?
If the closest point to the line defined by thesegment is in the segment, that is the correctpointOtherwise, endpoint of segment closest tointersection point
![Page 88: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/88.jpg)
09-86: Segment/Circle
How can we determine of a line segment and acircle intersect?
![Page 89: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/89.jpg)
09-87: Segment/Circle
How can we determine of a line segment and acircle intersect?
Find the point p on the segment closest to thecenter of the circleCheck to see if the distance from p to thecenter of the circle c is < radius r
How can we calculate the MTD vector?
![Page 90: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/90.jpg)
09-88: Segment/Circle
How can we determine of a line segment and acircle intersect?
Find the point p on the segment closest to thecenter of the circleCheck to see if the distance from p to thecenter of the circle c is < radius r
How can we calculate the MTD vector?Direction of vector: p − c
Length of MTD: r − ||p − v||
![Page 91: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/91.jpg)
09-89: Polygon / Circle
How can we determine if a polygon and a circleintersect?
![Page 92: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/92.jpg)
09-90: Polygon / Circle
How can we determine if a polygon and a circleintersect?
For each segment in the polygon, check forintersection of polygon and line segmentFind the point p closest to the center c of thecircle from all segments in the polygonMTD:
Direction of MTD vector: p − c
Length of MTD: r − ||p − v||
![Page 93: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/93.jpg)
09-91: Polygon / Circle
We can also use Axis of separation to determine ifpolygon and circle intersect
Once we have a candidate axis of separation, howcan we project the circle onto it?
![Page 94: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/94.jpg)
09-92: Polygon / Circle
![Page 95: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/95.jpg)
09-93: Polygon / Circle
Once we have a candidate axis of separation:Project the center of the circle to the axisProjection extends r units to either side of thispoint
How do we find candidate axis of separation?
![Page 96: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/96.jpg)
09-94: Polygon / Circle
Projectionof Center
radius
![Page 97: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/97.jpg)
09-95: Polygon / Circle
How do we find candidate axis of separation?Lines perpendicular to the sides of the polygonWill those always work?
![Page 98: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/98.jpg)
09-96: Polygon / Circle
How do we find candidate axis of separation?Lines perpendicular to the sides of the polygonLine from the center of the circle to the closestpoint on the polygon to the circle
![Page 99: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/99.jpg)
09-97: Closest point on a Circle
Given a circle (c, r) and a point q, what is the pointon the circle closest to q?
![Page 100: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/100.jpg)
09-98: Closest point on a Circle
rp
c
q d = c - q
b
b =||d|| − r
||d||d
p = q +||d|| − r
||d||d
![Page 101: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/101.jpg)
09-99: Closest point in AABB
GivenAABB (minX, minY, maxX, maxY)point q
What is the point closest to q that is inside theAABB?
![Page 102: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/102.jpg)
09-100: Closest point in AABB
![Page 103: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/103.jpg)
09-101: Closest point in AABB
“Push in” the point along each axis
![Page 104: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/104.jpg)
09-102: Closest point in AABB
if (q.x < minX)
p.x = minX
else if (q.x > maxX)
p.x = maxX
else
p.x = q.x
if (q.y < minY)
p.y = minY
else if (q.y > maxY)
p.y = maxY
else
p.y = q.y
![Page 105: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/105.jpg)
09-103: Dynmamic Intersection
Doing only static intersections will missintersections that happen between frames
Small, fast-moving objects (like bullets) canpenetrate thin objects (like walls)
We can model a small object like a bullet using aline / line segment
Already know how to do intersection wtih linesegments
![Page 106: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/106.jpg)
09-104: Dynamic Circle Intersection
Two circles, one moving and one staticDid the circles intersect between the frame?
![Page 107: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/107.jpg)
09-105: Dynamic Circle Intersection
![Page 108: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/108.jpg)
09-106: Dynamic Circle Intersection
Method 1:Sweep the moving circleCreate a rectangle and two circlesDo interesction as before
![Page 109: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/109.jpg)
09-107: Dynamic Circle Intersection
![Page 110: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/110.jpg)
09-108: Dynamic Circle Intersection
How do we find the vertices?
![Page 111: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/111.jpg)
09-109: Dynamic Circle Intersection
v
d
p
c = center of circle
v = [vx, vy] velocity vector
r = radius of circle
![Page 112: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/112.jpg)
09-110: Dynamic Circle Intersection
v
d
p
d = [−vx,vy]r
||v||
p = c + d
![Page 113: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/113.jpg)
09-111: Dynamic Circle Intersection
Method #2:3 Step Process
Is the moving circle even moving towards thestationary circle?If the moving circle continued along itscourse, would it eventually interect with thestationary circleWill the moving circle actually intersect withthe stationary circle?
![Page 114: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/114.jpg)
09-112: Dynamic Circle Intersection
Moving towards, might intersect
Moving away, cannot intersect
![Page 115: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/115.jpg)
09-113: Dynamic Circle Intersection
Cs-Cd
v
![Page 116: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/116.jpg)
09-114: Dynamic Circle Intersection
Angle bewteen velocity and line betweencenterpoints > 90
(cstatic − cdynamic) · v < 0
Circles moving apart, can’t intersect
![Page 117: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/117.jpg)
09-115: Dynamic Circle Intersection
If the dynamic circle is moving towards the staticcircle
Find the closest point to the static circle alongthe movement vectorSee if this point is > rd + rs
![Page 118: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/118.jpg)
09-116: Dynamic Circle Intersection
v
![Page 119: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/119.jpg)
09-117: Dynamic Circle Intersection
D
||Cs-Cd||
F
Cs = center point of static circleCd = center point of dynamic circle
![Page 120: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/120.jpg)
09-118: Dynamic Circle Intersection
D = v
||V ||· (Cs − Cd)
||Cs − cd||2 − D2 = F 2
Check to see if F 2 > (rs + rd)2
![Page 121: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/121.jpg)
09-119: Dynamic Circle Intersection
Dynamic circle is moving toward static circle
Might intersect, if dynamic circle moves far enough
How can we determine the exact position of firstintersection?
![Page 122: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/122.jpg)
09-120: Dynamic Circle Intersection
D (already calculated)
F (already calculated)
G
r + r s d
![Page 123: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/123.jpg)
09-121: Dynamic Circle Intersection
D = v
||V ||· (Cs − Cd)
||Cs − cd||2 − D2 = F 2
G2 = (rs + rd)2 − F 2
Check if ||v|| > D − G
![Page 124: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/124.jpg)
09-122: Dynamic Circle Intersection
Both circles moving different speeds
![Page 125: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/125.jpg)
09-123: Dynamic Circle Intersection
![Page 126: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/126.jpg)
09-124: Dynamic Circle Intersection
Can’t sweep both circles
Math we did a moment ago gets really hard if bothcircles are moving
... but there is an easy solution, reuse everythingwe’ve just done
![Page 127: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/127.jpg)
09-125: Dynamic Circle Intersection
Changing reference frames to the rescue!
Pretend that Cirle 2 is not moving at all
Velocity of Circle 1 in Cirlce 2’s frame of reference:
![Page 128: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/128.jpg)
09-126: Dynamic Circle Intersection
Changing reference frames to the rescue!
Pretend that Cirle 2 is not moving at all
Velocity of Circle 1 in Cirlce 2’s frame of reference:v1 − v2
![Page 129: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/129.jpg)
09-127: Dynamic Circle Intersection
![Page 130: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/130.jpg)
09-128: Dynamic Circle Intersection
Static
Dynamic
![Page 131: Game Engineering: 2D](https://reader033.vdocuments.mx/reader033/viewer/2022060422/6295a875c2397417bd03b020/html5/thumbnails/131.jpg)
09-129: Dynamic Circle Intersection