Check if two geometries are in collision
Check Geometry Collision Status
This example shows how to check the collision status of two collision geometries.
Create a box collision geometry.
bx = collisionBox(1,2,3);
Create a cylinder collision geometry.
cy = collisionCylinder(3,1);
Translate the cylinder along the x-axis by 2.
T = trvec2tform([2 0 0]); cy.Pose = T;
Plot the two geometries.
show(cy) hold on show(bx) xlim([-5 5]) ylim([-5 5]) zlim([-5 5]) hold off
Check the collision status. Confirm the status is consistent with the plot.
[areIntersecting,dist,witnessPoints] = checkCollision(bx,cy)
areIntersecting = 1
dist = NaN
witnessPoints = 3×2 NaN NaN NaN NaN NaN NaN
Translate the box along the x-axis by 3 and down the z-axis by 4. Confirm the box and cylinder are not colliding.
T = trvec2tform([0 3 -4]); bx.Pose = T; [areIntersecting,dist,witnessPoints] = checkCollision(bx,cy)
areIntersecting = 0
dist = 2
witnessPoints = 3×2 0.4286 0.4286 2.0000 2.0000 -2.5000 -0.5000
Plot the box, cylinder, and the line segment representing the minimum distance between the two geometries.
show(cy) hold on show(bx) wp = witnessPoints; plot3([wp(1,1) wp(1,2)], [wp(2,1) wp(2,2)], [wp(3,1) wp(3,2)], 'bo-') xlim([-5 5]) ylim([-5 5]) zlim([-5 5]) hold off
collisionStatus — Collision status
Collision status, returned as
1. If the
two geometries are in collision,
collisionStatus is equal to
1. Otherwise, the value is
sepdist — Minimal distance
real number |
Minimal distance between two collision geometries, returned as a real number or
NaN. The line segment that connects the witness points
witnesspts) realizes the minimal distance between the two
geometries. When the two geometries are in collision,
witnesspts — Witness points
Witness points on each geometry, returned as a 3-by-2 matrix. Each column
corresponds to the witness point on
geom2, respectively. The line segment that connects the two
witness points has length
septdist. When the two geometries are in
witnesspts is set to
Collision checking results are no longer reliable when the minimal distance falls below 10-5 m.
 E. G. Gilbert, D. W. Johnson, and S. S. Keerthi. A fast procedure for computing the distance between complex objects in three-dimensional space. in IEEE Journal on Robotics and Automation, vol. 4, no. 2, pp. 193-203, April 1988, doi: 10.1109/56.2083.