如何知道两个图像之间的冲突javafx

如何知道两个图像之间的冲突javafx,javafx,collision-detection,Javafx,Collision Detection,如何在javaFx上获得两个图像的碰撞或边界相交?我移动一个图像,但如果我触摸第二个图像,我想触发一些东西 gc.drawImage( img2, 0, 400, 950, 100 ); gc.drawImage( img3, 200, 200, 150, 50 ); if (event.getCode() == KeyCode.RIGHT ){ if(img2.intersects(img3.getBoundsInLocal())){

如何在javaFx上获得两个图像的碰撞或边界相交?我移动一个图像,但如果我触摸第二个图像,我想触发一些东西

gc.drawImage( img2, 0, 400, 950, 100 );
    gc.drawImage( img3, 200, 200, 150, 50 );

if (event.getCode() == KeyCode.RIGHT ){
            if(img2.intersects(img3.getBoundsInLocal())){
            doSomething();
        }
}

此代码不起作用

您可以为图像创建一个包装器,例如一个Sprite类,您可以使用该包装器为其提供x,y坐标。可以借助矩形2D形状获得交点

import javafx.geometry.Rectangle2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;

public class Sprite {

    private Image image;
    private double positionX;
    private double positionY;
    private double width;
    private double height;

    public Sprite(Image image) {
        this.image = image;
        width = image.getWidth();
        height = image.getHeight();
        positionX = 0;
        positionY = 0;
    }

    public void setPosition(double x, double y) {
        positionX = x;
        positionY = y;
    }

    public void render(GraphicsContext gc) {
        gc.drawImage(image, positionX, positionY);
    }

    public Rectangle2D getBoundary() {
        return new Rectangle2D(positionX, positionY, width, height);
    }

    public boolean intersects(Sprite spr) {
        return spr.getBoundary().intersects(this.getBoundary());
    }
}