net.phys2d.raw.collide
Class PenetrationSweep

java.lang.Object
  extended bynet.phys2d.raw.collide.PenetrationSweep

public class PenetrationSweep
extends java.lang.Object

This class will, given an intersection pair (an ingoing and outgoing intersection), calculate the penetration depth. This is the minimum distance that A and B need to be separated to get rid of any overlap.

The penetration depth or separation is calculated by running a sweepline between the two points of an intersection pair. We keep track of the upper bound defined by edges of polygon A and the lower bound defined by B. The maximum distance between these bounds is the value we are searching for.

        -<----
       |B     |
       |      | 
    out|      |in
  -----+--.---+-------
 |A    |  !  /        |
 |      \ ! /         |
 |       \!/          |
 |        .           |
  ->------------------
  

The sweepline always runs from the ingoing to the outgoing intersection. Usually the normal is perpendicular to the sweepline.

We cannot always use the whole intersection area. Take a look at the following example. If we would allow the vertex marked with to be included in the sweep, the penetration depth would be far too big. Therefore we 'cut off' the intersection area with two lines through the intersection points perpendicular to the sweep direction. Unfortunately this will break the algorithm for collision normals other than those perpendicular to the sweepline (it's should still be usable). The lines are called the borders of the intersection area.

   +--/---/---------------------*                                                             
 +-|-/-+ /                    A |                                            
  \|/ B|/                       |                                          
   x   /                        |                                            
  /|\ /|                        |                                            
   +-x--------------------------+                                         
    / \|  
 

Convexity

This algorithm should always work well for convex intersection areas. When the intersection area is not convex, the resulting separation might be erroneous.

Since colliding two convex polygons will always result in convex intersection areas, they should be used if possible. Colliding non-convex polygons seems to work pretty well in practice too, but more testing is needed.


Nested Class Summary
 class PenetrationSweep.ContourWalker
          The contour walker walks over the edges or vertices of a polygon.
 
Constructor Summary
PenetrationSweep(Vector2f normal, Vector2f sweepDir, Vector2f intersectionStart, Vector2f intersectionEnd)
          Constructs a Penetration Sweep object, with all its attributes set.
 
Method Summary
static float getPenetrationDepth(Intersection in, Intersection out, Vector2f normal, Vector2f[] vertsA, Vector2f[] vertsB)
          Given two intersecting polygons, the intersection points and a collision normal, get the maximum penetration distance along the normal.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PenetrationSweep

public PenetrationSweep(Vector2f normal,
                        Vector2f sweepDir,
                        Vector2f intersectionStart,
                        Vector2f intersectionEnd)
Constructs a Penetration Sweep object, with all its attributes set. This constructor is public only for testing purposes. The static method getPenetrationDepth(Intersection, Intersection, Vector2f, Vector2f[], Vector2f[]) should be called to get the penetration depth.

Parameters:
normal - The collision normal
sweepDir - The sweep direction
intersectionStart - The start bound of the intersection area
intersectionEnd - The end bound of the intersection area.
Method Detail

getPenetrationDepth

public static float getPenetrationDepth(Intersection in,
                                        Intersection out,
                                        Vector2f normal,
                                        Vector2f[] vertsA,
                                        Vector2f[] vertsB)
Given two intersecting polygons, the intersection points and a collision normal, get the maximum penetration distance along the normal.

Parameters:
in - The ingoing intersection
out - The outgoing intersection
normal - The collision normal
vertsA - The vertices of polygon A
vertsB - The vertices of polygon B
Returns:
the maximum penetration depth along the given normal