3

I'm currently working on intersect of bounds between different shapes with JavaFX. I would like to detect collision of two polygons on their points and not on their bounds (i.e. 2 polygons).

Please, see figure 1: not desired behavior, and figure 2: desired behavior.

Is there any existing algorithm to help me or any library to use? Thanks in advance :)

enter image description here

Here find my solution :

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

Output:

enter image description here

20.0:16.0 intersection detected

It seems to work properly, I will test with Path objects to replace Polygon objets.

PacDroid
  • 541
  • 1
  • 7
  • 22

2 Answers2

1

I was playing with your codes\, and I found this solution, I added mouse event to the polygon2, then I modified the if statement. If not too late, try.

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);
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

If you can use the java.awt.geom package in your application, I suggest you take a look at the Area class and especially the intersect method.

Basically you can do this:

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

You can create arbitrary areas using the GeneralPath class in the same package.

Sebastian
  • 7,729
  • 2
  • 37
  • 69
  • no sorry I would like to use only JavaFX if possible, I'm diging into intersect methods for the moment, but thanks. – PacDroid Nov 27 '13 at 18:09
  • I have a question about this: Is it possible to convert a java.awt.geom Area class into a JavaFx Polygon? I have a swing app that I want to convert to JavaFx. Thanks. – Davem M Dec 19 '14 at 00:23
  • @DavemM That would be a new question, I would say. But the answer is probably: Only if the Area describes a polygon, in which case you would use `getPathIterator`, iterate over the shape and create the FX Polygon. – Sebastian Dec 19 '14 at 08:40