Detecting Collision between two filled Rectangles

时间:2017-05-01 21:22:47

标签: java collision-detection game-physics

I'm trying to make it print out "Game over" when the Green Square(Cuboid) runs over/into the blue(CuboidKiller) one.

GAME class:

package plugin.dev.wristz;

import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JFrame;

public class Game extends JFrame {

    private static final long serialVersionUID = 294623570092988970L;

    public static ArrayList<CuboidKiller> killers;

    public static int h = 1024, w = 768;

    public static Game game;
    public static Graphics graphics, g2;
    public static Image image;

    public static Cuboid cuboid;

    public Game(String title) {
        setTitle(title);
        setSize(1024, 768);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(true);
        setVisible(true);
        setLocationRelativeTo(null);
        addKeyListener(new KeyHandler(cuboid));

        g2 = getGraphics();
        paint(g2);
    }

    public static void main(String[] args) {
        cuboid = new Cuboid();
        Thread cubi = new Thread(cuboid);
        cubi.start();

        killers = new ArrayList<CuboidKiller>();

        CuboidKiller a = new CuboidKiller(new Random().nextInt(h), new Random().nextInt(w), new Random().nextInt(50) + 20);

        killers.add(a);

        game = new Game("Killer Cuboids");
    }

    @Override
    public void paint(Graphics g) {
        image = createImage(getWidth(), getHeight());
        graphics = image.getGraphics();
        paintComponent(graphics);
        g.drawImage(image, 0, 0, this);

    }

    public void paintComponent(Graphics g) {
        checkGameOver();

        cuboid.draw(g);

        for (CuboidKiller killer : killers)
            killer.draw(g);

        repaint();
    }

    public void checkGameOver() {
        for (CuboidKiller killer : killers)
            if (killer.isTouching(cuboid))
                System.out.println("Game over!");

    }

    public int getH() {
        return h;
    }

    public void setH(int wh) {
        h = wh;
    }

    public int getW() {
        return w;
    }

    public void setW(int ww) {
        w = ww;
    }

}

Cuboid class:

package plugin.dev.wristz;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;

@SuppressWarnings("static-access")
public class Cuboid implements Runnable {

    private int x, y, xDirection, zDirection;

    public Cuboid() {
        this.x = 799;
        this.y = 755;
    }

    public void draw(Graphics g) {
        g.setColor(Color.GREEN);

        g.fillRect(x, y, 25, 25);
    }

    public void move() {

        x += xDirection;
        y += zDirection;

        if (x <= 10)
            x = 0 + 10;

        if (y <= 35)
            y = 0 + 35;

        if (x >= 1024 - 35)
            x = 1024 - 35;

        if (y >= 768 - 35)
            y = 768 - 35;

    }

    public void keyPressed(KeyEvent ev) {
        int keyCode = ev.getKeyCode();

        if (keyCode == ev.VK_LEFT) {
            setXDirection(-5);
        }

        if (keyCode == ev.VK_RIGHT) {
            setXDirection(5);
        }

        if (keyCode == ev.VK_UP) {
            setZDirection(-5);
        }

        if (keyCode == ev.VK_DOWN) {
            setZDirection(5);
        }
    }

    public void keyReleased(KeyEvent ev) {
        int keyCode = ev.getKeyCode();

        if (keyCode == ev.VK_LEFT) {
            setXDirection(0);
        }

        if (keyCode == ev.VK_RIGHT) {
            setXDirection(0);
        }

        if (keyCode == ev.VK_UP) {
            setZDirection(0);
        }

        if (keyCode == ev.VK_DOWN) {
            setZDirection(0);
        }

    }

    @Override
    public void run() {
        try {
            while (true) {
                move();
                Thread.sleep(5);

            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getXDirection() {
        return xDirection;
    }

    public void setXDirection(int xDirection) {
        this.xDirection = xDirection;
    }

    public int getZ() {
        return y;
    }

    public void setZ(int z) {
        this.y = z;
    }

    public int getZDirection() {
        return zDirection;
    }

    public void setZDirection(int zDirection) {
        this.zDirection = zDirection;
    }

}

Cuboid Killer:

package plugin.dev.wristz;

import java.awt.Color;
import java.awt.Graphics;
import java.util.HashMap;

public class CuboidKiller {

    private int x, y, radius;

    private HashMap<Integer, Integer> points;

    public CuboidKiller(int x, int y, int radius) {
        this.points = new HashMap<Integer, Integer>();
        setPoints();
        this.x = x;
        this.y = y;
        this.radius = radius;
    }

    public void draw(Graphics g) {
        g.setColor(Color.blue);

        g.fillRect(x, y, radius, radius);
    }

    public void setPoints() {
        this.points.put(x, y);
        this.points.put(x + radius, y);
        this.points.put(x + radius, y - radius);
        this.points.put(x, y - radius);
    }

    public boolean isTouching(Cuboid cuboid) {
        boolean result = true;

        //int a = cuboid.getX(), b = cuboid.getZ();

        result = true;

        return result;
    }

    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public HashMap<Integer, Integer> getPoints() {
        return points;
    }

    public void setPoints(HashMap<Integer, Integer> points) {
        this.points = points;
    }

}

1 个答案:

答案 0 :(得分:0)

嗯,有两种方法。您可以自己编写,也可以只使用Java 8提供的内容。

这个人对如何检测两个矩形之间的碰撞有一个非常好的解释:Java check if two rectangles overlap at any point

但如果我是写作者,我只会让两个类都包含Rectangle个对象(http://docs.oracle.com/javase/8/docs/api/java/awt/Rectangle.html),并且只需调用intersects()提供的Rectangle函数}。 : - )