How can I avoid flickering animation of multiple balls using plot3?
13 views (last 30 days)
Show older comments
Hello everyone,
its the first time I post into a forum and I will start directly with my problem description. Im trying to program a small simulation of particle collisions in 3D. Just a cubic space with multiple balls/spheres in it, which can collidate with boundaries and other particles. Like already mentioned I use plot3 because with this function I can simulate much more spheres without big lagging of the caluclations, compared with the function 'spheres(x,y,z)'. The disadvantage is that the balls are flickering like you can see in the mp4 video I attached.
The concept of my program is very common:
while
- Plot balls
- Pause of 0.001 seconds
- Calculate Next position with velocities and program runtime
- Delete balls
end
The function for plotting the balls in my program looks like this:
function [balls,particlesArray_partial]=PlotBall_hom(particlesArray_partial,balls,colors)
n=size(particlesArray_partial,2);
r=particlesArray_partial(7,1);
hold on
balls(1,:)=plot3(particlesArray_partial(1,:),particlesArray_partial(2,:),particlesArray_partial(3,:),...
'o','MarkerSize',r,'MarkerEdgeColor','k','MarkerFaceColor','bl');
hold off
end
Sorry if there is already a thread like this but I didnt found some.
Your sincerely
0 Comments
Answers (1)
Geoff Hayes
on 7 Apr 2020
Edited: Geoff Hayes
on 7 Apr 2020
Matthias - rather than deleting the balls (on each iteration), try re-using the graphics object handle to each ball:
close all;
hBallPlots = [];
hBallPlots(1) = plot3(NaN, NaN, NaN, 'o','MarkerSize',5,'MarkerEdgeColor','g','MarkerFaceColor','g');
hold on;
hBallPlots(2) = plot3(NaN, NaN, NaN, 'o','MarkerSize',5,'MarkerEdgeColor','r','MarkerFaceColor','r');
x = [-2*pi:0.01:2*pi; 2*pi:-0.01:-2*pi];
y = sin(x);
z = cos(x);
xlim([min(x(:)), max(x(:))]);
ylim([min(y(:)), max(y(:))]);
zlim([min(z(:)), max(z(:))]);
for k = 1:length(x)
for j = 1:length(hBallPlots)
set(hBallPlots(j),'XData',x(j,k), 'YData', y(j,k), 'ZData', z(j,k));
end
pause(0.001);
end
The only reason I have the inner for loop was because the balls were being coloured the same rather than red and green (in the above example which just might be a problem with my older version of MATLAB). If all balls are the same colour, then you could simplify with just
for k = 1:length(x)
set(hBallPlots,'XData',x(:,k), 'YData', y(:,k), 'ZData', z(:,k));
pause(0.001);
end
4 Comments
Geoff Hayes
on 8 Apr 2020
The for loop shouldn't be causing a lag since it is only ever called once if the hBallPlots is empty
if isempty(hBallPlots)
hBallPlots = zeros(n,1);
for k = 1:n
hBallPlots(k) = plot3(NaN, NaN, NaN, 'o','MarkerSize',5,'MarkerEdgeColor','g','MarkerFaceColor','g');
hold on;
end
end
. If it is always being called whenever your code calls Plotballs then an incorrect hBallPlots is being passed in and it should be defined as an empty array in your main script
balls = [];
so perhaps that was the problem. Please try initializing it to an empty array, put the for loop code back in, and maybe that will improve performance (fingers crossed!).
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!