Collisions and Intersections • When objects move, test for collision. • When projecting surfaces, check for intersections. (Many slides adapted from Amitabh Varshney) Collision Strategy • Brute force test. Fine if few shapes. • Test by bounding with simpler shape. – Only do brute force if necessary. • Use hierarchy of simpler shapes. – Faster for complex scenes. 2D Intersections • Sides intersect • Or one inside other Sides Intersect • Intersect lines and check whether intersection point is inside each line segment. • Check if each line divides other line segment in half. How to tell whether two line segments intersect. First, convert to equations for lines. (x1,y1) (x2,y2) goes to y = (y2-y1)/(x2-x1)x + (y1(x2-x1) – x1(y2-y1). Now suppose we have two lines, y = m1x+b1, y = m2x+b2. Solve for x and y. For example, we have m1x+b1=m2x+b2. x = (b1-b2)/(m2-m1). Now we want to know whether (x,y) is in between (x1,y1) and (x2,y2), and the same for the other line segment. One simple way is to tell whether the sum of the distance from each end point to (x,y) is the same as the distance between the end points. This is a bit cumbersome. We can do things with less computation by checking whether the line of line segment one divides the endpoints of line segment two, and vice versa. To do this, compute the line the first line segment lies on, and represent it as: ax + by = c, where (a,b) is a unit vector. Then compute (a,b)*(x1,y1) = c1, and (a,b)*(x2,y2) = c2. We should have either c1 <= c <= c2, or c2 <= c <= c1. One shape inside another • Easier for convex shapes. – Convex polygon is inside iff all vertices are inside. – Vertex is inside iff it is on inside of each side. • If shape isn’t convex, decompose it into convex shapes. Recall from before how to tell if a point is inside a convex shape, by judging whether it is on the appropriate side of each bounding line of the shape. We can write a line as (a,b)*(x,y) = c. Then a side of the line is (a,b)*(x,y) >= c (or <=c). Circles easier • Testing whether two circles intersect is much easier. • So put a circle around each shape (How?) (x2,y2) (x1,y1) r1 r2 (x2-x1)2 + (y2-y1)2 > (r1+r2)2 Circles • Pros: Very fast. – If scene is fixed, circles for scene can be computed once. – If object moves, circle can move with it easily. • Cons: Circle can exaggerate shape. Axial Rectangles • A rectangle with sides aligned with the x and y axes. – Easy to generate. Just take min and max x and y values. – Easy to check for intersection. • Don’t intersect if one max value less than other’s min value. Hierarchical • Keep subdividing space into axial rectangles: quadtrees. – Bound everything with axial rectangles. – Divide space into four squares. Does object share a square with the scene? – If yes, recurse. – At some point just check. • Many related, more complicated strategies possible. Algorithms for Visibility Determination • Object-Order – Sort the objects and then display them • Image-Order – Scan-convert objects in arbitrary order and then depth sort the pixels • Hybrid of the above Painter’s Algorithm • Object-Order Algorithm • Sort objects by depth • Display them in back-to-front order Painter’s Algorithm Second Fourth Third First Painter’s Algorithm • • • • Sort polygons by farthest depth. Check if polygon is in front of any other. If no, render it. If yes, has its order already changed backward? – If no, render it. – If yes, break it apart. Which polygon is in front? Our strategy: apply a series of tests. – First tests are cheapest – Each test says poly1 is behind poly2, or maybe. 1. If min z of poly1 > max z poly2, 1 in back. x B 2. The plane of the polygon with smaller z is closer to viewer than other polygon. A (a,b,c,)*(x,y,z) >= d. z x B A 3. The plane of polygon with larger z is completely behind other polygon. z 4. Check whether they overlap in image a. Use axial rectangle test. b. Use complete test. y y x Non-Overlapping x or y x Overlapping projection x B A B is on one side of A z Problem Cases: Cyclic and Intersecting Objects Painter’s Algorithm • Solution: split polygons • Advantages of Painter’s Algorithm – Simple – Easy transparency • Disadvantages – Have to sort first – Need to split polygons to solve cyclic and intersecting objects Z-Buffer Algorithm • Image precision, object order • Scan-convert each object • Maintain the depth (in Z-buffer) and color (in color buffer) of the closest object at each pixel • Display the final color buffer • Simple; easy to implement in hardware Z-Buffer Algorithm for( each pixel(i, j) ) // clear Z-buffer and frame buffer { z_buffer[i][j] = far_plane_z; color_buffer[i][j] = background_color; } for( each face A) for( each pixel(i, j) in the projection of A) { Compute depth z and color c of A at (i,j); if( z > z_buffer[i][j] ) { z_buffer[i][j] = z; color_buffer[i][j] = c; } } Efficient Z-Buffer • Incremental computation • Polygon satisfies plane equation Ax By Cz D = 0 • Z can be solved as D Ax By z= C • Take advantage of coherence A C B z = y C – within scan line: z = x – next scan line: Z Value Interpolation z1 y1 ys y2 y3 za zp za = z1 ( z1 z2 ) y1 ys y1 y2 zb = z1 ( z1 z3 ) y1 ys y1 y3 zb z2 z3 xb x p z p = zb ( zb za ) xb xa Z-Buffer: Analysis • Advantages – Simple – Easy hardware implementation – Objects can be non-polygons • Disadvantages – Separate buffer for depth – No transparency – No antialiasing: one item visible per pixel Spatial Data-Structures for Visibility • Octrees (generalization of Binary trees in 1D and Quad trees in 2D) • Binary-Space Partition Trees (BSP trees) (an alternative generalization of Binary trees in 1D) • Subdividing architectural buildings into cells (rooms) and portals (doors/windows) Portals • Similar to view-frustum culling • View-independent • Preprocess and save a list of possible visible surfaces for each portal Cells and Portals E A D B F C G H A B E C D H F G Images courtesy: Dave Luebke, UVa Cells and Portals E A D B F C G H A B E C D H F G Images courtesy: Dave Luebke, UVa Cells & Portals E A D B F C G H A B E C D H F G Images courtesy: Dave Luebke, UVa Cells & Portals E A D B F C G H A B E C D H F G Images courtesy: Dave Luebke, UVa Cells & Portals E A D B F C G H A B E C D H F G Images courtesy: Dave Luebke, UVa BSP Trees • Idea Preprocess the relative depth information of the scene in a tree for later display • Observation The polygons can be painted correctly if for each polygon F: – Polygons on the other side of F from the viewer are painted before F – Polygons on the same side of F as the viewer are painted after F Building a BSP Tree Typedef struct { polygon root; BSP_tree *backChild, *frontChild; } BSP_tree; BSP_tree *makeBSP(polygon *list) { if( list = NULL) return NULL; Choose polygon F from list; Split all polygons in list according to F; BSP_tree* node = new BSP_tree; node->root = F; node->backChild = makeBSP( polygons on front side of F ); node->frontChild = makeBSP( polygons on back side of F ); return node; } Building a BSP Tree (2D) 5a 2 3 5 5b 3 1 4 1 2 5a 4 5b Building a BSP Tree (2D) 5a 2 3 5 back front 5b 3 front 1 4 5a 2 back 1 4 5b Building a BSP Tree (2D) 5a 2 3 5 back front 5b 3 front 1 4 5a 2 back 1 4 back 5b Displaying a BSP Tree void displayBSP ( BSP_tree *T ) { if ( T != NULL) { if ( viewer is in front of T->root ) { // display backChild first displayBSP ( T->backChild ); displayPolygon ( T->root ); displayBSP ( T->frontChild ); } else { // display frontChild first displayBSP ( T->frontChild ); displayPolygon ( T->root ); displayBSP ( T->backChild ); } } Displaying a BSP Tree 5a 2 3 5 back front 5b 3 front 1 4 5a 2 back 4 1 Display order: 4, 5b, 3, 5a, 2, 1 (only 3 is front facing) back 5b BSP Trees: Analysis • Advantages – Efficient – View-independent – Easy transparency and antialiasing • Disadvantages – Tree is hard to balance – Not efficient for small polygons