Control speed of a moving marker in animation

8 visualizzazioni (ultimi 30 giorni)
Matevz
Matevz il 14 Ago 2023
Hello, I'm trying to do a simple animation of billiard in 2d. I know how to do the animations and have worked out the physics behind it, I just can't seem to figure out how to control the speed of the ball (marker) in animation.
More precisely, given a pool table and a ball on it with some initial velocity, I assume it moves in straight lines and take into accunt the force of friction. Naturally the ball slows down and stops over time. I can make an animation of how the ball moves, but have no idea how to make it go slower and slower until it stops.
Here's an example of animation I want to go slower and slower:
%Random Data
x1 = linspace(0, 3, 90);
y1 = x1(1:30)./2 + 1;
y2 = -3.*x1(31:end)./4 + 9/4;
x3 = linspace(3,0,90);
y3 = -1/2.*x3 + 1.5;
x4 = linspace(0,3,90);
y4 = 1.5.*ones(1,length(x4));
x5 = linspace(3,1.5,90);
y5 = (0-1.5)./(1.5-3) .*x5 + 1.5 - 3*(0-1.5)./(1.5-3);
x = [x1 x3 x4 x5];
y = [y1 y2 y3 y4 y5];
%Animated Figure
figure;
hold on;
plot([0 0 3 3 0], [0 1.5 1.5 0 0],'LineWidth',3,'Color','b')%pool table
axis([-1 3 -2 3]);
grid on;
box on;
hPlot = plot(NaN,NaN,'O');
for k = 2:length(x) %loop
set(hPlot, 'XData', x(k), 'YData', y(k));
drawnow();
pause(0.05)
end
hold off;

Risposte (1)

Chhayank Srivastava
Chhayank Srivastava il 14 Ago 2023
Hi,
I modified your code only to make it slower. In this version you only have to update one variable ie update_rate which which would increase the number of points and thus make it slower.
Hope it helps!!
%Random Data
x1 = linspace(0, 3, 90);
y1 = x1(1:30)./2 + 1;
y2 = -3.*x1(31:end)./4 + 9/4;
x3 = linspace(3,0,90);
y3 = -1/2.*x3 + 1.5;
x4 = linspace(0,3,90);
y4 = 1.5.*ones(1,length(x4));
x5 = linspace(3,1.5,90);
y5 = (0-1.5)./(1.5-3) .*x5 + 1.5 - 3*(0-1.5)./(1.5-3);
x = [x1 x3 x4 x5];
y = [y1 y2 y3 y4 y5];
%Animated Figure
figure;
hold on;
plot([0 0 3 3 0], [0 1.5 1.5 0 0],'LineWidth',3,'Color','b')%pool table
axis([-1 3 -2 3]);
grid on;
box on;
hPlot = plot(NaN,NaN,'O');
update_rate = 2; %How much you want to make it slow
b = 1:size(x,2); %base query points
nb = 1:1/update_rate:size(x,2); %new query points
x_new = interp1(b,x,nb); %interpolated x
y_new = interp1(b,y,nb); %interpolated y
for k = 2:length(x_new) %loop
set(hPlot, 'XData', x_new(k), 'YData', y_new(k));
drawnow();
pause(0.05)
end
hold off;

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Tag

Prodotti


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by