3.4.CONTACT GENERATION Generating contacts between rigid bodies Generating contacts between rigid bodies Introduction to contact generation A collision detection system may simply return the single point of maximum interpenetration if two objects are found to be in contact. Unfortunately, this does not provide adequate information to implement a realistic physics simulation, i.e. a collision between two objects may involve multiple points of contact (e.g. a car object resting on a surface). As such, the process of contact generation should produce a set of points of contact for each interpenetrating object pairing. Introduction to contact generation One box resting on top of another box will have a region (or patch) in direct contact. The contact patch can be of any shape (depending on the geometry of the touching objects). In order to simplify the process, a contact patch is typically simplified to a representative contact point or set of contact points. ● Single contact Collision detection Contact pair ● ● Contact generation As such, a means of modelling a contact patch using a set of contact points is needed. The next slide shows one means of accomplishing this mapping (producing reasonable physical behaviour in most situations). Common types of contact Point-face contact Face-face contacts Edge-edge contact ● ● ● In most cases, the following collision detection algorithms will try to model a contact using either point-face or edge-edge contacts (primitives with curved surfaces will use some of the other cases). Face-face collisions are needed when one or both contact faces are curved, otherwise, flat face-face contacts can be modelling using edge–edge and edge–face contacts. Common types of contact Edge-face contact Point-edge contact ● Edge–face contacts can often be replaced by a pair of edge– point contacts (except when the face is curved). Point-point contact ● Point–point contacts and point–edge contacts can be effectively ignored as they are unlikely to occur and can be handled as a point-face contact. Contact data Assuming the types of contact identified above, the following properties can be defined permitting the contact to be described: • Colllision point – the representative point of contact (given interpenetrate any number of points might be selected – some arbitrary means of selecting a point is adopted, e.g. mid penetration point) • Collision normal - direction in which an impact impulse will be applied between the two objects. By convention, the contact normal points from the first object involved toward the second. • Penetration depth - amount that the two objects are interpenetrating measured along the direction of the collision normal passing through the collision point Contact normal Contact ● point Penetration depth Contact data: Point-Face Contacts Point–face contacts are the most common and important type of contact. 1. The contact normal is given by the normal of the surface at the point of contact. 2.The contact point is given as the point involved in the contact (if interpenetrating, the midway point between the object’s point and projected face point could be used) 3. The penetration depth is calculated as the distance between the object point and the projected point. Contact normal ● Contact point Penetration depth Contact data: Edge-Edge Contacts Edge–edge contacts are the second most important type of contact and are used to model resting contacts between objects with flat or concave sides 1. The contact normal is at right angles to the tangents of both edges (built using a cross product) 2.The contact point is typically the closest point on one edge to the other (a point midway between both edges could also be used) 3. The penetration depth is the distance between the two edges. Contact normal Contact point ● Contact data: Edge-Face Contacts Edge–face contacts are only used with curved surfaces (although the contact data is generated in a very similar way to point–face contacts) 1. The contact normal is given by the normal of the face (the edge direction is ignored) 2.The contact point is calculated as the point of deepest penetration geometrically. 3. The penetration depth is the distance between the edge and the face along the direction of the normal passing through the contact point. Contact normal Contact point ● Penetration depth Contact data: Face-Face Contacts Face–face contacts occur when a curved surface comes in contact with another face (either curved or flat). In this case, the contact data is more arbitrarily selected. 1. The contact normal is given by the normal of the first face. 2.The contact point is calculated as the point of deepest penetration geometrically. If a point of deepest penetration cannot be readily obtained (e.g. two interpenetrating flat surfaces), an arbitrary point is selected. 3. The penetration depth is the maximum distance between the two faces involved within the contact. Contact normal Contact point ● Coherence and Contact Generation The most efficient algorithms for calculating collision information between a pair of convex objects will typically terminate whenever the point of deepest interpenetration is found, i.e. a single contact is provided. Generating a complete set of contacts is considerably more difficult as a contact can be interpreted in different ways (e.g. a point-face contact could be interpreted as another point–face contact for a different face, with a different penetration depth). To generate a complete set of contacts, a means of interpretating and integrating points of contact is needed. Coherence and Contact Generation The problem of contact set generation can be (somewhat) avoided by introducing a degree of frame coherence and then simply generating a single contact (of maximum penetration), e.g.: consider two stacked boxes Frame 1: A single contact is generated and resolved in the normal manner. Frame 2: As only one point of contact was resolved, the boxes will likely reinterpenetrate but probably in a different way. This is detected as a new contact, providing two contacts which are resolved. Frame 3: A third point of contact is generated. We have three contacts which is sufficient to keep the boxes stacked in a stable manner. ● Frame 1: First contact ● Frame 2: Second contact ● Coherence and Contact Generation To take advantage of the coherence, we store the type of contact (e.g. point–edge or edge–edge) and which feature was involved for each object. If in the next frame, the same type of contact is generated (but with updated contact properties) that the stored contact can be updated. If a new type of contact has been detected, then it is added to the set of cached contacts. Contacts are removed from the set of cached contacts if the interpenetration depth is less than some fixed value (i.e. the contact has moved sufficient apart). Generating contacts between rigid bodies Primitive Contact Generation Algorithms A contact generation algorithm generates contact information between pairs of potentially colliding primitives, returning zero, one or more contacts. Iterated over a set primitive pairings (e.g. as returned from some form of collision broad pass algorithm) a pool of contacts will be generated which can then be passed to the physics engine to be resolved. See the recommended course text book for details of contact generation given • sphere-sphere, and • sphere-plane primitives. Box-Plane Contact Generation This algorithm can return more than one contact. Only point-face contacts will be returned, i.e. rather than return a faceface contact, up to four point-face contacts will be returned (one for each corner point in contact with the plane/half-space). Likewise, an edge-face contact will be returned using two point-face contacts. ● ● ● ● ● ● Box-Plane Contact Generation Contacts are generated by checking for interpenetration of each box vertex against the plane, i.e. the vertex is in contact if (where p is the vertex location, n the plane normal and d the plane distance) foreach( Point p : box.Vertices ) { float d = dot( p, plane.n ); Determine the vertex-plane distance if( d < plane.d + ε ) { An epsilon value might be introduced to cover resting contacts contact.Point = Generate contact p + plane.n * (d - plane.d); information, assuming the contact point is contact.Normal = plane.n; contact.Penetration = plane.d – d; midway between the } } vertex and plane. Sphere-Box Contact Generation A sphere-box collision will result in a single contact, although the contact type can differ (face-face, edge-face, or point-face). All three types of contact can be tested/constructed using the same approach (assuming that the collision normal is from the perspective of the sphere), as follows: 1. Find the closest point on the box (OBB) to the centre of the sphere (as outlined within an earlier lecture). The closest point may be in a box corner, edge or face. 2. If the closest box point to sphere centre distance is less than the sphere radius then generate contact information. ● ● ● Box-Box Contact Generation Generating box-to-box contacts is notably more complex than the previous generation algorithms; however, the technique employed behind box-to-box contact generation is similar to that for any pair of concave shapes. Type of Box-Box Contacts There are six possible types of contact between two boxes Pointedge contact Edgeedge contact Point-face contact ● ● ● Face-face contact Edgeface contact Pointpoint contact ● Type of Box-Box Contacts: Simplification Face-face and edge-face contacts can be represented using a number of point-face or edge-edge contacts. The point–point and point–edge contacts do not have any obvious means of determining a contact normal, additionally these forms of contact are very unlikely to occur. As such, they can be safely ignored (and will result in a point-face or edge-face contact). Edgeface contact Face-face contact ●● ●● ●● Box-Box Contact Generation In order to generate contacts between two boxes the following steps are used: 1. Use a separating axis test along each possible separating axis as a means of providing an early out for non-collision or to determine the axis of greatest interpenetration. 2. Generate a contact for the point of greatest interpenetration. 3. Use the notion of contact coherence, to combine the contact with previously detected contacts to update/extend the set of box-box contacts. Box-Box Contacts: Separating axes The separating axis test can be performed for two boxes by projecting the half-size of the box onto the separating axis, i.e.: ● ● ● bool FindPenetrationOnAxis ( Box one, Box two, Vector3 axis ) { float oneProj = TransformToAxis(one, axis); Project the half-size of float twoProj = TransformToAxis(two, axis); each box onto the axis Determine the Vector3 centre = two.Centre - one.Centre; float distance = Math.Abs( Dot( center, axis) ); midpoint between Return true if the projected half-widths overlap return (distance < oneProj + twoProj); } the boxes and project this onto the axis Return the accumulated projection of each box axis onto the separating axis float TransformToAxis(Box return box.HalfSizeX * + box.HalfSizeY * + box.HalfSizeZ * } box, Vector3 axis) { Math.Abs( Dot( axis, box.AxisX ) ) Math.Abs( Dot( axis, box.AxisY ) ) Math.Abs( Dot( axis, box.AxisZ ) ); Box-Box Contacts: Separating axes test A total of 15 different axes must be tested (to confirm interpenetration), these include: • The three principal axes of each box • The nine axes which are perpendicular to each pair of principal axes from each box Box-Box Contacts: Building a contact Once the point of deepest interpenetration has been found it is necessary to interpret this point in terms of a particular type of contact. This is a complex problem to solve for general polyhedral. A workable approach for box-to-box contacts is to simply (and exhaustively) check for each possible type of point-face and edge-edge contact. If the point of deepest interpenetration is: • Along one of the principles axis of a box, then it is a point-face contact, • Along a perpendicular axis, then it is an edge-edge contact As only a single contact is returned, if more than one contact is found within the search, then the contact that has the greatest interpenetration is returned. Box-Box Contacts: Point-Face A point-face contact uses the same approach as for a sphere-box collision (but assuming a zero-radius sphere). Each vertex from one box is projected onto the principal axes of the other. If the vertex is inside the box, then a point– face collision is required. If the vertex is inside the box on more than one axis, then the axis with the shallowest penetration is used. Box-Box Contacts: Point-Face Ensure the point is Vector3 relPoint = box.TransformToLocal(point); expressed in the Vector3 normal; box’s local space Determine the axis of least (positive) penetration float minDepth = box.HalfSizeX – Math.Abs(relPoint.X); if (min_depth < 0) return; The collision normal will normal = box.AxisX * ((relPoint.x < 0)?-1:1); be constructed to be stored within the contact float depth = box.HalfSizeY – Math.Abs(relPoint.Y); if (depth < 0) return; else if (depth < min_depth) { min_depth = depth; normal = box.AxisY * ((relPoint.Y < 0)?-1:1); } depth = box.HalfSizeZ – Math.Abs(relPoint.Z); if (depth < 0) return 0; else if (depth < min_depth) { min_depth = depth; normal = box. AxisZ * ((relPoint.Z < 0)?-1:1); } Build the contact using the determined point, normal and interpenetration depth Box-Box Contacts: Edge-Edge Each edge from one object is checked against the edges of the other object. This is accomplished by: 1. Consider each edge of object A. 2. Work out its interpenetration with each edge of object B. 3. The shallowest interpenetration of an edge from A is the interpenetration of the edge. 4. The edge from object A with the deepest interpenetration is retained. Directed physics reading Directed reading • Read Chapter 13 of Game Physics Engine Development (pp263-297) on rotations. • Read Chapter 9 of Real Time Collision Detection (pp383-412) on convexity based methods of intersection. Summary Today we explored: How to model different forms of contact Means of generating contacts between rigid body primitives