在Java中循环到圆形碰撞

时间:2018-01-16 23:27:16

标签: java graphics collision-detection

所以我在圆圈中进行圆圈碰撞。我知道在这个网站上有许多类似我的问题,但我的问题在所有这些问题上都是独一无二的。当我运行我的代码时,圆圈每4次相互碰撞一次。含义:它们将经历3次而不会相互碰撞,但有一次它们会发生碰撞。非常感谢任何帮助。

public class Ball {
float x, y; // coordinates of ball rectangle
float xo, yo;
float vx = 2, vy = 2; // coordinates of velocity vector
Color colour; // ball colour
float d; // diameter of the ball or sizes of ball rectangle
Ellipse2D.Float circle;

// overloaded constructor
Ball(int x, int y, int vx, int vy, int d, Color colour) {
    this.x = x;
    this.y = y;
    this.d = d;
    xo = x;
    yo = y;
    this.setColour(colour);
    this.setVelocity(vx, vy);
    circle = new Ellipse2D.Float(x, y, d, d);
}

public void setColour(Color colour) {
    this.colour = colour;
}

public void setVelocity(int vx, int vy) {
    this.vx = vx;
    this.vy = vy;
}

public void show(Graphics g) {
    ((Graphics2D) g).setPaint(colour);
    circle.setFrame(x, y, d, d);
    ((Graphics2D) g).fill(circle);
    xo = x;
    yo = y;
}

public void hide(Graphics g) {
    Color c = ((Graphics2D) g).getBackground();
    ((Graphics2D) g).setPaint(c);
    circle.setFrame(xo, yo, d, d);
    ((Graphics2D) g).fill(circle);
}

public void setPosition(float x, float y) {
    this.x = x;
    this.y = y;
}

public void move(int a, int b, int xh, int yh) {
    if (vy > 0) {
        if (y + d + vy - yh - b > 0) {
            y = yh + b - d;
            vy = -vy;
        } else
            y += vy;
    } else {
        if (y + vy <= b) {
            y = b;
            vy = -vy;
        } else
            y += vy;
    }
    if (vx > 0) {
        if (x + d + vx - xh - a > 0) {
            x = xh + a - d;
            vx = -vx;
        } else
            x += vx;
    } else {
        if (x + vx <= a) {
            x = a;
            vx = -vx;
        } else
            x += vx;
    }
}

碰撞探测器在下面的课程中

public class Game extends JFrame {
int ah, bh, xh, yh; // parameters of the rectangle frame
Color[] ColorAr = { Color.red, Color.blue, Color.pink, Color.green,
        Color.yellow, Color.magenta, Color.black, Color.orange, Color.gray,
        Color.cyan };
Ball b[];
int quantity = 4;

public void paint(Graphics g) {
    int i;
    ((Graphics2D) g).setPaint(Color.black);
    ((Graphics2D) g).drawRect(ah, bh, xh, yh);
    for (i = quantity - 1; i >= 0; i--) {
        b[i].hide(g);
    }
    for (i = 0; i < quantity; i++) {
        b[i].show(g);
    }
}

public void prepare() {
    int i;
    ah = 20;
    bh = 40;
    xh = 400;
    yh = 400;

    b = new Ball[quantity];

    for (i = 0; i < quantity; i++) {
        b[i] = new Ball((int) (Math.random() * (300 - 1 + 1)) + 1, 100, 1,
                1, 26, ColorAr[(int) (Math.random() * 9)]);
    }
}

public void collision() {
    int radius = 13;
    int distance = 2 * radius;
    if (b[1].x + distance == b[0].x && b[1].y == b[0].y
            || b[1].x - distance == b[0].x && b[1].y == b[0].y) {
        b[1].vx = -b[1].vx;
        b[0].vx = -b[0].vx;
    }
}

public void run() {
    int i;
    while (true) {
        for (i = 0; i < quantity; i++)
            b[i].move(ah, bh, xh, yh);// move balls
        collision();
        for (int j = 0; j < 10000000; j++)
            ; // delay;
        // collision();
        repaint();
    }
}

public static void main(String args[]) {
    Game frame = new Game();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // frame.setBackground(Color.white);
    frame.setSize(450, 470);
    frame.prepare();
    frame.setVisible(true);
    frame.run();
}

}

1 个答案:

答案 0 :(得分:1)

对于两个圈子: 首先计算长度之和:radius_1 + radius_2(第一个和第二个圆圈)。 如果计算中心之间(虚)线的长度,则可以检测到两个圆的碰撞。如果那是length <= radius_1 + radius_2,则两个圆圈相撞。

相关问题