Animating a circle inside a rectangle
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
The goal is to create an animation that represents an animated (separate successive images separated by a time 'dt') particle (28*R pt, 'R' is the radius, and the position of its center is r=[x;y]) during a time interval 'deltat' within a rectangle (width=a and height=b), with a certain velocity v=[vx;vy]. This particle collides with the 'walls' of the rectangle and conserves its kinetic energy. The collision with the wall x=0 happens in a deltat=(R-r(1))/v(1), and the collision with the wall x=a happens in a deltat=(a-R-r(1))/v(1).
I tried to represent this, but there's always something wrong with the code.
[MY CODE]
/////////////////////////////////////////
a=20
b=10
R=0.5
v=[10;10]
r=[10;5]
vx=v(1)
vy=v(2)
x0=r(1)
y0=r(2)
dt=0.1
deltat=5
X=zeros(1,500)
%x to the right
xright=((a-R-x))/vx
x0 = a/2;
y = b/2;
for i=2:1:deltat
%rectangle
plot([0,a],[0,0],'r-',[a,a],[0,b],'r-',[0,0],[0,b],'r-',[0,a],[b,b],'r-')
hold on
%circle
X(i) = x(i-1) + vx*dt
plot(X, y,'ro','MarkerSize',28*R);
axis ([-10,30,-10,20])
hold off
pause(0.05)
end
/////////////////////////////////////////
My logic was to literally trace the circle's movement (first from the center to the right wall, then to the center again towards the left wall (...) ). Nonetheless, the code does not work.
Thank you.
採用された回答
darova
2020 年 3 月 22 日
Mistake:

Mistake

Also you shoud have conditions when ball collides:
if (x<0)||(a<x)
vx = -vx; % change sign
end
the same for vy
Add line (similar to yours)
Y(i) = Y(i-1) + vy*dt;
You can also add gravity
vy = vy - 9.8*dt;
14 件のコメント
darova
2020 年 3 月 22 日
You don't have to draw the box each loop

