1 / 17

Collision Detection

Collision Detection. Soon Tee Teoh CS 134. Collision Detection. In real-life, we have gravity and solid things can’t go through one another. In the virtual world, we need to program collision detection.

Download Presentation

Collision Detection

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.


Presentation Transcript

  1. Collision Detection Soon Tee Teoh CS 134

  2. Collision Detection • In real-life, we have gravity and solid things can’t go through one another. • In the virtual world, we need to program collision detection. • Collision detection is performed by checking for intersections between two objects. If a triangle of Object 1 intersects with a triangle of Object 2, then the two objects have collided. • However, to check for intersections between every pair of triangles in the scene is far too time-consuming. Therefore we use approximation techniques.

  3. Bounding Box • One approximate approximation technique is to find the bounding box of each object. • If the bounding boxes of two objects intersect, we say that they have collided. • This is a conservative estimate, because if the bounding boxes intersect, they two objects may not actually have collided. However, bounding box intersection will certainly detect every actual collision. • Easiest: Axis-aligned bounding box. • How to find the axis-aligned bounding box of an object. • Go through every vertex of the object. Find the minimum and maximum x, y and z coordinates. These coordinates form the bounding box of the object.

  4. Box-Box Intersection • Two objects are considered to have collided if their bounding boxes intersect. • To find if their bounding boxes intersect, just test to see if the x, y and z ranges of their bounding boxes all overlap. • If they all overlap, it means that the two bounding boxes intersect. Otherwise, they don’t intersect.

  5. Bounding Sphere • Alternatively, we can try to get the bounding sphere of an object. • Here is one method to get the bounding sphere: • Step 1: Get the axis-aligned bounding box of the object • Step 2: Set the center of the bounding sphere be the mid-point of the bounding box. • Step 3: Test all the vertices of the object and find the furthest vertex from the center of the bounding sphere. The radius of the bounding sphere is set to the distance of this furthest vertex.

  6. Sphere-Sphere Intersection • Two objects are said to have collided if their bounding spheres intersect. • To determine if two spheres intersect, simply calculate the distance between the centers of the two spheres. • If the distance is greater than the sum of the two sphere radii, they don’t intersect. Otherwise they intersect. r2 r1 d = r1 + r2 d > r1 + r2

  7. Sphere-Plane Intersection • Sometimes, it’s necessary to find the intersection between a sphere and a plane, for example, the bounding sphere of an object with a wall (or slope). • Given that the equation of the plane is n.p = k (where n is the unit normal of the plane, p is any point on the plane, and k is a number) • Then, given the center coordinates C of the sphere, and the radius r of the sphere, • The sphere and the plane intersect if |(n.C) – k| < r

  8. Dot Product • Let U and V be vectors such that U = (Ux, Uy, Uz), and V = (Vx, Vy, Vz) • Then, the dot product U.V = UxVx + UyVy + UzVz • U.V is also equal to |U||V| cos q where q is the angle between U and V. U V q

  9. Collision of Fast-Moving Objects • We need a different method to detect collision of fast-moving, and often small, objects. • Example, a bullet is fired, and we want to see if it intersects a wall. However, if we examine every time frame, because the bullet moves very fast, even though at some point in time it intersects the wall, we may only sample it in front of the wall and behind it, but on at the point of intersection. • Therefore, we need to consider the path of the bullet, and determine if that path intersects the wall. • We use a line to represent the path of the bullet. We then test for line-object intersection. We consider: • Line-Sphere intersection, and • Line-Triangle intersection

  10. Line-Sphere Intersection • Let a point on a line be X(t) = P + tD • Here X(t) is a function of t, and gives the point on the line, P is the starting point of the line, and D is a unit vector in the direction of the line. • Let a point on a sphere satisfy | X – C | = r • Here, X is a point on the sphere, C is the center of the sphere, and r is the radius of the sphere.

  11. Line-Sphere Intersection • Suppose the line and sphere intersect at point X, then | P + tD – C |2 – r2 = 0 • Let M = P – C. Then, | tD + M |2 – r2 = 0 • Expanding, t2 + 2D.Mt + | M |2 – r2 = 0 • See Note 1 on next page • Solving for t, t = -D.M +/- sqrt((D.M)2 – ( | M |2 – r2 )) • See Note 2 on next page • The discriminant d is (D.M)2 – ( | M |2 – r2 ) • If d > 0, the line and sphere intersect at two points. • If d = 0, the line and sphere intersect at one point. • If d < 0, the line and sphere don’t intersect. • If (d>0) or (d=0), we can solve for t. Assuming that P is the position of the fast-moving object at the beginning of the game loop, and D is the vector that it will travel during a game loop, then the objects intersect during this game loop if 0<t<1.

  12. Notes on Line-Sphere Intersection Note 1 Note 2 |a+b|2 = |a|2 + |b|2 + 2a.b Let A, B and C be coefficients of the quadratic equation: Ax2 + Bx + C = 0 Then, x = -B +/- sqrt(B2-4AC) 2A b by a+b ay a ax bx Proof: |a+b|2 = (ax+bx)2 + (ay+by)2 = ax2+ 2axbx + bx2 + ay2+ 2ayby + by2 But, |a|2 = ax2 + ay2 and |b|2 = bx2 + by2 Therefore, |a+b|2 = |a|2 + |b|2 + 2(axbx + ayby) |a+b|2 = |a|2 + |b|2 + 2a.b

  13. Line-Triangle Intersection • Once again, let a point on a line be X(t) = P + tD • Let a triangle be defined by its three corner points P0, P1 and P2. • Strategy: • First, find the intersection between the line and the plane containing the triangle. • Then, find out if this point is within the triangle

  14. Line-Triangle IntersectionThe Plane Containing the Triangle Equation of a plane: A point p on the plane will satisfy the equation p.n = k where n is the normal of the plane. Step 1: Find the normal n of the plane Let edge e0 be P1 – P0. Let edge e1 be P2 – P1. Then n = e0 x e1 In other words, the normal of the plane is the cross product of two edges. Step 2: Find k k = P0.n

  15. Line-Triangle IntersectionLine-Plane Intersection 1. Substitute equation of the line into equation of the plane. (P + tD) . n = k 2. Find t. Re-arranging, t = (k – P.n)/(D.n) 3. Substitute t back to get intersection point. Intersection point R = P + tD.

  16. Line-Triangle IntersectionCheck if point is within triangle Remember that the cross product of consecutive vectors going counter-clockwise will always be of the same sign. R is inside the triangle if it is always to the left side of each edge. P2 e1 e2 R P0 e0 P1 Therefore, Point R is inside the triangle if: (e0 x (R – P0)) . n > 0 and (e1 x (R – P1)) . n > 0 and (e2 x (R – P2)) . n > 0

  17. Speeding up Collision Detection • Spatial subdivision method • Divide the space into different regions. • At each step, determine which region each object is in. • Only test objects in the same region for collision.

More Related