public static void initialize( ref Manifold manifold, ref Transform xfA, float radiusA, ref Transform xfB, float radiusB, out Vector2 normal, out FixedArray2<Vector2> points )
{
normal = Vector2.Zero;
points = new FixedArray2<Vector2>();
if( manifold.pointCount == 0 )
return;
switch( manifold.type )
{
case ManifoldType.Circles:
{
normal = new Vector2( 1.0f, 0.0f );
var pointA = MathUtils.mul( ref xfA, manifold.localPoint );
var pointB = MathUtils.mul( ref xfB, manifold.points[0].localPoint );
if( Vector2.DistanceSquared( pointA, pointB ) > Settings.epsilon * Settings.epsilon )
{
normal = pointB - pointA;
normal.Normalize();
}
var cA = pointA + radiusA * normal;
var cB = pointB - radiusB * normal;
points[0] = 0.5f * ( cA + cB );
break;
}
case ManifoldType.FaceA:
{
normal = MathUtils.mul( xfA.q, manifold.localNormal );
var planePoint = MathUtils.mul( ref xfA, manifold.localPoint );
for( int i = 0; i < manifold.pointCount; ++i )
{
var clipPoint = MathUtils.mul( ref xfB, manifold.points[i].localPoint );
var cA = clipPoint + ( radiusA - Vector2.Dot( clipPoint - planePoint, normal ) ) * normal;
var cB = clipPoint - radiusB * normal;
points[i] = 0.5f * ( cA + cB );
}
break;
}
case ManifoldType.FaceB:
{
normal = MathUtils.mul( xfB.q, manifold.localNormal );
var planePoint = MathUtils.mul( ref xfB, manifold.localPoint );
for( int i = 0; i < manifold.pointCount; ++i )
{
var clipPoint = MathUtils.mul( ref xfA, manifold.points[i].localPoint );
var cB = clipPoint + ( radiusB - Vector2.Dot( clipPoint - planePoint, normal ) ) * normal;
var cA = clipPoint - radiusA * normal;
points[i] = 0.5f * ( cA + cB );
}
// Ensure normal points from A to B.
normal = -normal;
break;
}
}
}
}