• Remix
  • Share
  • New Entry

on 9 Nov 2023
  • 14
  • 29
  • 1
  • 0
  • 1676
Tumble Anything that fits in a unit sphere by remixing this!
Just be sure to parent your objects to TX2, the transform objects that moves and bounces and tumbles the pumpkin.
function drawframe(f)
if f==1
clf
TX = hgtransform('tag','bounce');
TX2 = hgtransform('tag','tumble','parent',TX);
%% PUMPKIN
bumps=10; bdepth=.1; bdepth2=.02; dimple=.2; width_r=1; height_r=.8;
[ Xs, Ys, Zs ] = sphere(199);
Rxy=(0-(1-mod(linspace(0,bumps*2,200),2)).^2)*bdepth + (0-(1-mod(linspace(0,bumps*4,200),2)).^2)*bdepth2;
Rz = (0-linspace(1,-1,200)'.^4)*dimple;
Xp = (width_r+Rxy).*Xs;
Yp = (width_r+Rxy).*Ys;
Zp = (height_r+Rz).*Zs.*(Rxy+1);
Cp = hypot(hypot(Xp,Yp),width_r.*Zs.*(Rxy+1));
%% STEM
sheight=.5; scurve=.4;
srad = [ 1.5 1 repelem(.7, 6) ] .* [ repmat([.1 .06],1,bumps) .1 ]';
[theta, phi] = meshgrid(linspace(0,pi/2,size(srad,2)),linspace(0,2*pi,size(srad,1)));
Xs = (scurve-cos(phi).*srad).*cos(theta)-scurve;
Zs = (sheight-cos(phi).*srad).*sin(theta) + height_r-max(0,dimple*.9);
Ys = -sin(phi).*srad;
surf(Xp,Yp,Zp,Cp,'parent',TX2);
shading interp
colormap([ linspace(.94, 1, 256); linspace(.37, .46, 256); linspace(0, .1, 256) ]');
surface(Xs,Ys,Zs,[],'FaceColor', '#008000', 'EdgeColor','none', 'Parent',TX2);
material([ .6, .9, .3, 2, .5 ])
lighting g
%% Axes setup
axis([-2 2 -2 2 1.75 7.8])
daspect([1 1 1])
view(3)
set(gca,'clipping','off','visible','off','xdir','r');
light('position',[ 0 -10 5],'style','local');
%% Motion Context
ctxt.Z = 9;
ctxt.Q = 0;
ctxt.Zv = 0;
ctxt.B = 0;
setappdata(gcf,'ctxt',ctxt);
else
TX = findobj('tag','bounce');
TX2 = findobj('tag','tumble');
end
ctxt = getappdata(gcf,'ctxt');
%% Compute cheap-o velocity, bounce, and squish
ctxt.Zv=ctxt.Zv+.032; % Cheap Gravity
ctxt.Z=ctxt.Z-ctxt.Zv; % Motion
if ctxt.Z<0 % bounce!
ctxt.Z=ctxt.Zv+ctxt.Z; % Push up by amount pushed below
ctxt.Zv = -ctxt.Zv;
ctxt.Q = 1;
end
ctxt.B = ctxt.B + 1/4;
bounce=cospi(ctxt.B);
ctxt.Q=max(ctxt.Q-.036,0); % Q tracks the magnitude of squish
bscale = (ctxt.Q*bounce)/2;
%% Create transform to move and squish the pumpkin
set(TX, 'Matrix', makehgtform('translate',[0 0 ctxt.Z], 'scale', [1+bscale 1+bscale 1-bscale]));
%% Create the transform to tumple the pumpkin
% divide by 12 for 2 tumbles, and 24 for a single tumble.
set(TX2, 'Matrix', makehgtform('xrotate',(f-24)/12*pi));
setappdata(gcf,'ctxt',ctxt);
end
Animation
Remix Tree