问题
I am trying to make a rotate a hitbox that detects collision. I want a non-rotating rectangle_1 moving from the left to the right and a rotating rectangle_2 moving from the right to the left, and I want the rotating hitbox to detect the collision regardless of its orientation. However, collision is detected only when rectangle_1 collides with the non-rotated rectangle_2. How can I make a rotating hit box detect collision? Here are the codes:
The main program:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Timer;
public class Rectangles extends JPanel implements MouseListener{
public static final int SCREEN_WIDTH = 400;
public static final int SCREEN_HEIGHT = 400;
private static final int TIMER_RATE = 20;
public static void main(String[] args) throws Exception {
Rectangles game = new Rectangles();
game.setup();
}
public void setup() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JScrollPane(this));
f.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
f.setLocation(200, 200);
f.setVisible(true);
this.addMouseListener(this);
this.setFocusable(true);
}
public Rectangles() {
rectangle_1 = new Rectangle_1();
rectangle_2 = new Rectangle_2();
gameover = false;
play();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.white);
g.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
g.setColor(Color.white);
draw((Graphics2D) g);
}
public void draw(Graphics2D g) {
if (gameover) {
rectangle_1.drawRectangle(g);
rectangle_2.drawRectangle(g);
return;
}
rectangle_1.drawRectangle(g);
rectangle_2.drawRectangle(g);
rectangle_1.move();
rectangle_2.move(1);
rectangle_2.rotateRectangle(g);
if (rectangle_2.collides(rectangle_1)) {
gameover = true;
}
}
public void play() {
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
repaint();
}
};
Timer timer = new Timer(TIMER_RATE, listener);
timer.start();
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
mouseAction(e.getX(), e.getY(), e.getButton());
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
/**
* Use this method to handle all mouse input. The main action here is to have the bird flap whenever the mouse is clicked.
*/
public void mouseAction(int x, int y, int button) {
if (button == 1) {
rectangle_1.flap();
}
}
private Rectangle_1 rectangle_1;
private Rectangle_2 rectangle_2;
private boolean gameover;
private int ticks = 0;
}
The rectangle_1 class:
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
public class Rectangle_1 {
public Rectangle_1() {
x = 0;
y = 240;
rectangle = new Rectangle((int) x, (int) y, 25, 25);
}
public void drawRectangle(Graphics2D g) {
g.setColor(Color.blue);
g.draw(rectangle);
}
public void move() {
x = x + 1;
rectangle.setLocation((int) x, (int) y);
}
public void flap() {
z = 10;
rectangle.setLocation((int) x, (int) y);
}
public boolean collides(Rectangle r) {
return r.intersects(rectangle);
}
public Rectangle getRectangle() {
return rectangle;
}
private double x;
private double y;
private double z = 0;
private Rectangle rectangle;
}
The rectangle_2 class:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.*;
import java.awt.Rectangle;
public class Rectangle_2 {
public Rectangle_2() {
x = 375;
y = 200;
hitBox_1 = new Rectangle((int) x + 25, (int) y, 25, 100);
}
public void drawRectangle(Graphics2D g) {
g.setColor(Color.red);
g.draw(hitBox_1);
}
public void rotateRectangle(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
AffineTransform at = g2.getTransform();
AffineTransform saveTransform = g2.getTransform();
angle += 0.01;
at.rotate(angle, hitBox_1.getX() + hitBox_1.getWidth() / 2, hitBox_1.getY() + hitBox_1.getHeight() / 2);
g2.setTransform(at);
g2.setColor(Color.red);
g2.draw(hitBox_1);
g2.setTransform(saveTransform);
}
public void move(double dx) {
x = x - dx;
hitBox_1.setLocation((int) x, (int) y);
}
public boolean collides(Rectangle_1 r) {
return r.collides(hitBox_1);
}
@Override
public String toString() {
return "Pipe [x = " + x + ", y = " + y + ", hitBox_1 = " + hitBox_1 + "]";
}
public Rectangle getHitBox_1() {
return hitBox_1;
}
public Rectangle getHitBox_2() {
return hitBox_1;
}
public double getX() {
return x;
}
private double x;
private double y;
private double angle = 0;
private Rectangle hitBox_1;
}
回答1:
2 rectangles are collided when and only when at least 1 of them has a corner point inside another rectangle.
A corner is inside another rectangle when and only when the distances from the corner point to 2 opposite sides of the rectangle both less than the length of the rectangle edge.
For computer graphics there is a standard algorithm to compute the distanced in 2D and 3D space. Google it and that should be done.
回答2:
you never rotated any of your rectangles - therefore you don't detect a collision on a (not rotated) rectangle)....
when you 'rotate' your rectangle with rotateRectangle
you just draw a rotated rectangle but never rotate the rectangle itself...
what do you have to do? you have to replace the Rectangle2 with something that can be rotated like a java.awt.polygon
//your source points
int[] xs = new int[]{0, 10, 10, 0};
int[] ys = new int[]{0, 0, 10, 10};
//create a polygon with these source points
Polygon p = new Polygon(xs, ys, 4);
//again: your source Points as double[]
double[] src = new double[]{
xs[0], ys[0],
xs[1], ys[1],
xs[2], ys[2],
xs[3], ys[3]};
//and destination points for rotating them
double[] dst = new double[8];
//to rotate them you need an affirm transformation (rotating around your center 5/5)
AffineTransform t = AffineTransform.getRotateInstance(0.1, 5, 5);
t.transform(src, 0, dst, 0, 4);
//and revert them back into int[]
int[] xTrans = new int[]{(int)dst[0], (int)dst[2],(int)dst[4],(int)dst[6]};
int[] yTrans = new int[]{(int)dst[1], (int)dst[3],(int)dst[5],(int)dst[7]};
//this would be your rotated polygon
Polygon pTrans = new Polygon(xTrans, yTrans, 4);
you can use the methode java.awt.Polygon.intersects(Rectangle r) to check if you intersect with your rectangle
if you want the very easiest way you can simple create a rotated instance from your rectangle:
AffineTransform t = AffineTransform.getRotateInstance(0.1, 5, 5);
Shape s = t.createTransformedShape(rectangle);
来源:https://stackoverflow.com/questions/32069321/how-can-i-make-rotating-hitboxes-that-detect-collision