checkCollision

Check if two geometries are in collision

Description

collisionStatus = checkCollision(geom1,geom2) returns the collision status between the two convex geometries geom1 and geom2. If the two geometries are in collision at their specified poses, checkCollision is equal to 1. If no collision is found, collisionStatus is equal to 0.

example

[collisionStatus,sepdist,witnesspts] = checkCollision(geom1,geom2) also returns the minimal distance and witness points of each geometry, sepdist and witnesspts, respectively, when no collision is found between the two geometries.

Examples

collapse all

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

Input Arguments

collapse all

Collision geometry, specified as one of the following:

Collision geometry, specified as one of the following:

Output Arguments

collapse all

Collision status, returned as 0 or 1. If the two geometries are in collision, collisionStatus is equal to 1. Otherwise, the value is 0.

Data Types: double

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, sepdist is set to NaN.

Data Types: double | NaN

Witness points on each geometry, returned as a 3-by-2 matrix. Each column corresponds to the witness point on geom1 and geom2, respectively. The line segment that connects the two witness points has length septdist. When the two geometries are in collision, witnesspts is set to nan(3,2).

Data Types: double | NaN

Limitations

  • Collision checking results are no longer reliable when the minimal distance falls below 10-5 m.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Introduced in R2019b