collisions and intersections when objects move, test for collision. when projecting surfaces, check...

41
Collisions and Intersections • When objects move, test for collision. • When projecting surfaces, check for intersections. (Many slides adapted from Amitabh Varshney)

Upload: sullivan-vass

Post on 14-Dec-2015

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

Collisions and Intersections

• When objects move, test for collision.

• When projecting surfaces, check for intersections.

(Many slides adapted from Amitabh Varshney)

Page 2: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

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.

Page 3: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

2D Intersections

• Sides intersect

• Or one inside other

Page 4: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

Sides Intersect

• Intersect lines and check whether intersection point is inside each line segment.

• Check if each line divides other line segment in half.

Page 5: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

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.

Page 6: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

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.

Page 7: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

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).

Page 8: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

Circles easier

• Testing whether two circles intersect is much easier.

• So put a circle around each shape (How?)

(x1,y1)r1

r2

(x2,y2)

(x2-x1)2 + (y2-y1)2 > (r1+r2)2

Page 9: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

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.

Page 10: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

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.

Page 11: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

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.

Page 12: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh
Page 13: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

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

Page 14: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

Painter’s Algorithm

• Object-Order Algorithm

• Sort objects by depth

• Display them in back-to-front order

Page 15: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

Painter’s Algorithm

First

Second

Third

Fourth

Page 16: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

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.

Page 17: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

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.

Page 18: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

z

B

A

z

B

A

2. The plane of the polygon with smaller z is closer to viewer than other polygon.

(a,b,c,)*(x,y,z) >= d.

x

x

3. The plane of polygon with larger z is completely behind other polygon.

Page 19: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

y

x

y

xNon-Overlapping x or y Overlapping projection

B is on one side of A

x

z

B

A

4. Check whether they overlap in image

a. Use axial rectangle test.

b. Use complete test.

Page 20: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

Problem Cases: Cyclic and Intersecting Objects

Page 21: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

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

Page 22: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

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

Page 23: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

Z-Buffer Algorithmfor( 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; }

}

Page 24: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

Efficient Z-Buffer

• Incremental computation

• Polygon satisfies plane equation

• Z can be solved as

• Take advantage of coherence– within scan line:

– next scan line:

0 DCzByAx

CByAxD

z

xCA

z

yCB

z

Page 25: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

Z Value Interpolation

21

1211 )(

yy

yyzzzz s

a

31

1311 )(

yy

yyzzzz s

b

ab

pbabbp xx

xxzzzz

)(

y1

ys

y2

y3

z1

z3

z2

zbzazp

Page 26: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

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

Page 27: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

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)

Page 28: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

Portals• Similar to view-frustum culling

• View-independent

• Preprocess and save a list of possible visible surfaces for each portal

Page 29: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

Cells and Portals

A

D

H

FCB

E

G

H

B C D F G

EA

Images courtesy: Dave Luebke, UVa

Page 30: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

Cells and Portals

A

D

H

FCB

E

G

H

B C D F G

EA

Images courtesy: Dave Luebke, UVa

Page 31: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

Cells & Portals

A

D

H

FCB

E

G

H

B C D F G

EA

Images courtesy: Dave Luebke, UVa

Page 32: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

Cells & Portals

A

D

H

FCB

E

G

H

B C D F G

EA

Images courtesy: Dave Luebke, UVa

Page 33: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

Cells & Portals

A

D

H

FCB

E

G

H

B C D F G

EA

Images courtesy: Dave Luebke, UVa

Page 34: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

BSP Trees

• Idea

Preprocess the relative depth information of the scene in a tree for later display

• ObservationThe 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

Page 35: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

Building a BSP TreeTypedef 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;

}

Page 36: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

Building a BSP Tree (2D)

2

13

5a5

5b

4

3

1

2

5a

4

5b

Page 37: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

Building a BSP Tree (2D)

2

13

5a5

5b

4

3

4

5b15a

2

front back

front back

Page 38: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

Building a BSP Tree (2D)

2

13

5a5

5b

4

3

15a

2

front back

front back4

5b

back

Page 39: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

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 );

}}

Page 40: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

Displaying a BSP Tree

Display order: 4, 5b, 3, 5a, 2, 1 (only 3 is front facing)

2

13

5a5

5b

4

3

15a

2

front back

front back4

5b

back

Page 41: Collisions and Intersections When objects move, test for collision. When projecting surfaces, check for intersections. (Many slides adapted from Amitabh

BSP Trees: Analysis

• Advantages– Efficient

– View-independent

– Easy transparency and antialiasing

• Disadvantages– Tree is hard to balance

– Not efficient for small polygons