2013-11-27 113 views
2

我目前正在研究JavaFX与不同形状之间的边界相交。 我想检测两个多边形在其点上的碰撞,而不是它们的边界(即2个多边形)。JavaFX十字形状与多边形

请参阅图1:不需要的行为,图2:所需的行为。

是否有任何现有的算法来帮助我或任何图书馆使用? 感谢提前:)

enter image description here

在这里找到我的解决办法:

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.layout.Pane; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Polygon; 
import javafx.scene.shape.Shape; 
import javafx.stage.Stage; 

public class Tester extends Application { 

@Override 
public void start(Stage stage) throws Exception { 
    Pane root = new Pane(); 
    root.setStyle("-fx-background-color:cyan;"); 
    Polygon p1 = new Polygon(); 
    p1.getPoints().addAll(new Double[]{ 
      50.,50., 
      50.,100., 
      60.,100., 
      60.,80., 
      80.,70., 
      80.,100., 
      100.,100., 
      100.,50. 
    }); 
    p1.setFill(Color.GREEN); 

    Polygon p2 = new Polygon(); 
    p2.getPoints().addAll(new Double[]{ 
      65.,100., 
      65.,90., 
      75.,80., 
      100.,100. 
    }); 
    p2.setFill(Color.RED); 

    root.getChildren().addAll(p1,p2); 

    stage.setScene(new Scene(root)); 
    stage.show(); 

    Shape inter = Shape.intersect(p1, p2); 
    root.getChildren().add(inter); 

    System.out.println(inter.getLayoutBounds().getWidth() + ":" + inter.getLayoutBounds().getHeight()); 
    if(inter.getLayoutBounds().getHeight()<=0 || inter.getLayoutBounds().getWidth()<=0) { 
     System.out.println("No intersection"); 
    } 
    else { 
     System.out.println("intersection detected"); 
    } 
} 

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

输出:

enter image description here

20.0:16.0 intersection detected

它似乎正常工作,我将用Path对象进行测试以替换Polygon对象。

+0

你可能要检查,如果你的一个多边形的边的交点是在其他的边界。使用:'mypolygon1.getBoundsInLocal()。contains(myPolygon2.getBoundsInLocal())' – Aspirant

+0

[检查形状与JavaFX的碰撞]的可能的重复(http://stackoverflow.com/questions/15013913/checking-collision-of-shapes -with-javafx) – jewelsea

+0

@user我测试过,但它是图1的情况。 – PacDroid

回答

0

如果你可以在你的应用程序中使用java.awt.geom包,我建议你看看Area类,特别是intersect方法。

基本上,你可以这样做:

area1.intersect(area2); 
boolean areasintersect = !area1.isEmpty(); 

您可以使用该GeneralPath类在同一个包的任意区域。

+0

没有抱歉我只想使用JavaFX,如果可能的话,我正在挖掘相交方法,但谢谢。 – PacDroid

+0

我对此有疑问:是否可以将java.awt.geom Area类转换为JavaFx Polygon?我有一个我想转换为JavaFx的swing应用程序。谢谢。 –

+0

@DavemM这将是一个新问题,我会说。但是答案可能是:只有Area描述了一个多边形,在这种情况下,你将使用'getPathIterator',迭代该形状并创建FX多边形。 – Sebastian

1

我正在玩你的代码\,我发现这个解决方案,我把鼠标事件添加到polygon2,然后我修改了if语句。如果不是太晚,试试。

public class Tester extends Application { 

    Shape inter; 
    Point2D pc; 
    double initX, initY, mx, my; 

    @Override 
    public void start(Stage stage) throws Exception { 
     Pane root = new Pane(); 
     root.setStyle("-fx-background-color:cyan;"); 
     final Polygon p2 = new Polygon(); 
     final Polygon p1 = new Polygon(); 
     p1.getPoints().addAll(new Double[]{ 
      50., 50., 
      50., 100., 
      60., 100., 
      60., 55., 
      80., 70., 
      80., 100., 
      100., 100., 
      100., 50. 
     }); 
     p1.setFill(Color.GREEN); 
     p2.getPoints().addAll(new Double[]{ 
      65., 100., 
      65., 45., 
      75., 80., 
      100., 100. 
     }); 

     p2.setFill(Color.RED); 

     p1.setTranslateX(400); 
     p1.setTranslateY(250); 

     p2.setOnMousePressed(new EventHandler<MouseEvent>() { 
      public void handle(MouseEvent me) { 
       initX = p2.getTranslateX(); 
       initY = p2.getTranslateX(); 
       pc = new Point2D(me.getSceneX(), me.getSceneY()); 
      } 
     }); 

     p2.setOnMouseDragged(new EventHandler<MouseEvent>() { 
      public void handle(MouseEvent me) { 
       double dragX = me.getSceneX() - pc.getX(); 
       double dragY = me.getSceneY() - pc.getY(); 
       double newXPosition = initX + dragX; 
       double newYPosition = initY + dragY; 
       p2.setTranslateX(newXPosition); 
       p2.setTranslateY(newYPosition); 
       System.out.println("no intersection"); 
       if (Shape.intersect(p2, p1).getBoundsInLocal().isEmpty() == false) { 
        p1.setTranslateX(p2.getTranslateX() + mx); 
        p1.setTranslateY(p2.getTranslateY() + my); 
        System.out.println("colision"); 

       } else { 
        mx = p1.getTranslateX() - p2.getTranslateX(); 
        my = p1.getTranslateY() - p2.getTranslateY(); 
       } 

      } 
     }); 
     root.getChildren().addAll(p1, p2); 

     final Scene scene = new Scene(root, 1200, 850); 
     stage.setScene(scene); 
     stage.show(); 
    } 

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