0
我有一個關於如何檢測這裏的碰撞的殘端,我們只使用數據向量和沒有類(我知道類已經實現,但我不允許使用他們)。多圈圓碰撞的算法
那麼,我該怎麼做呢?在互聯網上有一些例子,但他們只看兩個圈子。我有一個用戶定義的圈數(球)。那麼我需要確定哪些是最近的距離,如果是的話,怎麼樣?我很困惑如何從這個開始。
到目前爲止,我可以在屏幕上隨機位置創建一定數量的圓圈,並使它們沿相同的方向移動(這很好)。所有的球具有相同的半徑並具有相同的速度。
下面的代碼的主要部分:
N_balls = input('Enter the amount of balls: ');
radius = input('Enter radius for all balls: ');
for i = 1:N_balls
x_init(i) = randi([27, 550]);
y_init(i) = randi([27, 400]);
end
for i = 1:N_balls
ball(i) = drawBall(x_init(i), y_init(i), radius, 'r');
end
% Initialize a loop control variable
exit_loop = false;
x = [];
y = [];
velocity_x = 1;
velocity_y = 1;
distance = [];
% While is not commanded to exit the loop
while exit_loop == false
for i = 1:N_balls
[x(i), y(i)] = getCenter(ball(i));
xMove(ball(i), velocity_x);
yMove(ball(i), velocity_y);
redraw;
end
for i = 2:N_balls
distance(i-1) = sqrt(((x(i-1) - x(i)) * (x(i-1) - x(i))) + ( (y(i-1) - y(i)) * (y(i-1) - y(i))));
end
[dist, ball_num] = min(distance);
end
這是一個爛攤子,但,是的,我在這裏輸了...