David Eufrásio
2020 年 3 月 22 日
Thank you so much for your help.
Though, I tried what you said, but there are 3 questions that popped up:
- Whenever I change the velocity, the less positions the circle has... Shouldn't velocity not have any effect on that?
- The circle shows 'dragging' (not only 1 circle appears). Could that be a result of the 'hold on' // 'hold off'? Also, there is a circle on (0,0), I can't find the reason why...
- While running the code, the circle stops after getting to x=14 , y=9 , and it doesn't return - shouldn't the sign change on the equation?
Thank you once more. Here's how the code is currently:
a=20
b=10
R=0.5
v=[10;10]
r=[10;5]
vx=v(1)
vy=v(2)
x=r(1)
y=r(2)
dt=0.1
deltat=5
X=[x zeros(1,deltat)]
Y=[y zeros(1,deltat)]
%x to the right
xdireita=((a-R-x))/vx
%rectangle
plot([0,a],[0,0],'r-',[a,a],[0,b],'r-',[0,0],[0,b],'r-',[0,a],[b,b],'r-')
hold on
for i=2:1:deltat
X(i) = X(i-1) + vx*dt
Y(i) = Y(i-1) + vy*dt
if (X(i)<0)||(a<(X(i))
vx = -vx;
end
if (Y(i)<0)||(b<Y(i))
vy = -vy;
end
plot(X, Y,'ro','MarkerSize',28*R);
axis ([-10,30,-10,20])
pause(0.05)
end
hold off

darova
2020 年 3 月 22 日
Here are some tips
- Tip 1: make you trajectory longer

- TIp 2: remove dragging effect - draw circle, wait, delete

darova
2020 年 3 月 22 日
Also i have some warnings:

Do you know what do they mean?
David Eufrásio
2020 年 3 月 22 日
There is just a little detail that I'm missing... it's the fact that the particle goes past the rectangle (it only changes position when the center of the circle hits the x=a (...) ).
Even if I write
if ((X(i)-R)<0)||(a<(X(i)-R))
vx = -vx;
end
It doesn't change anything. What could be reason for that?
I believe the warnings are due to the semicolons I am not used to put... That's something I have to change in the future.
darova
2020 年 3 月 22 日
When ball bounces the wall you need to move X(i) a bit
Sometimes next position stil outside the box. Any ideas?

David Eufrásio
2020 年 3 月 22 日
My first idea would be to subtract the radius to the X(i) or Y(i) in the condition, because technically the ball has to change the sign of vx or vy whenever it has reached the X-R position, right? But it doesn't seem to work...
Maybe a command to eliminate all the values that are <0 or >a?
darova
2020 年 3 月 23 日
When the ball bounces (outside box) next position should be corrected

something like
X(i) = X(i) - dx;
David Eufrásio
2020 年 3 月 23 日
編集済み: David Eufrásio
2020 年 3 月 23 日
Thank you so much for the help. It finnally worked!
Here is the final code:
clear all
close all
clc
a=20;
b=10;
R=0.5;
v=[10;10];
r=[10;5];
vx=v(1);
vy=v(2);
x=r(1);
y=r(2);
dt=0.1;
deltat=1000;
X=[x zeros(1,deltat)];
Y=[y zeros(1,deltat)];
plot([0,a],[0,0],'r-',[a,a],[0,b],'r-',[0,0],[0,b],'r-',[0,a],[b,b],'r-')
hold on
for i=2:1:deltat
X(i) = X(i-1) + vx*dt;
Y(i) = Y(i-1) + vy*dt;
if (X(i)-R<=0)||(a<=X(i)+R)
vx = -vx;
end
if (Y(i)-R<=0)||(b<=Y(i)+R)
vy = -vy;
end
if (X(i)>a) || (Y(i)>b)
X(i+1)= X(i)-X(i-1);
Y(i+1)= Y(i)-Y(i-1)
end
particle = plot(X(i), Y(i),'ro','MarkerSize',28*R);
axis ([-10,30,-10,20])
pause(0.05)
delete(particle)
end
hold off
darova
2020 年 3 月 23 日
You created conditions for right and top boundaries. What about left one?
David Eufrásio
2020 年 3 月 23 日
Good point. Corrected it already. Thank you so much!
David Eufrásio
2020 年 3 月 28 日
Hello again!
Is there any way I can get the array X and the array Y not depend on 'deltat' and still make the animation work?
function animacao(r,v,a,b,R,deltat,dt)
vx=v(1);
vy=v(2);
x=r(1);
y=r(2);
plot([0,a],[0,0],'r-',[a,a],[0,b],'r-',[0,0],[0,b],'r-',[0,a],[b,b],'r-')
hold on
X=[x zeros(1,deltat)]; %here
Y=[y zeros(1,deltat)];
for i=2:dt:deltat
X(i) = X(i-1) + vx*dt;
Y(i) = Y(i-1) + vy*dt;
if (X(i)-R<=0)||(a<=X(i)+R)
vx = -vx;
end
if (Y(i)-R<=0)||(b<=Y(i)+R)
vy = -vy;
end
if (X(i)>a) || (Y(i)>b) || (X(i)<0) || (Y(i)<0)
X(i+1)= X(i)-X(i-1)
Y(i+1)= Y(i)-Y(i-1)
end
particula = plot(X(i), Y(i),'go','MarkerSize',28*R,'MarkerFaceColor','g');
axis ([-10,a+10,-10,b+10])
pause(0.05)
delete(particula)
end
hold off
darova
2020 年 3 月 28 日
Why can't you declare deltat inside function?
David Eufrásio
2020 年 3 月 28 日
Basically, some steps ahead in the main script the deltat (the time a particle takes to collide with a wall, or with another particle) is calculated for each cycle the code does, and the animation will draw the movement for that deltat. The problem is that sometimes deltat isn't an integer number (it's decimal), and therefore the instruction zeros gives an error.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Animation についてさらに検索
製品
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
