- use trig and come up with arrival/departure angles
- use a simple scheme like "collisions with the floor and ceiling change the sign of the y velocity, collisions with the walls change the x velocity"
Particle animation inside a rectangle
조회 수: 3 (최근 30일)
이전 댓글 표시
So I need to do a circle that it's moving inside a rectangle and colliding with the "walls" of it, with constant velocity. I really don't know hot to do that. I would like to do something like the DVD stand by animation but with a circle and less complex. Can somebody help me?
댓글 수: 0
채택된 답변
Mark Rzewnicki
2020년 3월 20일
What do you have so far?
I wouldn't worry about the animation at this point; there are a bunch of ways to make that happen once you have a good underlying program that tells you where the particle is. An animation example (with random particle positions) might look something like this:
xc=1; yc=2; r=1;
theta = 0:pi/30:2*pi;
x = r * cos(theta) + xc;
y = r * sin(theta) + yc;
circle = plot(x, y,'k');
axis([-20 20 -20 20]);
for k=1:20
rectangle('Position',[-17 -12 35 25]);
circle.XData = x + randi([-10 10],1);
circle.YData = y + randi([-10 10],1);
pause(0.5);
end
What you need to do is set the initial conditions for the particle (position & velocity in the x & y directions) and devise a way to determine its position for the length of the simulation. Something along the lines of:
new x position = old x position + x velocity * time
new y position = old y position + y velocity * time
And you'll need a scheme for handling collisions, which of course will be triggered when the x or y position of the particle (or its edge - if you define the center of the particle don't forget to add the radius back on) equals the x or y borders of the rectangle (think if/else statements there). You could:
Excited to see what you come up with! I once made a Pong game in VHDL for a digital systems course - similar stuff here.
댓글 수: 2
Mark Rzewnicki
2020년 3월 20일
편집: Mark Rzewnicki
2020년 3월 20일
--------------@gmail.com
Don't hesitate to reach out!
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Animation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!