- /
-
Bouncing MATLAB
on 11 Oct 2024
- 16
- 124
- 3
- 1
- 1749
I chopped up the sound to pick the boing noises I liked, and then synchronized different sounds to different bouncing letters.
function drawframe(f)
% Shorteners
q=@triangulation;
j=@polyshape;
K='KeepCollinearPoints';
% Transforms
persistent WX WR
if f==1
%% WORD SHAPE
% Basic idea - draw a word using a text object in a wide
% figure, screen-cap it, then contourit.
g=figure('Color','w','Vis','of','Pos',[ 100 100 3000 300 ]);
a=axes(g,'Vis','off');
t=text(a,15,15,'M A T L A B','Units','pixel',...
'FontSize',30,'FontWeight','bold','FontUnits','Pix',...
'VerticalA','baseline');
F=getframe(a,t.Extent);
close(g)
V=contourc(double(flipud(F.cdata(:,:,1))),[50 50])';
% Convert contour matrix into polyshape verts
i=1;
while i<size(V,1)
n=V(i,2);
V(i,:)=nan;
i=i+1+n;
end
% Center and scale to unit height
V=(V-min(V))/max(V(:,2),[],'all');
% Convert to polyshape so we get a clean triangulation
W1=j(V(2:end,:),K,true);
W1=W1.polybuffer(.011);
%% Create the WordSphere
% Constants
Th=.5; % radius of inner/outer part of sphere.
WX=gobjects(0);
WR=gobjects(0);
% Get the regions (one per letter) - sort regions left to right
W2=sortregions(W1,'centroid','ascend','ReferencePoint',[0 0]);
r=regions(W2);
% Pick nice colors for the letters.
colororder dye
for k=1:numel(r)
T=q(r(k));
% Aspects of the triangulation.
V=T.Points; % Get improved V
F=T.ConnectivityList;
E=T.freeBoundary;
% Center each letter around 0
WW=diff([min(V);max(V)]);
V=V-min(V)-WW/2.2;
% Constants for this shape when computing connecting edges
R=1:size(E,1); % All the boundary edges.
L2=size(V,1); % Layer 2 starts here.
% New triangulation faces
NF=[F; F+L2; % top and bottom
E(R,[2 1 2])+[0 0 L2]; % edges connecting top/bottom
E(R,[1 1 2])+[0 L2 L2];
];
% New border to draw lines on
EF=[E;E+L2];
WX(k)=hgtransform;
WR(k)=hgtransform(WX(k));
WT=q(NF,[V(:,1) zeros(L2,1) V(:,2)
V(:,1) ones(L2,1)*Th V(:,2) ]);
% Using SeriesIndex lets me color each letter differently
% without having to do wierd indexing myself. See the
% 'colororder' command above that picked out some nice colors.
patch(WR(k),'Vertices',WT.Points,'Faces',WT.ConnectivityList,...
'SeriesI',k,'FaceC','flat',...
'FaceA',1,...
'EdgeC','n');
patch(WR(k),'Vertices',WT.Points,'Faces',EF,...
'FaceVertexC',[],'FaceC','none','EdgeC','w','LineW',3);
end
% Prettify
set(gcf,'Color','k');
daspect([1 1 1]);
view([0 0])
set(gca,'pos',[0 0 1 1],'clipping','off');
axis([-.5 3.8 -1 1 0 3],'off')
%camzoom(1.2)
end
% Make the first frame look good for our minihack icon
% Note: I wrote this last year but missed publishing before
% the contest ended. Thus 48 frames.
f = mod(f-2,48)+1;
%% Place each letter bouncing to it's own beat.
% Vertical path
spc=(1:48)/48;
Yp=abs(cospi(spc*2));
Rp=spc*2*pi;
Sd=[0 diff(Yp)];
Sm=1-Yp;
Sm(Sd<0)=0;
Sv=sinpi(spc*12);
% Kerning:
Wk = [0.77 0.55 0.61 0.66 0.76 0.65];
Ws = [14 7 0 15 5 38];
Wh = [1.3 1 .5 1 .7 1.5];
Wrs = [0 0 1
0 1 1
1 0 0
1 1 0
0 0 1
1 0 1];
Wrm = [ .5 1 2 1 .5 1];
accum=0;
t=@makehgtform;
for k=1:numel(WX)
fk=mod(f+Ws(k),48)+1;
WR(k).Matrix = t('axisrotate',Wrs(k,:),Rp(f)*Wrm(k));
WX(k).Matrix = t('translate',[accum 0 Yp(fk)*Wh(k)*3],...
'scale', 1+[-Sv(fk) -Sv(fk) Sv(fk)]*Sm(fk)/2);
accum=accum+Wk(k);
%% Sound generation.
% Using an external tool, when letter hits bottom, signal an event
% that triggers a sound effect at this time that then gets mixed
% with other sound events.
%if Yp(fk)==0
% AS.event(k,(k-3.5)/2.5);
%end
end
end