Home > Back-end >  3D animation using comet3
3D animation using comet3

Time:09-21

I am doing a numerical simulation of the gravitational pull of two different bodies (also collisions) and now I am stuck because I wanted to create an animation of the system over time but found 3 problems:

The first is that I cannot trace a sphere in the path of the particle.

The second is when I tested the command comet3()

t=0:0.1:50;
axis([-2  12  -2  12   -7  7])
axis square
view([-20  20])
comet3 (x(t),y(t), z(t), 0.1);

hold off;

where x(t) is the component x and so on, I don't see anything.

And finally the last problem is that from what I saw the most common animations are at constant speed but my system has variable acceleration, there would be some way to add that too?

CodePudding user response:

I think your problem cannot be solved with comet3. So, my answer presents an alternative and more generic solution for drawing a moving sphere.

Main idea is to draw the sphere once and then to update its position in a loop. Of course, you could also draw other or more shapes to the axis and update them as well.

Please find further explanations inline.

%  Clear current figure;
clf;

% Get coordinates of a sphere
[X,Y,Z] = sphere;

% Create an axis for drawing
ax = axes;

% Draw sphere 
h = surf(ax, X,Y,Z);

% Set limits of the axis
ax.YLim = [ -1 20];
ax.ZLim = [ -1 20];
ax.XLim = [ -1 20];


% Set step size with which the object is moving
stepSize = 0.02;

for k = 1 : stepSize : 20
    
    % Manipulate position of the sphere
    h.ZData = h.ZData   stepSize;
    h.XData = h.XData   stepSize/2;
    
    % Force MATLAB to update the figure
    drawnow;
    
    % Wait some time to adjust speed
    pause(0.005);
    
end

Note that the code is tested in MATLAB and not in Octave.

  • Related