220 likes | 410 Views
Week 13 - Wednesday. CS361. Last time. What did we talk about last time? Intersection testing Bounding volumes Sphere AABB OBB k-DOP XNA tools. Questions?. Project 4. XNA Picker. Getting Bounding Spheres from XNA Models. Intersection Methods. Ray/sphere intersection.
E N D
Week 13 - Wednesday CS361
Last time • What did we talk about last time? • Intersection testing • Bounding volumes • Sphere • AABB • OBB • k-DOP • XNA tools
Ray/sphere intersection • We can write the implicit sphere equation as f(p) = ||p – c|| – r = 0 • p is any point on the surface • c is the center • r is the radius • By substituting in r(t) for p, we can eventually get the equation t2 + 2tb + c = 0, where b = d • (o – c) and c = (o – c) •(o – c) – r2 • If the discriminant is negative, the ray does not hit the sphere, otherwise, we can compute the location(s) where it does
Optimized ray/sphere • Looking at it geometrically, we can optimize the test • Find the vector from the ray origin to the center of the sphere l = c – 0 • Find the squared length l2 = l • l • If l2 < r2, then o is in the sphere, intersect! • If not, project l onto d: s = l • d • If s < 0, then the ray points away from the sphere, reject • Otherwise, use the Pythagorean theorem to find the squared distance from the sphere center to the projection: m2 = l2 – s2 • If m2 > r2, the ray will miss, otherwise it hits
Ray/box intersection • Ray box intersection is a key element to have in your arsenal • Bounding boxes are a very common form of bounding volume • A ray/box intersection is often the first test you will use before going down deeper • There are a couple of methods • Slabs method • Line segment/box overlap test
Slabs method • First find the t value where the ray intersects each plane • The box is made up of 3 slabs • Find the min t and max t for each slab • The final tmin is the max of all the tmin values • The final tmax is the min of all the tmax values • If tmin ≤ tmax, the ray intersects the box, otherwise it does not • The idea can be extended to frustums and k-DOPs
Separating axis test • For two arbitary, convex, disjoint polyhedra A and B, there exists a separating axis where the projections of the polyhedra are also disjoint • Furthermore, there is an axis that is orthogonal to (making the separating plane parallel to) • A face of A or • A face of B or • An edge from each polyhedron (take the cross product) • This definition of polyhedra is general enough to include triangles and line segments
Line segment/box overlap test • This method uses the separating axis test and only works for AABBs and line segments • The AABB has its center at (0,0,0) and size half vector h • The line segment is defined by center c and half vector w • If |ci| > wi + hi for any ix,y,zthen disjoint • There is another test for each axis that is the cross product of the x, y, and z axis and w • If any test passes, then disjoint • Only if all tests fail, then overlap
Triangle representation • One way to represent a triangle is with barycentric coordinates • For triangles, barycentric coordinates are weights that describe where in the triangle you are, relative to the three vertices • These weights are commonly labeled u, v, and w and have the following properties • u ≥ 0, v ≥ 0, w ≥ 0 and u + v + w ≤ 1
Ray triangle intersection • We represent a point f(u,v) on a triangle with the following explicit formula • f(u,v) = (1 – u – v)p0 + up1 + vp2 • Then, setting the ray equal to this equation gives • o + td = (1 – u – v)p0 + up1 + vp2 • This is simply a vector representation of three equations with three unknowns • If the solution has a positive t, and u and v between 0 and 1, it's an intersection
Ray/polygon intersection • First we compute the intersection of the ray and the plane of the polygon • Then we determine if that point is inside the polygon (in 2D) • The plane of the polygon is np • x + dp = 0 • np is the plane normal • dp is the distance along the normal from the origin to the plane • Intersection is found by: • np • (o + td) + dp = 0 • Then project onto xy, xz, or yz plane (whichever maximizes polygon area) and see if the point is in the polygon
Crossings test • If you want to see if a point is inside a polygon, you shoot a ray from that point along the positive x axis • If it intersects edges of the p0lygon an even number of times, it is outside, otherwise it is inside • Problems can happen if a ray intersects a vertex, so we treat all vertices with y ≥ 0 as being strictly above the x axis
Plane/box intersection • Simplest idea: Plug all the vertices of the box into the plane equation n • x + d = 0 • If you get positive and negative values, then the box is above and below the plane, intersection! • There are more efficient ways that can be done by projecting the box onto the plane
Next time… • Finish intersection test methods
Reminders • Keep working on Project 4 • Keep reading Chapter 16