%% Create and Visualize Mesh Collision Geometry
% Create an array consisting of the coordinates of ten points randomly chosen on the unit sphere. For reproducibility, set the random seed to the default value.

rng default
n = 10;
pts = zeros(n,3);
for k = 1:n
    ph = 2*pi*rand(1);
    th = pi*rand(1);
    pts(k,:) = [cos(th)*sin(ph) sin(th)*sin(ph) cos(ph)];
end

%
% Create a convex mesh collision geometry from the array. Visualize the collision geometry.

m = collisionMesh(pts);
show(m)

%
% Create a second array similar to the first, but this time consisting of 1000 points randomly chosen on the unit sphere.

n = 1000;
pts2 = zeros(n,3);
for k = 1:n
    ph = 2*pi*rand(1);
    th = pi*rand(1);
    pts2(k,:) = [cos(th)*sin(ph) sin(th)*sin(ph) cos(ph)];
end

%
% Create and visualize a mesh collision geometry from the array. Observe that choosing more points on the sphere results in a sphere-like mesh.

m2 = collisionMesh(pts2);
show(m2)

%
% Create an array consisting of the coordinates of the eight corners of a cube. The cube is centered at the origin and has side length 4.

cubeCorners = [-2 -2 -2 ; -2 2 -2 ; 2 -2 -2 ; 2 2 -2 ;...
    -2 -2 2 ; -2 2 2 ; 2 -2 2 ; 2 2 2]

%
% Append cubeCorners to pts2. Create and visualize the mesh collision geometry from the new array. Because the cube contains the sphere, the sphere points that are interior to the cube are disregarded when creating the geometry.

pts3 = [pts2;cubeCorners];
m3 = collisionMesh(pts3);
show(m3)

% Copyright 2019 The MathWorks, Inc.