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

时间:2015-06-08 09:23:41

标签: java javafx collision-detection intersect stroke

我正在使用JavaFx 8库。

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

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

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

1 个答案:

答案 0 :(得分:2)

碰撞检测工作正常:

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的文档,“ Bounding Rectangles ”部分,了解如何计算各种边界。