Notes_10_04 1 of 17 Collision Detection for Polygonal Objects given global location ri and attitude i for all bodies at time t given s i ' P for all vertices on all bodies (constants) polygons must not be self-crossing polygons must not have holes ri P 4 ri P3 yi ' xi ' ri P1 y j' x j' r P2 ri P 2 j r P1 j r P3 j Notes_10_04 2 of 17 Bounding Circle - quick check before the simulation, find maximum radius i max norm s i ' P over all vertices for each body (one constant scalar value per body that may be computed a priori) at each time t, compute center distance c ij norm ri r j between all pairs of bodies if c ij i j then no collision can occur between bodies i and j if c ij i j then bodies i and j are candidates for collision detection does not require calculation of ri for any vertices on any bodies P works best for centroidal origins and objects with low aspect ratios i yi ' xi ' c ij y j' j x j' Notes_10_04 3 of 17 Axis Aligned Bounding Box (AABB) - quick check at each time t, calculate ri for all vertices on all bodies P find AABB for each body xi MAX xi MIN yi MAX yi MIN test between all pairs of bodies x MAX i xj MIN AND x MAX j xi MIN AND y MAX i yj MIN AND y yi MAX j MIN if true, then AABBs intersect and bodies i and j are candidates for collision detection if false, then AABBs do not intersect and collision cannot occur between bodies i and j works best for objects with low aspect ratios yi yi ' xi ' yj MAX yi MIN y j' x j' MAX yj MIN Notes_10_04 4 of 17 Point in Polygon must check if any vertices on body j are inside body i AND are inside body j check if any vertices on body i computational complexity O( ni x nj ) for convex objects and O( 2 ni x nj ) for ray casting point in polygon does not always work for thin bodies (may need edge intersection) Notes_10_04 5 of 17 Point in Polygon – Convex Objects vertices must be ordered CW around polygon at each time t, calculate ri for all points on all bodies that are candidates for collision P select one point r j on body j and test if it is to the right or to the left of edge ri ri on body i P Q external normal n̂ unit R ri ri P P P Q P P to the left of current edge if rj ri n̂ 0 , the point is to the left of the edge, is outside the polygon and the search across edges may be terminated for that point P T if rj ri n̂ 0 , the point is on the edge and may be inside the polygon P T if rj ri n̂ 0 , the point is to the right of the edge and may be inside the polygon P T expand rj ri P Rr P T Q i ri y j y i P P P x Q i xi x j xi P P P y point r j must be to the right of ALL edges to be inside body i P computational complexity O( ni x nj /2 ) because may terminate search early does not work for bodies with concavities or holes Q i yi P Notes_10_04 6 of 17 Point in Polygon – Ray Casting at each time t, calculate ri for all points on all bodies that are candidates for collision P select one point r j on body j and test if it is inside body i P cast an infinite ray in any direction from r j and compute intersections with all edges on body i P an infinite ray to the right from r j intersects edge ri ri P P Q 0 d i 1 AND d j 0 y j yi P yi yi P P di Q d j xi xi di x j xi Q P P P if if there are an even number of intersections, r j is not inside body i P if there are an odd number of intersections, r j is inside body i P computational complexity O( ni x nj ) works for concavities, holes and self-crossing and is independent of CW versus CCW boundary special case when r j is on an edge or vertex of body i P algorithm used by MATLAB “inpolygon” 0 1 2 3 4 2 4 3 2 Notes_10_04 7 of 17 % t_inpolygon.m - test inpolygon % HJSIII, 11.4.13 clear % vertices for body i - bounded 0 to 1 ni = 5; ri = rand(2,ni); ri = [ ri ri(:,1) ]; % close the boundary % points for j nj = 500; rj = rand(2,nj); % which are inside? % points j vertices i in = inpolygon( rj(1,:),rj(2,:), ri(1,:),ri(2,:) ); figure( 1 ) clf plot( ri(1,:),ri(2,:),'b', rj(1,in),rj(2,in),'.r', rj(1,~in),rj(2,~in),'.g' ) % bottom of t_inpolygon 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 Notes_10_04 8 of 17 Edge Intersection must check every edge on body i for intersection with every edge on body j ri P r dj Q j di r P ri Q j ri P d i ri Q ri P rj P d j rj Q rj P intersect if 0 d i 1 and r Q i di dj ri x x x x P Q j Q j Q i Q j Q P d i P P r j r j rj ri d j xj P xj P xi P xj P y y P j Q i y y P j Q i yi P yi P yi P yi P y x y x Q j Q i Q i Q i yj P xi P yi P xi P x y x y P xi P Q yj P P xi Q yj j j j j P P 0 dj 1 if denominator = 0, segments are parallel expand y j y i P P x Q i xi P x r ri xi y P must check if they are coincident j P j P Rr P T Q i yi Q i P ri 0 P 0 computational complexity O( ni x nj ) edge intersection does not always work if one body is completely inside the other (may need point in polygon) Notes_10_04 9 of 17 Separating Axis Theorem (SAT) vertices must be ordered CW around polygon before the simulation, select one edge s i 'Q s i ' P on body i and compute external normal n̂ unit R s i 'Q s i ' P compute the projection p i for all points s i ' R onto the normal and store the minimum value R pi MIN (one scalar constant per edge that may be computed a priori) p i s i ' R s i ' P unit R s i 'Q s i ' P R T at each time t, calculate ri for all points on all bodies that are candidates for collision P select edge ri ri on body i and compute external normal n̂ unit R ri ri called the separating axis Q P Q P select point r j on body j and compute its projection p j onto the separating axis P P p j rj ri P n̂ P T P P compute the projection p j onto the separating axis for all other points on body j and store the maximum p j MAX and minimum p j MIN values check for overlap of projections onto the separating axis 0 p AND p MIN j MAX j pi MIN if false, the projections do not overlap, there can be no collision and the test may be terminated if true, the projections overlap and must continue checking all other edges ri ri Q P does not work for bodies with concavities or holes very similar to convex body point in polygon however do not need to check (i in j) and (j in i) computational complexity O( ni x nj / 2) because may terminate search early AND do not need to check body i against body j Notes_10_04 n̂ 10 of 17 ri Q separating axis ri R ri P piMIN pjMAX pjMIN r P j Notes_10_04 11 of 17 Classification of Contacts i i j edge-edge j vertex-edge i i j j multiple vertex-edge vertex-edge edge-edge multiple vertex-edge multiple edge-edge Body i vertices in j 0 1 0 1 multiple edge-edge Body i edge intersections 1 2 1 2 Body j vertices in i 1 1 ≥2 ≥2 Body j edge intersections 2 2 2 2 Contact points 3 4 5 5 Notes_10_04 12 of 17 Interpolating Time of Collision (vertex-edge) r P j ri Q ri P given candidate vertex r j t on body j that collides with body i at time t P given candidate edge ri t ri t on body i that has two edge intersections with body j at time t Q P must know position and velocity for all three points at time t-h (time step h) Constant velocity predictor predict position of all three points at local time 0 t h using vales from t-h ri P ri Pt h ri Pt h t ri Q ri Qt h ri Qt h t r r P P j t h j P r j t h t when vertex r j intersects edge ri ri P Q P r P j ri Rr P T Q i substituting position equations and collecting similar terms a t bt c 0 2 P P a rj t h ri t h P P b rj t h ri t h P c rj t h ri t h P R r ri t h R r P P P ri t h rj t h ri t h R r ri t h T T T Q i t h Q i t h Q i t h P P R r T Q i t h ri t h P ri 0 P Notes_10_04 t 13 of 17 b b 2 4ac 2a must check 0 t h and it must not be imaginary if not true, then that vertex did not actually collide with that edge r P j ri P ri Q relative motion of body j with respect to body i Constant acceleration predictor predict position of all three points at local time 0 t h measured from t-h ri P ri Pt h ri Pt h t 12 ri Pt h t 2 ri Q ri Qt h ri Qt h t 12 ri Qt h t 2 r r P P j t h j rj t h t 12 rj t h t P P 2 substituting position equations and collecting similar terms c 4 t c 3 t c 2 t c1 t c 0 0 4 3 2 c4 1 4 r ri t h R r ri t h c3 1 2 r P ri t h R r P P P ri t h 12 rj t h ri t h c2 P j t h P j t h P T T P i t h Q i t h P r r R r r r r r R r r P j t h 1 2 c1 r j t h ri t h P Q i t h T P i t h P j t h 1 2 T P i t h P Q i t h R r T Q i t h P i t h P j t h ri t h P R r T Q i t h R r T Q i t h P ri t h ri t h P P i t h P P P ri t h rj t h ri t h R r T Q i t h ri t h P (same as b above) Notes_10_04 c 0 r j t h ri t h P P R r T Q i t h ri t h P (same as c above) use root finding algorithm for t (e.g. MATLAB “roots”) must check 0 t h and it must not be imaginary 14 of 17 Notes_10_04 15 of 17 Three-dimensional Collision Detection Quick checks Bounding spheres and AABB both work well Point in polyhedron – convex bodies at each time t, calculate ri for all points on all bodies that are candidates for collision P select one point r j on body j and test if it is outside or inside a facet on body i defined by P external normal n̂ and any vertex ri on the facet P P P P if rj ri n̂ 0 , the point is outside the polyhedron and the search across facets may be terminated for that point P T if rj ri n̂ 0 , the point is on the facet and may be inside the polyhedron P T if rj ri n̂ 0 , the point is inside the facet and may be inside the polyhedron P T point r j must be inside of ALL facets to be inside body i P point in polygon does not always work for thin bodies (may need edge intersection) Point in polyhedron – ray casting at each time t, calculate ri for all points on all bodies that are candidates for collision P select one point r j on body j and test if it is inside body i P cast an infinite ray in any direction from r j and compute intersections with all facets on body i P if there are an even number of intersections, r j is not inside body i P if there are an odd number of intersections, r j is inside body i P works for concavities, holes and self-crossing and is independent of CW versus CCW boundary Notes_10_04 16 of 17 ray-facet intersection is similar to edge-facet intersection described below point in polygon does not always work for thin bodies (may need edge intersection) Facet intersection check if edges for a given facet on body j intersect facet on body i establish local coordinate frame with origin at the first vertex for facet on i, local x axis along first edge and local y defined by second edge (CCW vertex sequence around facet i) local z axis will be parallel to external normal transform vertices for facet i and facet j into local coordinates and perform all following computations in local coordinates test where edge r j r j intersects facet i Q use d j z j / z j z j P P Q P P and check 0 d P j 1 if false, that edge does not intersect the facet and proceed to check next edge on j for same facet i if true that edge intersects the plane of the facet at r r j d j P P r r Q j P j use planar point in polygon test if x,y components of r are inside x,y components of facet i if false, the edge intersects the plane of the facet outside its boundary if true, the edge intersects the facet inside its boundary must also check if edges for same facet on body i intersect same facet on body j facet intersection does not always work if one body is completely inside the other (may need point in polygon) Notes_10_04 17 of 17 Handling Collision Physical simulators differ in the way they react to a collision. Some use the softness of the material to calculate a force, which will resolve the collision in the following time steps like it is in reality. Due to the low softness of some materials this is very CPU intensive. Other simulators estimate the time of collision by interpolation, roll back the simulation, and calculate the exact time of collision. This may require several iterations. After an inelastic collision, special states of sliding are often imposed. For example, an open pin-in-slot constraint is added for a vertex-edge intersection to force that vertex to slide on the edge rather than penetrate it. The Open Dynamics Engine uses this approach.