一个正方形由四个角定义,左上角、右上角、左下角、右下角。如果您的更新/碰撞检测足够快,那么以下将起作用。这考虑了只有一个角发生碰撞的事件(如果您的矩形旋转或在 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");
}
}