Click or drag to resize
DigitalRuneHow To: Compute the Minimal Distance of Objects

This topic will demonstrate how to compute the minimal distance between a pair of objects.

This topic contains the following sections:

Add DigitalRune namespaces

The following example will use data types from DigitalRune Geometry and Mathematics. The following namespaces must be imported at the beginning of the source code file:

C#
using DigitalRune.Geometry;
using DigitalRune.Geometry.Collisions;
using DigitalRune.Geometry.Shapes;
using DigitalRune.Mathematics.Algebra;
Create collision objects

First, we create two collision objects. One object gets the shape of a box. The other object gets a sphere shape. Both objects are positioned and rotated.

C#
GeometricObject geometryA = new GeometricObject()
{
  Shape = new BoxShape(1, 2, 3),
  Pose = new Pose(new Vector3F(-2, 0, 0), QuaternionF.CreateRotationZ(0.1f)),
};
GeometricObject geometryB = new GeometricObject()
{
  Shape = new SphereShape(1),
  Pose = new Pose(new Vector3F(2, 0, 0), QuaternionF.CreateRotationZ(-0.2f)),
};

Now, a collision object is created for each geometric object. A collision object stores additional information required by the collision detection.

C#
CollisionObject objectA = new CollisionObject(geometryA);
CollisionObject objectB = new CollisionObject(geometryB);
Compute minimal distance

Let's create a collision detection which handles the computation of closest-point queries and contact queries. Normally, this is done only once for the entire application.

C#
CollisionDetection collisionDetection = new CollisionDetection();

The collision detection computes a closest-point query with:

C#
ContactSet contactSet = collisionDetection.GetClosestPoints(objectA, objectB);

The resulting contact set is a collection of contacts. For a closest-point query the contact set contains exactly one contact that describes the closest-point pair. The contact contains a property for the penetration depth, which is the inverse of the separation distance. The separation distance is positive for separated objects and negative for penetrating objects. The contact also contains the positions of the closest points on the objects. Finally, we can retrieve the following information for further use:

C#
Contact contact = contactSet[0];
float distance = -contact.PenetrationDepth;  // The distance between the objects.
Vector3F pointA = contact.PositionAWorld;    // The point on object A that is closest to object B (in world space).
Vector3F pointB = contact.PositionBWorld;    // The point on object B that is closest to object A (in world space).