Code: Select all
%David Diehl, 2010
clear,clc
%Prompt for input
r = input('Radius Size: ');
f = input('Friction Setting: ');
hs = input('High Speed Dampening: ');
ls = input('Low Speed Dampening: ');
%Get number of verts, ending cell, and make vert index matrix
vertCount = dlmread('Tri.jm', ' ', 'A3..A3'); %Number of vertices
vertEnd = num2str(3+vertCount); %ending line of vertice information
endCell = strcat('H', vertEnd); %N/A yet, used to make verts matrix
verts = dlmread('Tri.jm', ' ', 'A4..H12'); %Change '12' to the last ending row of verts
xyz = verts(:,1:3); %x y z coords for verts
[maxVerts,zzz] = size(verts); %max vertices
tris = dlmread('Tri.jm', ' ', [2 1 2 1]); %number of faces (tris)
faceStart = maxVerts + 3; %line on which tri index starts
faceEnd = faceStart + maxVerts + 2; %line on which tri index ends
faceMatrix = [0 0 0]; %initialize matrix
%Sort vertexs into matrix
v1M = [0 0 0]; %vert 1 2 3 matrix
v2M = [0 0 0];
v3M = [0 0 0];
for i = 1:tris
v1 = dlmread('Tri.jm', ' ', [faceStart 0 faceStart 0]); %gets vert 1 for tri 1 through max tris
faceStart = faceStart + 3; %sets the tri index line to the 2nd vertex in the tri
vertNum1 = xyz((v1+1),1:3); %sets the vertex coords from the # specified in v1
v1M = [v1M;vertNum1]; %puts all vertNum1 in matrix
end
v1M = v1M(2:end,:) %takes off initial zeros
faceStart = faceStart - (3*tris)+1; %resets the tri starting point to the 2nd vertex index
for i = 1:tris
v2 = dlmread('Tri.jm', ' ', [faceStart 0 faceStart 0]);
faceStart = faceStart + 3;
vertNum2 = xyz((v2+1),1:3);
v2M = [v2M;vertNum2];
end
v2M = v2M(2:end,:)
faceStart = faceStart - (3*tris)+1;
for i = 1:tris
v3 = dlmread('Tri.jm', ' ', [faceStart 0 faceStart 0]);
faceStart = faceStart + 3;
vertNum3 = xyz((v3+1),1:3);
v3M = [v3M;vertNum3];
end
v3M = v3M(2:end,:)
faceStart = faceStart - (3*tris)+1;
[rowVM,colVM] = size(v1M) %size of the vert indexs, same as tri count
for k = 1:rowVM
p1x = v1M(k,1);
p1y = v1M(k,2);
p1z = v1M(k,3);
p2x = v2M(k,1);
p2y = v2M(k,2);
p2z = v2M(k,3);
distR = sqrt(((p1x-p2x)^2) + ((p1y-p2y)^2) + ((p1z-p2z)^2)); %radius bewteen point 1 and 2
slope12 = asin((p1y-p2y)/distR); %slope in rad
d = 2*r; %diameter of sphere
%while(1)
%for l = 1:m
col = [p1x p1y p1z r f hs ls]
R1 = d/cos(slope12); %adjacent side of similar tri
alpha = atan((p1x-p2x)/(p1y-p2y)); %angle between hypotenuse and adjacent side
dx = R1*sin(alpha);
dy = R1*cos(alpha);
p1x = p1x + dx;
p1y = p1y + dy
m = m+1
%end
end
%makes collision file out of spheres set at vertex points (uses after above is complete, used for testing)
x = verts(:,1)';
y = verts(:,2)';
z = verts(:,3)';
nx = verts(:,4)';
ny = verts(:,5)';
nz = verts(:,6)';
u = verts(:,7)';
v = verts(:,8)';
locx = 0;
locy = 0;
locz = 0;
col = [locx locy locz r f hs ls];
for n = 1:maxVerts
locx = x(1,n)';
locy = y(1,n)';
locz = z(1,n)';
col = [col;locx locy locz r f hs ls];
end
col = col(2:end,:);
%writes collision file
dlmwrite('collision', maxVerts, ' ')
dlmwrite('collision', col, '-append', 'roffset', 0, 'delimiter', ' ')
type collision