碰撞检测:被击中的是哪一方? [Java中,Slick2D]

时间:2013-04-27 16:02:59

标签: java collision-detection slick2d bounding-box

我有两个矩形,r1正在移动,而r2是一个固定的瓦片。我需要知道r2(或r1)的哪一侧被击中以确定r1应该去哪个方向。碰撞检测与shape.intersects(otherShape)一起使用,遗憾的是这意味着r1将“沉入”r2内的几个像素。当处理矩形的角落时,这是非常有问题的,因为这会导致每个矩形的两边相交,从而通过检查哪个边相交哪个边无效而找出被击中的边。

我们所知道的:

  • r1和r2的位置和中心位置
  • r1和r2的速度和速度

所以鉴于这些信息,我需要一种能够获得碰撞的算法。

1 个答案:

答案 0 :(得分:0)

正方形由四个角定义,左上角,右上角,左下角,右下角。如果您的更新/冲突检测足够快,那么以下内容将起作用。这考虑了只有一个角碰撞的事件(如果您的矩形在2D中旋转或移动)。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JComponent;
 import javax.swing.JFrame;

public class TestMovingRect extends JComponent {
JFrame frame;
Rectangle2D r1;
Rectangle2D r2;
int speedX, speedY;
int width = 20;
int height = 20;

public static void main(String[] args) {
    new TestMovingRect();
}

public TestMovingRect() {
    r1 = new Rectangle2D.Double(0, 0, width, height);
    r2 = new Rectangle2D.Double(200, 0, width, height);
    speedX = 1;
    frame = new JFrame();
    frame.setSize(500, 500);
    frame.add(this);
    frame.setVisible(true);
    loop();
}

public void tick(){
    System.out.println("Old r1: " + r1.getX() + "," + r1.getY());
    double x = r1.getX();
    double y = r1.getY();
    r1.setRect(x + speedX, y + speedY, 10, 10);
    System.out.println("New r1: " + r1.getX() + "," + r1.getY());

}

private void loop() {
    while (true){
        tick();
        sleep();
        checkIntersect();
        frame.validate();
        frame.repaint();
    }
}

private void checkIntersect() {
    Point2D upperLeft = new Point2D.Double(r1.getX(), r1.getY());
    Point2D upperRight = new Point2D.Double(r1.getX() + r1.getWidth(),
            r1.getY());
    Point2D lowerLeft = new Point2D.Double(r1.getX(), r1.getY()
            + r1.getHeight());
    Point2D lowerRight = new Point2D.Double(r1.getX() + r1.getWidth(),
            r1.getY() + r1.getHeight());

    if (r2.contains(upperRight)){
        System.out.println("UpperRight hit");
        //Do stuff
    }

    if (r2.contains(lowerRight)) {
        System.out.println("lowerRight hit");
        // Do stuff
    }

    if (r2.contains(lowerLeft)) {
        System.out.println("LowerLeft hit");
        // Do stuff
    }

    if (r2.contains(upperLeft)) {
        System.out.println("UpperLeft hit");
        // Do stuff
    }
}

private void sleep() {
    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

@Override
public void paintComponent(Graphics g) {
    // TODO Auto-generated method stub
    super.paintComponents(g);
    g.setColor(Color.blue);
    g.fillRect((int) r1.getX(), (int) r1.getY(), (int) r1.getWidth(),
            (int) r1.getHeight());
    g.setColor(Color.red);
    g.fillRect((int) r2.getX(), (int) r2.getY(), (int) r2.getWidth(),
            (int) r2.getHeight());
}


 }

另一种(更简单的)方法是插入矩形的速度。如果它向右移动并且它相交,那么你知道它必须击中右侧。但是,如果你的矩形在2D中旋转或移动,你将增加复杂性,上面的代码就可以了。

修改:发表评论

if (r2.contains(upperRight)){
        if (speedX * speedX > speedY * speedY) {
            System.out.println("I hit on the right side");
        }

        else if (speedY * speedY > speedX * speedX) {
            System.out.println("I  hit on the topside");
        }

        else if (speedX * speedX == speedY * speedY) {
            System.out
                    .println("I hit on a 45 degree diagonal, side is ambigious, please decide for me");
        }
    }