- /
-
What Goes Up...
on 28 Oct 2024
- 24
- 124
- 0
- 1
- 1645
Cite your audio source here (if applicable):
drawframe(1);
Write your drawframe function below
function drawframe(f)
persistent hAx bP bX bY pY bR bps status sP fP S Z RFud RC eB E
% setup
if f==1
%% anonymous helper functions
S = @linspace;
Z = @zeros;
RFud = @(A) [A;flipud(A)];
RC = @(A,C) repmat(A,C,1);
eB = @(th) [cosd(th),sind(th)];
E = @(X,Y,a,b) eB((0:1:359)').*[a,b] + [X,Y];
%% figure/axes
% store axes handle
hAx = gca();
% set axes props
set(hAx,...
'Visible','off',...
'Units','Normalized',...
'InnerPosition',[0 0 1 1],...
'XLim',[-0.9 0.9],...
'YLim',[-0.4 1.4],...
'NextPlot','Add');
% set figure backgroud color
set(gcf,'Color',[.24 .89 .7]);
%% ball properties
% bounces/s
bps = 0.5;
% x position
bX = 0;
% y position
bY = 0;
% previous y position
pY = 1; % arbitrary start value
% status ('up','down','bounce','top')
status = 'bounce';
% radius
bR = 0.2;
%% graphics objects
% shadow patch
sP = patch(hAx,...
'Faces',1:360,...
'Vertices',Z(360,2),...
'FaceVertexCData',RC([0.25 0.25 0.25],360),...
'EdgeAlpha',0,...
'EdgeColor',[0 0 0],...
'LineWidth',1);
% ball patch
bP = patch(hAx,...
'Faces',1:360,...
'Vertices',Z(360,2),...
'FaceVertexCData',RC([1 1 0],360),...
'FaceColor','interp',...
'EdgeColor',[0 0 0],...
'LineWidth',2);
% face patch
fP = patch(hAx,...
'Faces',[1:360;361:720;721:1080],...
'Vertices',Z(1080,2),...
'FaceVertexCData',RC([0 0 0],1080),...
'EdgeColor',[0 0 0],...
'LineWidth',3);
end
% y position of the ball in this frame
bY = abs(sin(((f-1)/96)*4*pi*bps));
% determine ball status and set parameters used to update the patches
if bY < bR % ball is compressing
% set status
status = 'bounce';
% set ellipse params
y = max(bY,0.5*bR);
a = min(2*bR-bY,1.5*bR);
b = y;
as = a;
else % ball is in the air
% set status
if bY > 0.9
status = 'top';
elseif bY < pY
status = 'down';
else
status = 'up';
end
% set ellipse params
offset = min((1-bY)/10,.2*bR);
y = bY;
a = bR-offset;
b = bR+offset;
as = bR*(1.25-bY);
end
% get patch data and update patches
set([bP,sP,fP],{'Vertices'},{E(0,y,a,b);E(0,0,as,.015);F(0,y,a,b)},{'FaceAlpha'},{1;1-bY;1});
% set pY to be used as previous y position in next frame
pY = bY;
drawnow;
% get patch Vertices for the face
function V = F(X,Y,a,b)
% eye vertices (left only)
eV = E(-.3*a,Y+.3*b,bR*.1,bR*.2);
switch status
case 'up'
% mouth vertices
m = eB(S(-30,-150,180)');
mV = [RFud(.6*m)].*[a,b] + [X,Y];
case 'down'
% mouth vertices
mV = E(bX,Y-.5*bR,a*.2,b*.2);
case {'bounce','top'}
% mouth vertices
mV = [RFud(S(-.6*a,.6*a,180)'),RC(Y-.3*b,360)];
% eye vertices (left only)
eX = S(0,.6*a,180)';
eV = [RC(RFud(eX(1:90)),2),RFud(tand(15)*eX)] + [-.5*a,Y+.05*b];
end
% final vertices - [mouth;left eye;right eye]
V = [mV;eV;abs(eV)];
end
end
Movie
Audio
This submission does not have audio.