Presentation is loading. Please wait.

Presentation is loading. Please wait.

Collision Detection Slide 27: Intersection of 2 Boxes

Similar presentations


Presentation on theme: "Collision Detection Slide 27: Intersection of 2 Boxes"— Presentation transcript:

1 Collision Detection Slide 27: Intersection of 2 Boxes
39-40: Sweep Algorithms 68: Other Things You Shouldn't be able to Go Through Collision Detection Resume at Sphere sweeping 74: Sphere sweeping OR 77 How do we translate a Plane OUT by R" 87: Solving Vector Equations

2 When that occurs, we say that a collision has occurred.
When an object moves from A to B, it is highly probable that something in between will block it's path. When that occurs, we say that a collision has occurred. Detecting this and determining the point (the hit point) during the movement where collision has occurred is called collision detection. Determining what to do at the hit point is called collision reaction. The collision detection problem is often described as the problem of determining what can and cannot be hit.

3 Detecting collision in a movement from A to B
Finding the hit point (if any) Reacting to the collision Sliding Hitting then sliding Bouncing Hitting then bouncing building object at A object at B

4 Takeaway

5 Collision detection Collision detection Point colliding with a plane
Sphere colliding with a plane Box colliding with a plane Triangle colliding with a plane Collision detection

6 Section Basic Routines What is the projection of a point on a line?
on a plane What is the intersection of a line with a plane with a sphere

7 The 4 Problems in Pictures
2 projections P P N point on line point on plane Q Q A B 2 intersections (nearest only) line with sphere N line with plane A A B Q Q' Q B Q, not Q'

8 Three of the 4 problems deal with a line of the form
Note Three of the 4 problems deal with a line of the form P = A + (B-A) * t Once you solve for t, you plug t into the above This gives you the solution

9 Point Projected Onto to Line
Q A B Define Q to be the projection of point P onto the line from A to B projection (P,A,B) = Q

