• Remix
  • Share
  • New Entry

  • Tim

  • /
  • Happy pumpkin

on 9 Nov 2023
  • 20
  • 63
  • 0
  • 2
  • 1952
Bounce Anything that fits in a unit sphere by remixing this!
Just be sure to parent your objects to TX, the transform objects that moves and bounces the pumpkin.
function drawframe(f)
if f==1
clf
TX = hgtransform('tag','bounce');
%% 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',TX);
shading interp
colormap([ linspace(.94, 1, 256); linspace(.37, .46, 256); linspace(0, .1, 256) ]');
surface(Xs,Ys,Zs,[],'FaceColor', '#008000', 'EdgeColor','none', 'Parent',TX);
[x,y,z] = sphere(100);
surf(x/3+.55, y/3, z/3+.6, 'FaceC','w', 'EdgeC','none', 'Parent',TX);
surf(x/3, y/3-.55, z/3+.6, 'FaceC','w', 'EdgeC','none', 'Parent',TX);
surf(x/5+.6, y/5-.1, z/3+.68, 'FaceC','k', 'EdgeC','none', 'Parent',TX);
surf(x/5+.1, y/5-.63, z/3+.64, 'FaceC','k', 'EdgeC','none', 'Parent',TX);
z(z > 0) = nan
surf(x/2+.6, y/2-.6, z/1.1+.6, 0*x+cat(3, 1, 1, 1), 'FaceC','k', 'EdgeC','none', 'Parent',TX);
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');
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]));
setappdata(gcf,'ctxt',ctxt);
end
Animation
Remix Tree