clear all;
close all;
clc;
count = 1;
%Declaring the balls initial conditions
R_Ball = 2;
initpos.x = 0;
initpos.y = 2.4;
initvel.x = 2;
initvel.y = 4;
gravity.x = 0;
gravity.y = 9.81;
restitution = 0.7;
GroundBall_friction = 0.2;
%Animation timestep
dt = 0.01;
%Executing the animation
pos.x = initpos.x; % initial position
pos.y = initpos.y; % initial position
vel.x = initvel.x; % initial velocity-x
vel.y = initvel.y; % initial velocity-y
t_arc = linspace(0,(2*vel.y)/gravity.y,4000);
for k = 1:4000
%Updating the ball's position
vel.x = vel.x;
vel.y = vel.y - gravity.y*t_arc(k)
pos.x = pos.x + vel.x*t_arc(k);
pos.y = pos.y + vel.y*t_arc(k) - (1/2)*gravity.y*(t_arc(k).^2);
if vel.y < 0 && pos.y < 0
vel.y = (restitution)*vel.y;
vel.y = -vel.y;
vel.x = vel.x + GroundBall_friction*(restitution - 1)*vel.x;
end
if vel.x < 0.
break;
end
clf;
%Drawing the frame
subplot(2,1,1)
hold on
line([0 30],[0 0]);
rectangle('position', [pos.x pos.y R_Ball R_Ball],'Curvature',[1 1],'FaceColor','r');
posxx(count) = pos.x;
posyy(count) = pos.y;
plot(posxx + 1/2*R_Ball, posyy + 1/2*R_Ball,'b');
axis([0 30 0 10]);
hold off
subplot(2,1,2)
hold on
velyy(count) = vel.y;
velxx(count) = vel.x;
plot(posxx,velxx);
plot(posxx,velyy);
count = count+1;
hold off
axis([0 30 -10 10]);
%Refresh rate
pause(dt)
end
How can I maintain my ball rolling on the X-axis and not pass through the Y-axis.

 採用された回答

Image Analyst
Image Analyst 2017 年 2 月 11 日

0 投票

Add this to the end of the loop, just before the final "end"
if pos.y < 0
break;
end

7 件のコメント

Jack Zimmerman
Jack Zimmerman 2017 年 2 月 11 日
編集済み: Jack Zimmerman 2017 年 2 月 11 日
This doesnt work since the Ball doesn't complete the first bounce.
Image Analyst
Image Analyst 2017 年 2 月 11 日
When it stops, the y value is
pos.y
ans =
-0.0392194572774256
So it's below the x axis since it has a negative y. What values of y do you want it to quit for?
Jack Zimmerman
Jack Zimmerman 2017 年 2 月 11 日
編集済み: Jack Zimmerman 2017 年 2 月 11 日
I want the ball to move along the X-Axis so when the X-Velocity tends to 0 it stops on the X-axis at Y = 0. Preferably, I'd like the ball to never go below the X-axis at any point in the animation. I dont want it to quit at some Y position but complete the motion and stop without slowly falling.
Image Analyst
Image Analyst 2017 年 2 月 11 日
What do you want to happen when it goes negative? If you don't want to break, maybe just clamp it at 0
if pos.y < 0
pos.y = 0;
end
Jack Zimmerman
Jack Zimmerman 2017 年 2 月 12 日
Thanks that seems to work.
Walter Roberson
Walter Roberson 2017 年 2 月 12 日
pos.y = max(0, pos.y)
does the same thing without needing an "if"; and it can be vectorized.
Jack Zimmerman
Jack Zimmerman 2017 年 2 月 12 日
Exactly what I needed Thank you.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Animation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by