10 Projection of a Point onto a Line
Point projectionOfPoint (const Point &q, double &t) const { //If p = p1 + td is this line for any point p and we choose p such that v = p - q is a perpendicular vector //from q to p, then d.v = 0 since A.B=|A||B|cos 90=0. Consequently, //d.(p - q) = 0 //d.(p1+td - q) = 0 //d.(p1-q)+td.d = 0 //t = -d.(p1-q) / d.d = d.(q-p1) / d.d" Vector direction = end - start; t = direction.dot (q - start) / direction.dot (direction); return start + direction * t; } A P B Let Q = A + (B-A) t be any point on the line Let V = B-A to simplify the math Q Choose a particular Q such that Q-P is perpendicular to B-A; i.e. V Hence (Q-P).V = 0 X.Y = |X| |Y| cos  = 0 when  = 90 degrees (A + V t - P).V = 0 Substitute for Q (V t + A - P).V = 0 Rearrange V.V t + (A-P).V = 0 Multiply through t = -(A-P).V / V.V Solve for t t = (P-A).V / V.V Negate t = (P-A).(B-A) / (B-A).(B-A) substitute for V Projection (P,A,B) = A + (B-A) t where t is given above

11 Projection of a Point Onto a Plane
Q Define Q to be the projection of point P onto the Plane projection (P, aPlane) = Q

12 Recall: About Planes N Let the plane be [a,b,c,d] = [Nx,Ny,Nz,-N.P0] as a 4-vector Or N.(P-P0) = 0 as an equation where P is [x,y,z] Plane [a,b,c,d] has normal N = [a,b,c] and d = -N.P0 for some no longer known point P0 that was used to construct it

13 Projection of a Point P onto a Plane
Temporarily replace P by B we're used to seeing P on the plane P B P dp Q Let the plane be [a,b,c,d] = [Nx,Ny,Nz,-N.P0] as a 4-vector Or N.(P-P0) = 0 as an equation where P is [x,y,z] Let dp be the distance to the plane. Let it intersect at Q. Hence dp = N.(B-P0) Substitute B for P in the plane equation to get the distance dp = N.B-N.P0 Simplifying dp = N.B+d Replacing by the actual value of the plane Therefore Q = B - dp N Backing up by dp along N Q = B - (N.B+d) N Substitute by dp Replacing B back to P projection (P, aPlane) = P - (N.P+d) N

14 Intersection of a Line with a Plane
Q B Define Q to be the intersection of the line from A to B with the plane if it exists it does not exist if the line is parallel with the plane intersection (A, B, aPlane) = Q

15 Intersecting a Line with a Plane
Let the plane be [a,b,c,d] = [Nx,Ny,Nz,-N.P0] as a 4-vector P Or N.(P-P0) = 0 as an equation where P is [x,y,z] B Let P = A + (B-A) t be the equation of the line N.(A + (B-A) t -P0) = 0 Substitute P into the plane equation N.A + N.(B-A) t – N.P0 = 0 Perform N. on each term N.(B-A) t = N.P0 – N.A Move non-t terms to the right t = N.(P0 - A) / N.(B-A) Solve for t t = N.(A-P0) / N.(A-B) = (N.A+d) / N.(A-B) negate numerator and denominator and substitute for –N.P0 if the denominator is 0, there is no intersection (line is parallel to plane) intersection (A,B, aPlane) = A + (B-A) t where t = (N.A+d) / N.(A-B) provided the denominator is not zero

16 Intersection of a Line with a Sphere
B Q Q' Q, not Q' Define Q to be the nearest intersection of the line from A to B with the sphere of radius R if it exists intersection (A,B,aSphere) = Q

17 A Note About Spheres The equation of a sphere is most familiar as
where C = [c1, c2, c3] is the center [x-c1]2+ [y-c2]2 + [z-c3]2 = R2 where [x, y, z] is a point on the surface The vector form is more compact where C = [c1, c2, c3] is the center (P - C).(P - C) = R2 where P = [x, y, z] is a point on the surface

18 Intersection of a Line with a Sphere
Let P = A + (B-A) t = A+Dt be the equation of the line t = 0 at A t = 1 at B D = B-A Let aSphere have center C = [c1, c2, c3] and radius R Let (P - C).(P - C) = R2 be the equation of the sphere where P = [x, y, z] is a point on the surface Equation of sphere (A+Dt - C).(A+Dt - C) = R2 Substitute for P (Dt + A - C).(Dt + A - C) = R2 Re-arrange P = A + (B-A) t = A+D t (Dt).(Dt) + 2Dt.(A - C)+ (A - C).(A - C) = R2 Multiply through (a+b)2 = a2+2ab+c2 D.Dt2 + 2D.(A - C)t + (A - C).(A - C) - R2 = 0 Group in powers of t a t2 + b t + c This is a quadratic a = (B-A).(B-A) b = 2 (B-A).(A-C) c = (A-C).(A-C) - R2

19 Intersection of a Line with a Sphere
a t2 + b t + c This is a quadratic a = (B-A).(B-A) b = 2 (B-A).(A-C) c = (A-C).(A-C) - R2 -b ±  b2 – 4ac 2a t = Solution is Note: a is positive since Q.Q = qx2 + qy2 + qz2 D = b2 – 4ac < 0: 0 intersection D = b2 – 4ac = 0: 1 intersection Tangent to sphere (let's view that as a miss) D = b2 – 4ac > 0: 2 intersections -b ± D 2a t = -b - D 2a t = is smallest value Since a is positive

20 Intersection of a Line with a Sphere: Summary
Let the line be P = A + (B-A) t t = 0 at A t = 1 at B Let aSphere have center C and radius R a = (B-A).(B-A) With b = 2 (B-A).(A-C) c = (A-C).(A-C) - R2 If D = b2 - 4ac > 0, there is an intersection at -b - D 2a t = Otherwise, there is no intersection intersection (A,B,aSphere) = A+(B-A) t

21 The Intersection Problem: In terms of t-values
Q intersection-t (A,B,aPlane) = (N.A+d) / N.(A-B) provided the denominator is not zero intersection-t (A,B,aPlane) = A B Q a = (B-A).(B-A) -b - D 2a where b = 2 (B-A).(A-C) c = (A-C).(A-C) - R2 provided D = b2 - 4ac > 0 Both can supply t = 2.0 when there is no solution

22 Basic Routine Conclusions
What is the projection of a point on a plane? What is the intersection of a line on a line? with a plane with a sphere Projection (P,A,B) Projection (P,aPlane) Intersection (A,B,aPlane) Intersection-t (A,B,aPlane) Intersection (A,B,aSphere) Intersection-t (A,B,aSphere)

23 Basic Routine Conclusions
Projection (P,A,B) = A + (B-A) t where t = (P-A).(B-A) / (B-A).(B-A) Projection (P, aPlane)) = P - (N.P+d) N where the plane = [Nx,Ny,Nz,d] Intersection (A,B, aPlane) = A + (B-A) t where t = (N.A+d) / N.(A-B) provided the denominator is not zero = A + (B-A) t -b - D 2a where t = provided D = b2 - 4ac > 0 where a = (B-A).(B-A) b = 2 (B-A).(A-C) c = (A-C).(A-C) - R2 Intersection (A,B,aSphere)

24 Section Movement Boxes What is it? How do we use it?

25 Maximum and Minimum Points
The minimum (or maximum) of two points is the independent minimum (or maximum) of two x's minimum (or maximum) of two y's minimum (or maximum) of two z's Minimum of [2, 7, 4] and [0,10, 5] = [0, 7, 4]

26 Movement Boxes A movement box is a bounding box encompassing an object moving from A to B. B A Easy to build: a box with the minimum of the two minimums and the maximum of the two maximums Similarly, easy to build if we're moving a point

27 BoundingBox order doesn't matter
Intersection of 2 Boxes BoundingBox order doesn't matter BoundingBox intersection (const BoundingBox &box) { #define box1 this-> #define box2 box. } return BoundingBox ( box1 minimum.minimum (box2 minimum), box1 maximum.maximum (box2 maximum)); box1 box2 max min box1.intersection (box2) VERSUS box2.intersection (box1)

28 Movement Box Conclusions
What is it? A bounding box that encompasses the moving object from its start to end points How do we use it? The only objects a moving object can collide with are those in the movement box

29 Collision Detection Trees
Section Collision Detection Trees What are they? How do we use them?

30 Collision Detection Trees
A collision detection tree is like a visibility tree except that it includes only objects you can collide with. No grass or bushes, trunks but not branches A large static one for non-moving objects A small dynamic one for moving objects The leaves are expected NOT to contain many objects

31 Building a List of Candidates
Start at the root of the tree; it's assumed the movement box B is inside the tree T. If there are objects in T that intersect with B Add them to your list For each child C of T If C's bounding box intersects with T Repeat recursively with C Note to programmers: Just link candidates together (avoid using Collections that need to grow)

32 CanSee and CanHit

33 Candidates are considered in arbitrary order
An object canSee or canHit an obstacle if it's intended movement is blocked by it. Both routines return a boolean, i.e., true or false. canHit also returns a t-value for the nearest hit t < 1.0 implies a hit t  1.0 implies no hit Candidates are considered in arbitrary order t = 0.6 t = 0.9 canSee is false final answer for either case canHit is true (t = 0.9) not final answer canHit is true (t = 0.6) CanSee cares about ANY blockage. CanHit cares about the nearest one.

34 CanSee

35 CanSee: Application 1 (Sun flares)
Games know where the sun is Player canSee the sun if it's daylight, sunny, and nothing blocks the way from player's eye position to sun

36 CanSee: 2 Applications (Enemies See You)
Games know where the player is… Enemy canSee the player if the player is in it's frustum and nothing blocks the way from enemy's eye position to player's eye position maybe a cheap frustum can be used

37 Cheap Frustum We can implement a cheap frustum with dot products
A.B = |A||B| cos  A B Pplayer Penemy "In frustum" if cos  < 20 degrees; i.e., < degrees * 2 radians / 360 degrees =  / 9 radians pick any suitable angle 20 A = [0,0,-1,0] * Wenemy unit length if Wenemy has no scale B = normalized (Pplayer - Penemy) In cheap frustum if A.B <  / 9 Pplayer = [0,0,0,1] * Wplayer Penemy = [0,0,0,1] * Wenemy

38 Collision Detection Algorithms

39 Increasingly complicated shapes include
Sweep Algorithms There are different algorithms (or formulas) for sweeping different shapes from A to B. Increasingly complicated shapes include Point Sphere Polygon Box including Bounding box Cylinder Capsule Was not going to do these but changed my mind Barrel Convoid Bunch of polygons also referred to as a polygon soup Wilf's name

40 Shapes on Left Can Hit Any Shapes on Right
we don't sweep planes but we can hit them Plane etc Point Box Sphere Polygon Capsule Cylinder Convoid Barrel Point Box Sphere Polygon Capsule Cylinder Convoid Barrel Quite a few interactions

41 Each shape has a different algorithm
The Algorithm CanSee (A, B) Uses the collision tree algorithm to find a list of candidates which we'll call shapes. Finds the first shape S for which Shape CanSee (A, B, shape S) is true Generally confined to point sweeping CanHit (A, B) Uses the collision tree algorithm to find a list of candidates which we'll call shapes. Finds the nearest shape S for which Each shape has a different algorithm Shape CanHit (A, B, shape S) is true Point sweeping is just the simplest case

42 The Core Algorithm for Point Sweeping to Hit a basic Shape
1. Start with the line P = A + (B-A) t t = 0 at A t = 1 at B which may be shorter to write as P = A + D t where D = B-A 2. Write the shape as an equation of some type one that involves P 3. See where the line intersects the shape by substituting for P in the shape 4. The resulting equation will have the variable t in it solve for t 5. If there are multiple solutions, pick the nearest one the one with the smallest t

43 Collision Detection Trees Conclusion
What are they? Like visibility trees used to detect collisions with objects that you can collide with. How do we use them? Used initially for finding subtrees that intersect the movement box and then using sweep algorithms for finding results of canSee and canHit on those objects in the movement box.

44 Point Sweep Algorithms
Section Point Sweep Algorithms There are as many algorithms as there are shapes you wish to collide with. As part of the course we will initially consider all those except for cylinders and capsules This MOSTLY follows the book

45 Front/Back facing Planes and Faces

46 Polygons have a Front and a Back
A front face has points ordered counter-clockwise P3 P0 P4 P2 P1 A back face has points ordered clockwise This assumes polygons are convex; i.e., each edge turns inward Right hand rule: Place fingers on right hand on line P0->P1, rotate toward next point P2. The thumb points in the direction of the normal.

47 How to Build a Face Normal
P4 Take 3 consecutive points P3 i.e. p0, p1, p2 P0 front facing Pivot around p0; from p1 toward p2 P2 P1 N = (p1-p0) x (p2-p0) P1 P2 P0 N "follows" right hand rule back facing Rotate toward P3 P4

48 Planes also Have a Side The direction of the plane is dictated by
the direction of N N facing the back Suppose you have a ray from A to B A B facing the front How do you tell if you're facing the back or the front A B I mean, how do you tell mathematically?

49 Recall: the Dot Product
A.B = |A| |B| cos  where |A| = |B| = 1 A B  = 45° A.B = 0.707  = 90° A.B = 0  = 135° A.B = aligned with (> 0) perpendicular (= 0) aligned against (< 0)

50 Planes also Have a Side A B N facing the back  aligned with A B N
facing the front aligned against

51 Summary A ray is directed into a FRONT plane or a face IF
B N a FRONT face or plane The ray is aligned against the normal N For collision detection, the backs of planes or faces are considered invisible. Why? You can't get to a back face without first going through a front face

52 CanHit Routines for one Shape CAN (where useful) make use of CanHit Routines for Other Shapes

53 What CanHit Can Provide
We wish to think of the canHit algorithm as returning 2 things: Whether or not a hit has occurred? If a hit has occurred, the t value at the collision point. bool Point::canHit (Point A, Point B, Shape S, float &t) { //Most algorithms are of the form… //Find case 1 where no hit can occur, in which case return false //Find case 2 where no hit can occur, in which case return false //Ultimately, you have found a condition where a hit can occur, //you have computed t, how far from A to B you can go. //So you now return true }

54 Suppose we develop an algorithm for detecting collisions with Eggs.
How We Will Proceed Suppose we develop an algorithm for detecting collisions with Eggs. This means we have shown how to implement Point::canHit (Point A, Point B, Egg anEgg, float &t) So when we use this in some other algorithm, of course we don't describe it again. Instead we can say Let wasHit = Point::canHit (A, B, anEgg, t) … if (!wasHit) return false; if (!Point::canHit (A, B, anEgg, t)) return false; If there was a hit, the t can be used for other computation

55 The General Form of All Point Sweep Routines
P = A + (B-A) t = A+D t Point::CanHit (A, B, aShape, t) The line from A to B can be written in two forms by describing where point P is on the line as follows: Form1: P = A + (B-A) t When t = 0, P = A When t = 1, P = B Form2: where D = B-A P = A+Dt Simpler to work with mathematically but we'll replace D by B-A if the final result contains D What you'll see in the top right corner to remind you of this P = A + (B-A) t = A+D t

56 Point Versus Plane AND Point Versus Sphere

57 Point Versus Plane/Sphere
B N A P P = A + (B-A) t = A+D t t = 0 at A t = 1 at B D = B-A Point::CanHit (A, B, aPlane, t) Solution is t = intersection-t (A,B,aPlane) A B Q Q' Point::CanHit (A, B, aSphere, t) Solution is t = intersection-t (A,B,aSphere) Making use of the sophisticated functions previously defined

58 Point Versus Polygon

59 How can you tell if you hit a Polygon?
For a n-sided polygon, you have to build n+1 planes 1. normal facing away (no hit) N 2. plane parallel to ray (no hit) NOT SHOWN 3. inside polygon N 0. Build a face plane 1. Shoot a ray at the face plane This is the intuitive explanation 4 cases 2. Build side planes. Determine if the hit point is on the positive side of ALL "inward facing side planes" 4. outside polygon

60 Point Versus Polygon: Detailed Version
Let P = A + (B-A) t t = 0 at A t = 1 at B Point::CanHit (A, B, aPolygon, t) P = A + (B-A) t = A+D t Let aPolygon consists of points P0, P1, …, Pn-1 Every time we build a normal, we normalize it. We can only return a hit at the very end, but we can return no hit sooner P4 = Pn-1 P3 Build face normal N = (P1 – P0) x (P2 – P0) 1 P0 N Build face plane with N and P0 F = [Nx, Ny, Nz, -N.P0] 2 P2 P1 3 Can we hit the face plane if (!Point::CanHit (A, B, plane F, t)) return false; t = N.(P0-A) / N.(B-A) If we get to here, t has been computed Next slide continues

61 Point Versus Polygon: Detailed Version
Point::CanHit (A, B, aPolygon, t) P = A + (B-A) t = A+D t P4 = Pn-1 P3 Nn P P0 N Compute point for given t Compute P = A + (B-A) t 4 P2 Loop for n = 0 to n-2 Consider 2 consecutive points Pn and Pn+1 5 P1 P0 and P1 the first time Nn  (P2 – P1) and Nn  N Nn is side plane normal Build normal Nn = N x (Pn+1 – Pn) (P1 – P0) the first time Build side plane Sn = [Nnx, Nny, Nnz, -Nn.Pn] P0 the first time if (P is NOT on the positive side of Sn) return false (no hit) P0 the first time If we get here, the t of step 4 represents a hit point inside the polygon. Return true (a hit) 6

62 Point Versus Box or Convoid

63 Point Versus Box or Convoid
Point::CanHit (A, B, aBox, t) P = A + (B-A) t = A+D t Let P = A + (B-A) t t = 0 at A t = 1 at B A box has 6 pre-built faces A bounding box must have it's 6 faces materialized At most 3 will be front facing and at most 1 will be hit if (Point::CanHit (A, B, P, t)) return true (a hit) For each polygon P Otherwise, return false (no hit) Point::CanHit (A, B, aConvoid, t) Let bestT = 2.0 //Implying no hit so far if (Point::CanHit (A, B, P, t)) Replace bestT by minimum (bestT, t) For each polygon P Finally, if bestT is still 2, return false (no hit); otherwise set t to bestT and return true (a hit) Resolve the issue with Point CanHit (A, B, aPolygon)

64 Point Versus Cylinder or Capsule
NOT DONE YET

65 Section What's wrong with just point sweeping?

66 What's Wrong with Point Sweeping

67 Going Through Keyholes
a keyhole a large object a keyhole A B side view front view A point sweep indicates that the point can go from A to B

68 Other Things You Shouldn't be able to Go Through
Cracks in a building Narrow openings from one canyon area to another Windows Plank walkways Doors slightly ajar Gratings

69 Other Things You Shouldn't be able to Do
Because the faster you go, the more likely you are to go through walls Besides, if you end up inside an object, how do you back out. You can't compute where you would go without collision detection and then compute if you are hitting an object once you get there

70 An object of roughly the same size needs to be swept.
Implications Objects cannot be moved without some form of sweeping. Large objects like players and jeeps cannot be moved with point sweeping. An object of roughly the same size needs to be swept. The more the swept object approximates the shape of the real object, the more accurate the collision detection will be.

71 Section Conclusion What's wrong with just point sweeping?
Large objects can go through arbitrarily small openings like keyholes, cracks between doors, jumbled up piles of debris, windows

72 Section How much more complicated is sphere sweeping compared to point sweeping? Sphere::CanHit (A, B, aPlane, t) Sphere ::CanHit (A, B, aSphere, t) Sphere ::CanHit (A, B, aPolygon, t)

73 Sphere Sweeping

74 Prelude to Understanding Only partial explanations in this section
Sphere Versus Plane Sphere Versus Sphere Only partial explanations in this section

75 Sweeping a Sphere Against a Plane
B Sphere radius R C R plane P1 A plane P2 equivalent Move a sphere from A to B through plane P1 Memorize the hit point C Move the plane out by R to P2 Move a point from A to B through plane P2 You get the same the hit point C

76 Sweeping a Sphere Against a Sphere
Sphere2 radius R2 R1 R2 B Sphere1 radius R1 C A equivalent Move sphere1 from A to B through sphere2 Memorize the hit point C Change radius of sphere2 to R1 + R2 Move a point from A to B through sphere2 You get the same the hit point C

77 How do we translate a Plane OUT by R
Q' = Q + R*N Translated Q Q Q Plane P = [a,b,c,d] Translated plane P' = [a,b,c,-N.Q'] original plane plane translated by R How do you get Q from plane [a,b,c,d]? Just add an appropriate factor of N to Q 2 Ways

78 RECALL: One Way BUT NOT A GOOD WAY
Point Plane::anyPoint () { //With plane [a,b,c,d] denoting [nx,ny,nz,-N.P0] of the plane N.(P-P0) = 0, //N = [a,b,c], P = -dN (a vector re-interpreted as a point and scaled by –d) //is a point on the plane since the distance to the plane is // P.(N-P0) // = (-dN)(N-P0) // = -dN.N - N.P0 // = -d – N.P0 // = -(-N.P0) – N.P0 // = +N.P0 – N.P0 // = 0 return normal * -d; } inline Point3D pointOnPlaneFromMinusPDotN () const {return normal * minusPDotN.negated ();} [a, b, c, d] You can ask aPlane.anyPoint () A disadvantage: You don't know where it is! If it's a kilometer away, it will be innacurate

79 Another Way: Translating a Plane OUT by R
Start with a known point (anywhere at all) A good choice: If moving from A to B. Choose B (or A) It doesn't matter if B is on the positive or negative side N B Let Q = Projection (B, aPlane) Q Then, pullout by R Q' = Q + R N pullout radius Q is on the plane N B Q R Q' Finally, build a new plane through Q' [Nx, Ny, Nz, -N.Q'] same normal new point

80 Sphere Versus Plane Sphere Versus Sphere
Complete explanation

81 Sphere Versus Plane OR Sphere
P = A + (B-A) t = A+D t Let P = A + (B-A) t = A+Dt t = 0 at A t = 1 at B D = B-A Sphere1::CanHit (A, B, plane1, t) where R is the radius of the sphere Discussed above Let plane2 be plane1 moved by out by R Return Point::CanHit (A, B, plane2, t) Sphere::CanHit (A, B, aPlane, t) Sphere1::CanHit (A, B, sphere2, t) Sphere ::CanHit (A, B, aSphere, t) where R1 is the radius of the sphere1 and R2 is the radius of the sphere2 Sphere ::CanHit (A, B, aPolygon, t) Discussed above Let sphere3 have radius R1+R2 Return Point::CanHit (A, B, sphere3, t)

82 Sphere Versus Polygon Ready
It's so complicated, we'll have to do it in stages A bit of an explanation why it's complicated Peliminaries useful for the full explanation Inverse sweeping of INFINITE lines Ready Inverse sweeping of FINITE lines The full solution Orange slides like this will progress through steps

83 Sphere Versus Polygon Step 1 of 5 steps
A bit of an explanation why it's complicated Peliminaries useful for the full explanation Inverse sweeping of INFINITE lines Inverse sweeping of FINITE lines The full solution

84 Sweeping a Sphere Against a Polygon
B Sphere radius R polygon P1 C N A polygon P2 R This is sweeping a point against a translated polygon This is sweeping a sphere against a polygon This is moving the polygon in by R in the direction of N If it intersects, that should be a valid hit Is it possible for a point to miss the polygon and yet the sphere hits it?.

85 Sweeping a Sphere Against a Polygon's Edges
B Sphere radius R N polygon P1 polygon P2 separated by R A This Sphere Hits But the Point doesn't

86 A Side View B A Sphere radius R N
top view front view The Point sweep doesn't even hit the plane Yet the sphere sweep hits the edge Maybe the edges need to be replaced by cylinders TOO HARD

87 Sphere Versus Polygon Step 2 of 5 steps
A bit of an explanation why it's complicated Preliminaries useful for the full explanation Inverse sweeping of INFINITE lines Inverse sweeping of FINITE lines The full solution

88 Solving Vector Equations
Short Aside 1 Solving Vector Equations

89 Solving Vector Equations
Consider a vector equation A t1 + B t2 + C t3 + D = 0 How do you solve for t2, say? Trick1: Use Dot product to convert to numbers A. to each term A.B is now just a number Trick2: Use Cross product to eliminate vectors Cxto each term CxC is now [0,0,0] = 0 Beware using dot product too soon since cross products can no longer be applied

90 Solving Vector Equations
Consider A t1 + B t2 + C t3 + D = 0 Eliminate A: apply "Ax" to both sides A t B t C t D = A x 0 + AxB t2 + AxC t3 + AxD = 0 Rewrite E t2 + F t3 + G = 0 and repeat E = AxB F = AxC G = AxD where

91 Solving Vector Equations
Consider E t2 + F t3 + G = 0 Eliminate A: apply Ex to both sides E t F t G = E x 0 + ExF t3 + ExG = 0 Rewrite H t3 + J = 0 and repeat H = ExF J = ExG where

92 Solving Vector Equations
Consider H t3 + J = 0 Eliminate Vectors (choice 1): apply H. to both sides H t J = H. H x t3 = -H.J / H.H Eliminate Vectors (choice 2): apply J. to both sides H t J = J. J x t3 = -J.J / J.H

93 Eliminating Extraneous Variables
t3 = -H.J / H.H where H = ExF J = ExG where E = AxB F = AxC G = AxD If you want a symbolic as opposed to a constant solution It's a mess

94 How many Equations versus Unknown
A t1 + B t2 + C t3 + Dt4 + E = 0 How many unknowns? 4 How many equations? 1 Don't know the answer 3 As many as there are variables 4

95 Short Aside 2 C A B t = ? Call this process: "Solve for t in line from A to B with plane point C"

96 Solving for t A B C Consider moving an edge from A to B via t.
Let C be a point on the plane swept by the moving edge. What is the value of t where the edge intersects C? A B P = A + (B-A) t = A+D t t = ? C Call this process: "Solve for t in line from A to B with plane point C"

97 Solving for t Let the end opposite A be X. P = A + (B-A) t
C X The equation of the top line P = A + (B-A) t t goes right Start at A, go right. The equation of the middle line C = P + (X-A) t1 Start at P, go down. t1 goes down Can we solve for t? The KNOWNS: A, B, X, C

98 C A B t = ? X Y Solving for t Call this process: "Solve for t in line from A to B with plane point C" X A B t = ? C P P = A + (B-A) t C = P + (X-A) t1 The KNOWNS: A, B, X, C Compare the two P's C - (X-A) t1 = A + (B-A) t (C-A) - (X-A) t1 = (B-A) t Move A from right to left Recall: MxM = 0 for any M (cross product law) Solve for t Multiply both sides by x(X-A) to make t1 go away = 0 (C-A)x(X-A) - (X-A)x(X-A) t1 = (B-A)x(X-A) t ((B-A)x(X-A) t = (C-A)x(X-A) Rearrange and solve for t Solution to Rt = S. Apply .R to both sides to get (R.R)t = S.R Looks complicated but it's not t = [(C-A)x(X-A)].[((B-A)x(X-A)] / [((B-A)x(X-A)].[((B-A)x(X-A)]

99 Sweeping a Complicated Object

100 Sweeping Parts of a Complicated Object
Suppose we have a complex object moving from A to B A B P1 P1' Pick any part P1. It moves from P1 to P1' where So it's easy to transfer the movement to parts P1' = P1 + (B-A)

101 Inverse Sweeping is Sometimes Easier

102 Sometimes It's Easier to Solve the Inverse Problem
Instead of sweeping a sphere against a polygon. Consider sweeping parts of a polygon against a sphere parts of a polygon: points, lines, planes Let's start with line sweeping. A line can be represented by 2 points Point P1, Point P2 with any points on the line represented by P1 + (P2-P1) t for some t There are 2 kinds of lines infinite lines where t is NOT BOUNDED finite lines where 0 ≤ t ≤ 1 Which one is likely to be harder to solve?

103 Infinite Line Sweep Versus Finite Line (or Edge) Sweep

104 Discussion: Sweeps Versus Inverse Sweeps
What's the difference between sweeping a sphere from A to B (to see if you hit an edge) d Let V = B-A A A+V And sweeping an edge from B to A (to see if you hit a sphere) V1 V1-V There is no difference But one may be easier A V2-V V2

105 "Infinite Line / Edge Sweep Against Sphere"
Here's an infinite line sweep that hits the sphere midsection Here's an edge sweep (finite line) hitting midsection too sweep direction D edge t = 0.6 A B t = 0.5 infinite line sphere Finite lines can move further than infinite lines

106 Sphere Versus Polygon Step 3 of 5 steps
A bit of an explanation why it's complicated Preliminaries useful for the full explanation Inverse sweeping of INFINITE lines Inverse sweeping of FINITE lines The full solution

107 Infinite Line Versus Sphere

108 "Infinite Line Sweep Toward Sphere"
Imagine sweeping an infinite line toward a sphere sweep direction D the line is hitting top or bottom part of sphere What just happened? A B line sphere

109 2 Views of the Same Sweep

110 "Infinite Line Sweep Sphere"
sweep direction D B A sphere line Top View B C center sweep plane sphere Front View A Looking INTO the line

111 Showing The Steps To Finding The Hit Point

112 Steps in "Infinite Line Sweep Toward Sphere"
sweep direction D P1 P3 B A sphere line Top View P2 P1 P3 B 1. P1 = projection (C, sweep plane) C is sphere center 2. P2 = projection (P1, sweep line) C center 3. P3 = Intersect line P1 to P2 with sphere 4. Convert P3 to a suitable t-value sweep plane sphere P2 Front View A

113 Infinite Line Versus Sphere
InfiniteLine::CanHit (A, B, sphere, t) P = A + (B-A) t = A+D t where R is the radius of the sphere AND C is it's center Let E be the direction of the infinite sweep line Build sweep plane in the line sweep direction from normal N = (B-A) X E and point A 1. Find the projection of the sphere center onto the sweep plane t = 0.2? 1. P1 = projection (C, plane) top view If the sphere is completely above or below the plane, i.e., |P1-C|  R, there is no solution E lineA A B lineB C R P1 2. Drop a perpendicular from P1 to lineA P2 2. P2 = projection (P1, sweep lineA) P3 3. The line from P1 to P2 must intersect the sphere side view (looking INTO the lines) 3. P3 = Intersection (line P1 to P2 with sphere) A B N 4. Solve for t in line from A to B intersecting with plane point P3 t = 0.2? There is a solution if 0 ≤ t ≤ t1 P3 P1 P2

114 Sphere Versus Polygon Step 4 of 5 steps
A bit of an explanation why it's complicated Peliminaries useful for the full explanation Inverse sweeping of INFINITE lines Inverse sweeping of FINITE lines The full solution

115 "Finite Line Sweep Toward Sphere"
Imagine sweeping a finite line toward a sphere Assuming an infinite sweep was done earlier sweep direction D no hit one END hit END hit A B line sphere This is equivalent to 2 point sweeps

116 Sphere Versus Polygon Step 5 of 5 steps
A bit of an explanation why it's complicated Peliminaries useful for the full explanation Inverse sweeping of INFINITE lines Inverse sweeping of FINITE lines The full solution

117 Putting it All Together

118 Sphere Versus Polygon: Observations
Sphere sweep (radius R) against polygon Can point sweep with the face plane if we move the plane inward by R That intersection point could turn out to be outside the polygon If it's inside, is this the final solution? YES, further tests will have larger t Reverse "infinite line sweeping" from the polygon can be done By infinite line sweeping the edges back toward the sphere Pick the minimum t of all the lines Reverse "point-sweep" of the edge points. This has larger t than others. Pick minimum Did we forget anything

119 Sphere Versus Polygon

120 Sphere Versus Polygon Sphere::CanHit (A, B, polygon1, t) if
P = A + (B-A) t = A+D t Sphere::CanHit (A, B, polygon1, t) where R is the radius of the sphere if Let plane2 be polygon1's plane1 moved out by R Point::CanHit (A, B, plane2, t) if (P) is inside polygon2's side planes, return hit with this t Otherwise If there was a hit, return hit with the minimum t for each edge E of polygon1, find the minimum one of InfiniteLine::CanHit (B, A, sphere, t) Point::CanHit (B, A, sphere, t) Otherwise for each vertex V of polygon1, find the minimum one of If there was a hit, return hit with the minimum t Return that there is no hit Otherwise

121 Section Conclusion Sphere::sweeping is much more complicated
InfiniteLine::CanHit (A, B, sphere, t) Needed this additional capability Sphere::CanHit (A, B, aPlane, t) Sphere ::CanHit (A, B, aSphere, t) Sphere ::CanHit (A, B, aPolygon, t)

122 Section Convoid sweeping Is it hard?
Does anything special have to be done to improve efficiency?

123 Convoid Versus Convoid

124 Convoid Versus Convoid
We already know how to handle "sphere versus polygon". So, if we could handle "polygon versus polygon" and hence "convoid versus convoid", we could handle every other form. Barrels, cylinders and capsules excluded

125 Showing Why Point Sweeps and Edge Sweeps are adequate

126 Left polygon sweeps right Right polygon sweeps left
"Polygon Sweep Polygon" Consider point and edge sweeping sweep direction D A B Are there point intersections with faces and edge intersections with edges Left polygon sweeps right polygon1 polygon2 Right polygon sweeps left

127 Looking a Point Sweeps Edge On
far rectangle smaller than near far far near near Looking at the sweep direction from the top Looking INTO the sweep direction

128 Polygons Can Have Very Different Shapes
Depending on which one is moving, either get a point intersection edge intersection Looking INTO the sweep direction

129 We want to fill in this table
Point Sweep is enough Edge Sweep is enough One Polygon Entirely Inside Another ? Both polygons Exactly the same Polygons Overlap We want to fill in this table

130 Showing Far to Near Sweep
Small object sweeping into a large object far 4 Hits Including center hit near Looking INTO the sweep direction

131 Showing Near to Far Sweep
Large object sweeping into a small object Far to near sweep far 4 Misses Hits the plane but not the face near Looking INTO the sweep direction

132 Point Sweep is enough Edge Sweep is enough One Polygon Entirely Inside Another ? Both polygons Exactly the same Polygons Overlap

133 If both objects have the same size when sweeping
Near to Far Sweep If both objects have the same size when sweeping 4 Grazing Points far What about centers Let C1 be the center of Polygon1 Let C2 be the center of Polygon2 near Corners NOT DECISIVE If we sweep centers both ways Both HIT Looking INTO the sweep direction

134 Point Sweep is enough Edge Sweep is enough One Polygon entirely Inside Another ? Both polygons exactly the same Include their mid-points Polygons Overlap

135 Shapes NOT Fitting into Each Other
Far to near sweep 8 Edge Intersections far Like these near Near to far sweep 8 More Edge Intersections NOT SHOWN Looking INTO the sweep direction

136 Polygons Can Have Very Different Shapes
If neither is fully inside the other, at least one edge of one will intersect with an edge of the other

137 Point Sweep is enough Edge Sweep is enough One Polygon entirely Inside Another Both polygons exactly the same Include their mid-points Polygons Overlap

138 Polygon to Polygon Sweep
Polygon (or Convoid) sweeping involves Point sweeping of each other's vertices Plus an extra center to center point sweep Edge sweeping from the first polygon to the second Assumes we have edge sweeping figured out

139 Edge Versus Edge Sweep

140 This Function involving NEW MATH
sweep direction B-A By sweeping one edge into another, we wish to find the intersection (if there is one)

141 How Do We Do It t Pt p2 p1 q3 R S s p3 r q2 q1 Qt
sweep direction from A to B t Pt Qt p2 p1 q3 R s S r p3 q2 q1 Let Pt = p1 + (B-A) t Let Qt = q1 + (B-A) t Let R = Pt + (Qt-Pt) s R is on the Pt-Qt edge, the s-direction Let S = p3 + (q3-p3) r S is on the r-direction Intersection where R = S

142 How Do We Do It? Copying Equations Over Pt = p1 + (B-A) t
Qt = q1 + (B-A) t R = Pt + (Qt-Pt) s Intersection where R = S S = p3 + (q3-p3) r R = Pt + [Qt-Pt] s = S = p3 + (q3-p3) r ( p1 + (B-A) t ) + [( q1 + (B-A) t )-( p1 + (B-A) t )] s = p3 + (q3-p3) r (B-A) t cancels (p1 - p3) + (B-A) t + (q1-p1) s + (p3-q3) r = 0

143 All we want is to solve for t
Final Result (p1 - p3) + (B-A) t + (q1-p1) s + (p3-q3) r = 0 of the form A + sB + tC + rD = 0 All we want is to solve for t If we wanted the intersection point, we would solve for r, then S. S = p3 + (q3-p3) r

144 3. You are now left with a t term
Gist of Solution Eliminate a term like "t1 E" by multiplying by xE; cross product Convert to non-vectors by using .(B-A); dot product Solution is a fraction. No solution if denominator is 0. A + sB + tC + rD = 0 1. xB to eliminate s term 2. xD to eliminate r term 3. You are now left with a t term 4. .(B-A) to solve for t

145 Section Conclusion Polygon::sweeping involves
Point sweeping of each other's vertices Edge sweeping of the first polygon edges with the second Plus an extra center to center point sweep For efficiency, a polygon needs to track shared vertices a mechanism to avoid testing the same vertex or edge twice + shared edges

146 Cylinder / Barrel / Capsule Collision Detection
Advanced Collision Detection Cylinder / Barrel / Capsule Collision Detection

147 Cylinder / Barrel / Capsule Collision Detection

148 Capsule Collision Detection
There are 4 capsule variations… 1. An infinite cylinder. a point could go through the center 2. A finite cylinder (no end caps). a point could go through the center 3. A cylinder with flat ends; i.e., a barrel 4. A cylinder with round ends; i.e., a capsule Christer Ericson, Real-time Collision Detection

149 Infinite Cylinder Collision Detection
Representing a Cylinder 1. Two end points P and Q. Let d = Q-P. 2. A radius r r P d Q For a finite cylinder, expect to hit between P and Q…

150 Sweeping a Point to Hit an infinite Cylinder
Sweep a point from A to B Determine where and if it hits the infinite cylinder with radius r and endpoints P and Q A sample hit points r P d Q B For a finite cylinder, expect to hit between P and Q…

151 Solution: Point Sweep from A to B intersecting Cylinder
sample hit points A Solution without derivation r Y P Q X B Let m = A-P Let n = B-A Let d = Q-P At2 + B t + C = 0 0 real solutions: no intersection 1 real solution (grazing): no intersection where A = (d.d) (n.n) – (n.d)2 2 real solutions: pick the one with smallest t B = 2(d.d) (m.n) – 2 (m.d)(n.d) For a finite cylinder, no intersection if t outside range 0 to 1. C = (d.d) [(m.n) – r2] – (m.d)2

152 The Equation of an infinite Cylinder
Pick a point X on the surface and drop a perpendicular to point Y on the line from P to Q sample hit points A The derivation r X Y P Q B Let R = Y-X Any point is a fixed distance r from the PQ-axis The equation of the cylinder is |R|2 = r2

153 The Equation of an infinite Cylinder
Let v be the vector from P to X and w be the vector from P to Y. Let R be Y-X so that the cylinder equation is |R|2 = r2. sample hit points A r v w Y P R Q X B Rewriting the equation in terms of v and w v + R = w v – w = -R (v – w).(v – w) = r2

154   Derivation 1. Start with cylinder equation |R|2 = r2.
2. Replace by dot product version R.R = r2. f (R) 3. Replace R by a function of v, w. f (v,w) 4. Replace w by a function d. f (v,d) 5. Replace v by a function of t. f (t) 6. Solve for t f (t) = 0 The result is a quadratic of the form At2+Bt+C = 0

155 Step 3: Replace R by a function of v, w, d.
sample hit points r v w Y P R Q X B Rewriting equation R.R = r2 in terms of v and w v + R = w See diagram v - w = -R Reworking equation R.R = r2 (-R).(-R) = r2 (v-w).(v-w) = r2 R.R = --R.R. Substitute for -R (v – w).(v – w) = r2

156 Step 4a: Replace w by a function d.
Let d = Q - P r w Y P Q v X w = "the shadow of v" in the direction d w = "the shadow of v" * (d / |d|) vector / length = unit vector w = "|v| cos " * (d / |d|) for some angle  definition of shadow w = (|d| / |d|) |v| cos  * (d / |d|) for some angle  Multiply by 1 = |d| / |d| trying to make it look like dot product w = |d| |v| cos  * (d / |d|2) for some angle  simplifying w = (d.v / |d|2) d A.B = |A| |B| cos 

157 Step 4b: Substitute for w in the cylinder equation
Let d = Q - P r w Y P Q v X (v – w).(v – w) = r2 equation of cylinder w = (d.v / |d|2) d w as a function of d (v – (d.v / |d|2) d).(v – (d.v / |d|2) d) = r2 substitute for w × |d|2 × |d|4 (|d|2 v – (d.v) d).(|d|2 v – (d.v) d) = |d|4 r2 Multiply both sides by |d|4 Continued on next slide

158 Step 4b: Substitute for w in the cylinder equation
Let d = Q - P r w Y P Q v X (|d|2 v – (d.v) d).(|d|2 v – (d.v) d) = |d|4 r2 From previous slide ((d.d) v – (d.v) d).((d.d) v – (d.v) d) = (d.d)2 r2 |d|2 = d.d (A-B).(A-B) = A.A-2A.B+B.B A -B (d.d)2(v.v) – 2(d.v)(d.d)(d.v) + (d.v)2(d.d)) = (d.d)2 r2 A.A -2A.B B.B (d.d)(v.v) – 2(d.v)2 + (d.v)2 = (d.d) r2 divide by ONE d.d (d.d)(v.v) – (v.d)2 = (d.d) r2 -2X+X = -X

159 Step 5: Replace v by a function of t.
Let d = Q - P Let X be the equation of the line from A to B r w Y P Q v X B (d.d)(v.v) – (v.d)2 = (d.d) r2 From previous slide v = X - P from diagram X = A + t*(B-A) equation of the line from A to B v = A + t*(B-A) - P substitute for X Let m = A-P v = A-P + t*(B-A) Make it look simpler Let n = B-A v = m + t n Continued on next slide

160 Step 5: Replace v by a function of t.
(d.d)(v.v) – (v.d)2 = (d.d) r2 From previous slide (end of step 4b) Let m = A-P From previous slide v = m + t n Let n = B-A v.v = m.m+2m.nt+n.nt2 (A+B).(A+B) = A.A+2A.B+B.B v = m + t n (v.d) = (m.d+n.dt) Post multiply on both sides by .d (v.d)2 = (v.d).(v.d) = (m.d+n.dt).(m.d+n.dt) = (m.d)2+2(m.d)(n.d) t+(n.d)2 t2 Substitute v.v and v.d into the first equation (d.d)(m.m+2m.n t+n.n t2) – (m.d)2+2(m.d)(n.d) t+(n.d)2 t2 = (d.d) r2 At2 + B t + C = 0 0 real solutions: no intersection 1 real solution (grazing): no intersection where A = (d.d) (n.n) – (n.d)2 2 real solutions: pick the one with smallest t B = 2(d.d) (m.n) – 2 (m.d)(n.d) C = (d.d) [(m.n) – r2] – (m.d)2

161 Summary: Point Sweep from A to B intersecting Cylinder
sample hit points A r Y P Q X B Let m = A-P Let n = B-A Let d = Q-P At2 + B t + C = 0 0 real solutions: no intersection 1 real solution (grazing): no intersection where A = (d.d) (n.n) – (n.d)2 2 real solutions: pick the one with smallest t B = 2(d.d) (m.n) – 2 (m.d)(n.d) For a finite cylinder, no intersection if t outside range 0 to 1. C = (d.d) [(m.n) – r2] – (m.d)2

162 Capsule Collision Detection
There are 4 capsule variations… 1. An infinite cylinder. pick smallest t if there is a solution 2. A finite cylinder (no end caps). pick smallest t in range 0 to 1 if there is a solution 3. A cylinder with flat ends; i.e., a barrel build a plane for each end, pick smallest t with hit within r of center 4. A cylinder with round ends; i.e., a capsule build a sphere for each end, pick smallest t which hits sphere Christer Ericson, Real-time Collision Detection (pages )

163 Summary: Point Sweep For a Flat End Cap
sample hit points r Y P Q X B To build a plane, you need a normal and a point on the plane N P For end P Let normal N = normalized (P-Q) to point outward Let point P0 = P The plane is Nx.x+Ny.y+Nz.z – N.P0 = 0 Once you find t (if it exists), compute intersection from P1 = A+(B-A) t No solution if | P1-P0 | >= r For end Q Let normal N = normalized (Q-P) Let point P0 = Q Same idea as above

164 Cylinder / Barrel / Capsule Collision Detection
Advanced Collision Detection Conclusion Point::sweeping over Cylinder / Barrel / Capsule Collision Detection Other forms of sweeping Not done Haven't you had enough

165 Was the COLLISION DETECTION takeway achieved
Plane Point Point Sphere Sphere Polygon Polygon colliding with a Box Box Cylinder Cylinder including Bounding box Only Point Sweeping done Barrel Barrel Capsule Capsule Convoid Convoid Bunch of polygons Was the COLLISION DETECTION takeway achieved


Download ppt "Collision Detection Slide 27: Intersection of 2 Boxes"

Similar presentations


Ads by Google