如何制作旋转碰撞检测盒

如何制作旋转碰撞检测盒

本文介绍了如何制作旋转碰撞检测盒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试旋转检测碰撞的命中盒。我想让一个非旋转的矩形_1从左侧移动到右侧,并让一个旋转的矩形_2从右侧移动到左侧,并且我希望旋转的点击框能够检测到碰撞,而无论其方向如何。但是,仅当矩形_1与未旋转的矩形_2碰撞时才检测到碰撞。如何使旋转的命中盒检测到碰撞?以下是代码:

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:

主程序:

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;
}

rectangular_1类:

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;
}

rectangular_2类:

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;
}


推荐答案

当和

只有当从拐角点到两个相对边的距离时,拐角才在另一个矩形内。矩形的长度都小于矩形边缘的长度。

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.

对于计算机图形学,有一种标准算法可以计算2D和3D空间中的距离。 Google,应该这样做。

For computer graphics there is a standard algorithm to compute the distanced in 2D and 3D space. Google it and that should be done.

这篇关于如何制作旋转碰撞检测盒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 02:21