Detecting Collision
This example provides information on using CollisionDetection between objects. Two boxes are created to be our collision objects. Then we create a collision set, defining what objects to check for collision, the collision color, near miss distance, and so on. Run the Add-In and freehand move the boxes. Notice how they change color and the message in the logger when they are colliding.
Use this procedure to detect collision between objects:
Create two parts and bodies to use in collision detection.
Check to see if there is a collision between the two parts.
Add the parts to a collision set.
Check for collisions.
Solution
Create two parts and bodies to use in collision detection.
// Create two parts and bodies to use in collision detection. Part p1 = new Part(); p1.Name = "My Part1"; Part p2 = new Part(); p2.Name = "My Part2"; station.GraphicComponents.Add(p1); station.GraphicComponents.Add(p2);
Check to see if there is a collision between the two parts.
CollisionType colType = CollisionDetector.CheckCollision(p1, p2, 0.01); switch (colType) { case CollisionType.Collision: Logger.AddMessage(new LogMessage("Part: " + p1.Name + " and part: " + p2.Name + " is colliding!")); break; case CollisionType.NearMiss: Logger.AddMessage(new LogMessage("There is a near miss between part: " + p1.Name + " and part: " + p2.Name + ".")); Vector3 p1Point; Vector3 p2Point; Logger.AddMessage(new LogMessage("The distance between them are: " + CollisionDetector.MinimumDistance(p1, p2, out p1Point, out p2Point))); Logger.AddMessage(new LogMessage("The closest points are: ")); Logger.AddMessage(new LogMessage("For part: " + p1.Name + " x: " + p1Point.x + " y: " + p1Point.y + " z: " + p1Point.z)); Logger.AddMessage(new LogMessage("For part: " + p2.Name + " x: " + p2Point.x + " y: " + p2Point.y + " z: " + p2Point.z)); break; case CollisionType.None: Logger.AddMessage(new LogMessage("There is no collision!")); break; }
Add the parts to a collision set.
// Add the two boxes to a collision set. CollisionSet cs = new CollisionSet(); cs.Name = "My CollisionSet"; // Set the near miss distance to 0.01 meters. cs.NearMissDistance = 0.01; // Set the near miss color to black. cs.NearMissColor = System.Drawing.Color.Black; // Activate this collision set for collision checks. cs.Active = true; // Set the collision color to semi-transparent yellow. cs.CollisionColor = Color.FromArgb(128, System.Drawing.Color.Yellow); // Turn on highlighting of colliding objects. cs.Highlight = true; // Add the collision set to the station. station.CollisionSets.Add(cs); // Add the two parts to the two different collision object collections of the collision set. cs.FirstGroup.Add(p1); cs.SecondGroup.Add(p2);
Check for collisions.
// Force check the station for collisions. CollisionDetector.CheckCollisions(station); // Force check the collision set for collisiosn. CollisionDetector.CheckCollisions(cs);
Example
This example provides information to detect collision between objects.
public void CollisionDetection()
{
Project.UndoContext.BeginUndoStep("CollisionDetection");
try
{
Station station = Station.ActiveStation;
// Create two parts and bodies to use in collision detection.
Part p1 = new Part();
p1.Name = "My Part1";
Part p2 = new Part();
p2.Name = "My Part2";
station.GraphicComponents.Add(p1);
station.GraphicComponents.Add(p2);
// Create two boxes.
Body box1 = Body.CreateSolidBox(new Matrix4(Vector3.XVector, 0.0),
new Vector3(0.1, 0.1, 0.1));
box1.Name = "My Box1";
Body box2 = Body.CreateSolidBox(new Matrix4(Vector3.XVector, 0.0),
new Vector3(0.1, 0.1, 0.1));
box2.Name = "My Box2";
// Move box2 0.105 meters along the X-axis .
box2.Transform.X = box2.Transform.X + 0.105;
// Add the boxes to the bodies.
p1.Bodies.Add(box1);
p2.Bodies.Add(box2);
// Manually check to see if there is a collision between the two parts.
// Also tests for a near miss, defined as being within 0.01 meter from each other.
CollisionType colType = CollisionDetector.CheckCollision(p1, p2, 0.01);
switch (colType)
{
case CollisionType.Collision:
Logger.AddMessage(new LogMessage("Part: " + p1.Name
+ " and part: " + p2.Name + " is colliding!"));
break;
case CollisionType.NearMiss:
Logger.AddMessage(new LogMessage("There is a near miss between part: "
+ p1.Name
+ " and part: " + p2.Name + "."));
Vector3 p1Point;
Vector3 p2Point;
Logger.AddMessage(new LogMessage("The distance between them are: "
+ CollisionDetector.MinimumDistance(p1, p2, out p1Point, out p2Point)));
Logger.AddMessage(new LogMessage("The closest points are: "));
Logger.AddMessage(new LogMessage("For part: " + p1.Name + " x: " + p1Point.x
+ " y: " + p1Point.y + " z: " + p1Point.z));
Logger.AddMessage(new LogMessage("For part: " + p2.Name + " x: " + p2Point.x
+ " y: " + p2Point.y + " z: " + p2Point.z));
break;
case CollisionType.None:
Logger.AddMessage(new LogMessage("There is no collision!"));
break;
}
// Reset the collision sets and highlights.
CollisionDetector.ResetCollisions();
// Add an event handler to the collision event.
CollisionDetector.Collision += new CollisionEventHandler(myCollisionEventHandler);
// Activate auto check for collisions each time the graphics is updated.
CollisionDetector.AutoCheck = true;
// Test for collisions on polygon level, not just for the bounding boxes.
CollisionDetector.FastCheck = false;
// Add the two boxes to a collision set.
CollisionSet cs = new CollisionSet();
cs.Name = "My CollisionSet";
// Set the near miss distance to 0.01 meters.
cs.NearMissDistance = 0.01;
// Set the near miss color to black.
cs.NearMissColor = System.Drawing.Color.Black;
// Activate this collision set for collision checks.
cs.Active = true;
// Set the collision color to semi-transparent yellow.
cs.CollisionColor = Color.FromArgb(128, System.Drawing.Color.Yellow);
// Turn on highlighting of colliding objects.
cs.Highlight = true;
// Add the collision set to the station.
station.CollisionSets.Add(cs);
// Add the two parts to the two different collision object collections of the collision set.
cs.FirstGroup.Add(p1);
cs.SecondGroup.Add(p2);
// Force check the station for collisions.
CollisionDetector.CheckCollisions(station);
// Force check the collision set for collisiosn.
CollisionDetector.CheckCollisions(cs);
// Freehand move the boxes in the graphics.
// Notice the messages generated by the event handler code,
// and the change in color of the boxes.
}
catch
{
Project.UndoContext.CancelUndoStep(CancelUndoStepType.Rollback);
throw;
}
finally
{
Project.UndoContext.EndUndoStep();
}
}
private void myCollisionEventHandler(object sender, CollisionEventArgs e)
{
switch (e.CollisionEvent)
{
case CollisionEvent.CollisionStarted:
Logger.AddMessage
(new LogMessage("Collision started by collision set: '" + e.CollisionSet.Name
+ "' First part : '" + e.FirstPart.Name
+ "' Second part: '" + e.SecondPart.Name + "'"));
break;
case CollisionEvent.CollisionEnded:
Logger.AddMessage(new LogMessage("Collision ended by collision set: '" + e.CollisionSet.Name
+ "' First part : '" + e.FirstPart.Name
+ "' Second part: '" + e.SecondPart.Name + "'"));
break;
case CollisionEvent.NearMissStarted:
Logger.AddMessage(new LogMessage("Near Miss started by collision set: '" + e.CollisionSet.Name
+ "' First part : '" + e.FirstPart.Name
+ "' Second part: '" + e.SecondPart.Name + "'"));
break;
case CollisionEvent.NearMissEnded:
Logger.AddMessage(new LogMessage("Near Miss ended by collision set: '" + e.CollisionSet.Name
+ "' First part : '"
+ e.FirstPart.Name + "' Second part: '" + e.SecondPart.Name + "'"));
break;
default:
break;
}
}
Required Namespaces
ABB.
ABB.