gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:24:00 27 4
gpt4 key购买 nike

我正在使用 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);
}

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

最佳答案

碰撞检测工作得很好:

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 的文档,“边界矩形”部分介绍了如何计算各种边界。

关于javafx粗笔画路径(线)与圆之间的碰撞问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30705426/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com