Maksym Rudenko
Maksym Rudenko

Reputation: 746

Can't find collision with JavaFx shape afrer perspective transform

I have a group of rectangles (or polygons) with dynamicly calculated perspective transform (applied via drag'n'drop). The problem is that coordinates of rectangles are not up to date after this transform and I can't find collision with cars centers. zones

As you can see on image, wrong zones are green because before drag'n'drop rectangles where on top left of imageVeiw.

Here how I'm trying to find collision:

  rectGrpoup.getChildren().forEach(child -> {
        final Predicate<Car> carInZone = car -> {
            final Bounds boundsInLocal = mainController.getImageView().getBoundsInLocal();
            //different image size when detect and display
            Point fixedScalePoint = scaleService.fixScale(new Point(car.getLastCenter()), screenSize, new ScreenSize(boundsInLocal.getHeight(), boundsInLocal.getWidth()));
            //I need help in next line
            return Shape.intersect((Rectangle) child, new Rectangle(fixedScalePoint.getX(), fixedScalePoint.getY(), 10, 10)).getBoundsInLocal().getWidth() != -1;
        };

        if (cars.stream().anyMatch(carInZone)) {
          child.setFill(Color.GREEN);
          } else {
          child.setFill(Color.RED);
        }
    });

Where child is rectangle (or I can refactor for polygon) from group. Child has cordinates before transform, so predicate doesn't work.

Presentation of program work

p.s. I can provide any code if it help

Upvotes: 2

Views: 118

Answers (1)

Maksym Rudenko
Maksym Rudenko

Reputation: 746

PerspectiveTransform in JavaFX in fact extends class Effect and has nothing similar with real transformations (e.x. Rotate).

And the main difference is described in jdoc of the class:

Note that this effect does not adjust the coordinates of input events or any methods that measure containment on a {@code Node}. The results of mouse picking and the containment methods are undefined when a {@code Node} has a {@code PerspectiveTransform} effect in place.

This means that changes on your shape are shown visually, but coordinates are not being updated.

Other solutions:

  • Work with really 3D, e.x. - reach same effect with PerspectiveCamera
  • Get same effect with real transformations like rotation and zoom

Upvotes: 1

Related Questions