具有粗笔划和圆的路径(线)之间的 javafx 碰撞问题

javafx collision issue between path(line) with thick stroke and circle

我正在使用 JavaFx 8 库。

我的任务很简单:我想检查圆是否与周围有粗笔画的路径发生碰撞。问题是 Path.intersect() 和 Shape.intersect() 这两个函数都忽略了 path/line.

周围的笔画
Path tempPath = new Path(player.getPath().getElements());
//player.getDot() is Circle
if(tempPath.intersects(player.getDot().getBoundsInParent())){
   Shape intersect = Shape.intersect(tempPath, player.getDot());
   if(intersect.getBoundsInLocal().getWidth() != -1){
      System.out.println("Path Collision occurred"); 
   }
}

我的路径由许多 LineTo 对象组成。 格式是这样的:

/** Creates path and player dot */
private void createPath() {
    this.path = new Path();
    this.path.setStrokeWidth(20);
    this.path.setStroke(Color.RED);
    this.path.setStrokeLineCap(StrokeLineCap.ROUND);
    this.path.setStrokeLineJoin(StrokeLineJoin.ROUND);

    this.dot = new Circle(10, Color.BLUE);
    this.dot.setOpacity(1);
}

如何实现成功的碰撞检测?

碰撞检测工作正常:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.shape.ClosePath;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.Shape;
import javafx.scene.shape.StrokeLineCap;
import javafx.scene.shape.StrokeLineJoin;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {

        BorderPane root = new BorderPane();

        Circle circle = new Circle(100, 100, 30);
        Path path = new Path();

        double x = 144;

        path.getElements().add(new MoveTo(x, 140));
        path.getElements().add(new LineTo(500, 100));
        path.getElements().add(new ClosePath());

        path.setStrokeWidth(60);
        path.setStrokeLineCap(StrokeLineCap.ROUND);
        path.setStrokeLineJoin(StrokeLineJoin.ROUND);

        Shape shape = Shape.intersect(circle, path);

        boolean intersects = shape.getBoundsInLocal().getWidth() != -1;

        System.out.println("Intersects: " + intersects);

        Pane pane = new Pane();
        pane.getChildren().addAll(circle, path);
        root.setCenter(pane);

        Scene scene = new Scene(root, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}

将 x = 144 切换为 x = 145 进行测试。控制台显示是否有路口

你遇到的问题是你在比较不同的界限。

请参阅 Node 的文档,“边界矩形”部分了解如何计算各种边界。