Document Sample

Introduction Narrow-phase Broad-phase UCT Algorithm Circle: Collision Detection Kosie van der Merwe 24 March 2011 Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase Outline 1 Introduction 2 Narrow-phase 3 Broad-phase Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase What is Collision Detection? In a game or simulation, you sometimes need to know: Whether 2 objects intersect/collide. Where they intersect. When they ﬁrst intersect. This lecture will focus on the ﬁrst point. Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase Considerations How many objects do you have? How much time do you have per frame? At 60FPS you have about 2-5ms just for collision detection. Is time continuous or in steps? Do objects move one at a time or all at the same time? What level of collision detail do you need? Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase What won’t be covered? Vector or matrix math? Numerical stability for ﬂoating point numbers. Techniques speciﬁc to 3D, but most of the techniques can extended to 3D though. Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase A collision detection system A collision detection system consists of two main phases: Broad phase: decide which objects might possibly collide and only them for collision. Narrow phase: test whether 2 particular objects collide. If you test every pair of N objects there will be O(N 2 ) tests. The broad-phase seeks to reduce the number of possibly expensive tests. The narrow-phase might have layers to test going from cheap to expensive, with the property that if any test fails the objects aren’t colliding. Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase Outline 1 Introduction 2 Narrow-phase 3 Broad-phase Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase Objects Objects in the world can be any number of complex shapes. However, we can approximate the objects with: Circles Rectangles (axis-aligned or orientated) Convex Hulls: a convex polygon that encloses objects tightly. These shapes act as bounding “volumes”. They completely encompass the whole object. They are roughly in the order of fastest to slowest and from loosest ﬁt to tightest ﬁt. Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase Axis-Aligned Bounding Box The Axis-Aligned Bounding Box (AABB) is a rectangle that is aligned with the coordinate axes. Additionally, the AABB completely encloses the object. For instance, where the blue box is the AABB: Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase Find the AABB The AABB can be deﬁned by: top: the highest y -value. bottom: the lowest y -value. left: the lowest x-value. right: the highest x-value. Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase Testing 2 AABBs To test if two AABBs intersect we use the following code: function intersect(aabb1, aabb2): return not ((aabb1.left < aabb2.right) or (aabb1.right > aabb2.left) or (aabb1.top < aabb2.bottom) or (aabb1.bottom > aabb2.top)) Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase Orientated Bounding Box The Orientated Bounding Box (OBB) is related to the AABB, but instead of being axis-aligned is rotated. This allows a tighter ﬁt at the cost of a more expensive test. Creating and testing an OBB is out of the scope of this lecture. But you can check out: http://www.ragestorm.net/tutorial?id=22 Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase Circle The bounding circle has: A centre. A radius. To test if 2 circles intersect, check: c1 − c2 ≤ r1 + r2 Alternatively: (c1,x − c2,x )2 + (c1,y + c2,y )2 ≤ (r1 + r2 )2 Circles, other than having a quick test, are easy to rotate. Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase Find the bounding circle Finding a tight ﬁtting circle can be difﬁcult. You have a few option for ﬁnding the centre: Use the centre of the object. Use the centre of the AABB/OBB. Some optimisation algorithm such as Simulated Annealing. Note: don’t take the average of all points, taking the centre of the AABB is better. To ﬁnd the radius, just ﬁnd the furthest point from the centre. Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase Gilbert-Johnson-Keerthi Algorithm The GJK algorithm is used to ﬁnd whether 2 convex polygons intersect. It does this by cleverly using what’s called a Minkowski Sum. Given 2 shapes A and B, described as a set of points. Then the Minkowski Sum is given by: A + B = {a + b : a ∈ A, b ∈ B} Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase Minkowski Sum Here is an example of the Minkowski Sum of 2 squares. Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase Minkowski “Difference” The Minkowski Sum GJK uses is informally called Minkowski Difference This “Difference” is given by: A − B = {a − b : a ∈ A, b ∈ B} This has 2 nice properties: If A and B intersect then the origin is contained in A − B If A and B aren’t intersecting, then the distance between A and B is the distance between A − B and the origin. Although not covered, you can extend GJK to give you the distance between A and B. Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase Minkowski “Difference” Here is an example of the Minkowski “Difference” of 2 squares. Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase Support Function One of the main bits of the GJK algorithm is the support function. Given a direction, d it ﬁnds the point in A − B that is the furthest along that direction. Stated alternatively, it ﬁnds c ∈ A − B such that d · c is maximised. Since A and B are convex polygons, A − B is a convex polygon. Thus we only have to consider the vertices of A − B. The vertices A − B is the convex hull of the vertices of A minus the vertices of B. Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase Support Function So we can write: c = ABij = Ai − Bj for some i and j. Where ABij , Ai and Bj are the vertices of A − B, A and B, respectively. So we can write: max ABij · d = max Ai · d − Bj · d = max Ai · d − min Bj · d = max Ai · d − max −Bj · d = max Ai · d − max Bj · −d Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase Support Function function supportPoly(A, d): maxvert = A[0] for i = 1 to len(A)-1: if dot(maxvert, d) < dot(A[i], d): maxvert = A[i] return maxvert function support(A, B, d): return supportPoly(A, d) - supportPoly(B, -d) Note: you can speed things up by doing pre-computations each polygon to calculate supportPoly faster. Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase GJK: Actual algorithm We can then write the algorithm as: S = ... #choose any point on minkowski difference simplex = [S] d = -S while True: A = support(d) #find point furthest along d if dot(A, d): #if A not past the origin. return False simplex += [A] #add the new point to simplex if containsOrigin(simplex, d): return True simplex = updateSimplex(simplex) d = updateD(simplex) Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase What are simplexes? A simplex is basically a generalisation of a triangle in 2D and a tetrahedron in 3D, to arbitary dimensions. In our 2D GJK we will only use up to 3 points in our simplex. So the simplexes we’ll have to consider are: A point. A line. A triangle. However, due to how the code works, containsOrigin and updateSimplex, will only have to handle lines and triangles. Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase updateSimplex This function takes the simplex and returns the sub-simplex that’s closest to the origin. Note we use the fact that the last added vertex is at the end of the simplex array, to reduce the number of comparisons. Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase updateSimplex - Implementation function updateSimplex(simplex): if len(simplex) == 2: A = simplex[1] B = simplex[0] if dot(B-A, -A) > 0: #sub-simplex is AB return [A, B] else: #sub-simplex is A return [A] Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase updateSimplex - Implementation Continued else: A = simplex[2] B = simplex[1] C = simplex[0] if dot(B-A, -A) > 0: #sub-simplex is AB return [A, B] else if dot(C-A, -A) > 0: #sub-simplex is AC return [A, C] else: #sub-simplex is A return [A] Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase updateD This function return the direction from the simplex to the origin. We’re guaranteed the simplex will be a point or a line. Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase updateD - Implementation function updateD(simplex): if len(simplex) == 1: A = simplex[0] return -A if len(simplex) == 2: A = simplex[1] B = simplex[0] s = sign(cross(B-A, -A)) return vector(-s*(B-A).y, s*(B-A).x) Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase containsOrigin This tests whether a given simplex contains the origin. For the sake of the example, we’ll consider touching polygons to be intersecting. Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase containsOrigin - Implementation function containsOrigin(simplex): if len(simplex) == 2: A = simplex[1] B = simplex[0] return dot(B-A, -A) == 0 else: A = simplex[2] B = simplex[1] C = simplex[0] return cross(B-A, -A) * cross(A-C, A) >= 0 Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase Some comments If you you have rotating objects, instead of rotating the objects, rotate the direction vector. You can handle non-polygon convex shapes, if you implement a support function. If your objects don’t move very much, you can use the last added point from the previous run to speed things up signiﬁcantly. Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase Outline 1 Introduction 2 Narrow-phase 3 Broad-phase Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase Why broad-phase? When 2 objects are on opposite sides of the world they can’t possibly collide. Broad-phase collision detection tries to reduce the number of comparisons. It does this by only considering object pairs that possibly could collide. So it can signiﬁcantly lower the run time from O(N 2 ). Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase Temporal Coherence In general, objects don’t tend to move much from frame to frame. This is called temporal coherence. We can use this fact to update our data structures quickly. Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase Grids If our world is of a ﬁnite size we can divide it into grid cells. Then each cell can contain a list of objects that partially or completely overlap with it. Then to check if an object intersects with any other object, we only consider other objects that share cells with it. If an object moves we can just remove it from all the cells it was in, and add it to the cells that now contain it. Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase Grids Some considerations: Avoid checking a pair of objects twice. Assign objects unique IDs and only compare to objects with lower IDs. Make the cells the right size. Kosie van der Merwe Collision Detection Introduction Narrow-phase Broad-phase Sweep and prune Consider the objects on an axis, with 1D bounding boxes on this axis. This bounding box will have a beginning value and an ending value on this axis. For each axis, we can insert these end-points into an array and sort them. If the start of one object is between the start and end of the other one, they might intersect. Kosie van der Merwe Collision Detection

DOCUMENT INFO

Shared By:

Categories:

Tags:

Stats:

views: | 5 |

posted: | 11/27/2011 |

language: | Dutch |

pages: | 37 |

OTHER DOCS BY wanghonghx

How are you planning on using Docstoc?
BUSINESS
PERSONAL

By registering with docstoc.com you agree to our
privacy policy and
terms of service, and to receive content and offer notifications.

Docstoc is the premier online destination to start and grow small businesses. It hosts the best quality and widest selection of professional documents (over 20 million) and resources including expert videos, articles and productivity tools to make every small business better.

Search or Browse for any specific document or resource you need for your business. Or explore our curated resources for Starting a Business, Growing a Business or for Professional Development.

Feel free to Contact Us with any questions you might have.