JavaFX intersection of shapes and polygons


Question

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.

1
3
11/27/2013 6:20:03 PM

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);
    }
}
1
8/28/2016 6:14:30 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon