• Remix
  • Share
  • New Entry

on 13 Oct 2021
  • 9
  • 35
  • 0
  • 0
  • 262
% Signed Distance Function (SDF) of Torus
e=@(x)((x(:,1).^2+x(:,2).^2).^.5-1).^.5+x(:,3).^2-1;
E=8e2;
x=[0,2,0];
v=x-4;
s=E;
% bouncing particle simulation
for j=0:(E*3)
p=x(end,:);
d=e(p);
% if hits boundary, compute reflected velocity
if d>=-1/E
% compute gradient of SDF
n=(e(repmat(p,[3,1])+eye(3)/E).'-d)*E;
n=n/norm(n);
v=v-2*n*v.'*n;
end
% update vectors: timestep = 1/10
x=[x;p+v/10];
s=[s;rand*E];
end
% plotting magic ;)
scatter(x(:,1),x(:,2),s,s,'.');
colormap(turbo);
axis equal off;
Remix Tree