bobo老师您好,添加circle互相碰撞机制后,我有一个问题:
一开始页面不会有bug,但碰撞事件较密集(例如多个circle在边界碰撞)会出现circle“吸附”到绘图板边界的情况(我猜测是否和每次绘图的刷新帧率(这里是1000/20=50)有关),如图:
circle之间碰撞逻辑如下:
public void move(int minx,int miny,int maxx,int maxy,Circle[] circles) {
x += vx;
y += vy;
checkCollision(minx,miny,maxx,maxy,circles);
}
private void checkCollision(int minx,int miny,int maxx,int maxy,Circle[] circles) {
//监测边界碰撞
if (x <= minx || x >= maxx) vx = -vx;
if (y <= miny || y >= maxy) vy = -vy;
//监测圆的碰撞,碰撞的判定条件为圆心距离之和小于2r
for (Circle c : circles) {
if (this == c) continue;
//交换速度
if (getD(this,c) <= 4*r*r) {
int cvx = c.vx, cvy = c.vy;
c.vx = vx;
c.vy = vy;
vx = cvx;
vy = cvy;
}
}
}
//得到两圆心距离的平方
private int getD(Circle c1, Circle c2) {
return (int) (Math.pow(Math.abs(c1.x-c2.x),2) + Math.pow(Math.abs(c1.y-c2.y),2));
}
相应地我在初始生成circle数组时避免circle之间的重叠:
//绘制n个半径为r的圆
public static Circle[] generateCircles(int n, int r, int sceneWidth, int sceneHeight) {
Circle[] circles = new Circle[n];
int[][] location = new int[n][2];
for (int i = 0; i < n; i++) {
int x,y;
//检查初始是否有重叠,如果有重叠则重新生成x和y
do {
x = r+(int)(Math.random()*(sceneWidth-2*r));
y = r+(int)(Math.random()*(sceneHeight-2*r));
} while (checkIfInValid(location,x,y,r));
location[i][0] = x;
location[i][1] = y;
int vx = (int)(Math.random()*11)-5;
int vy = (int)(Math.random()*11)-5;
circles[i] = new Circle(x,y,vx,vy,r);
}
return circles;
}
//检查是否有重叠,如果有重叠返回true,如果没有重叠返回false
private static boolean checkIfInValid(int[][] location, int x, int y,int r) {
for (int[] ints : location) {
int cx = ints[0], cy = ints[1];
if (cx == 0) break;
int d2 = (int) (Math.pow(Math.abs(x-cx),2) + Math.pow(Math.abs(y-cy),2));
if (d2 <= 4*r*r) return true;
}
return false;
}
请问是否应该应该改写碰撞逻辑?