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
这是一个烂摊子,但,是的,我在这里输了...