0

My question today is relatively simple, is there a way to have collision handling with the Javafx implementation of an SVGPath? EX: if I drop a particle on the screen, and it encounter Any part of the svgPath, it'll signal a collision.

I tried using the regular bounds collision, but it gives me a bounding box that is gigantic for the SVGPath if the path is shaped like an 'L'

The path I'm specifically playing with is:

m 252,12.362183
     c 1.03171,23.632637 -4.57241,55.427587 9,69 65.41611,65.416117 361.05896,43.999997 469,43.999997

Do I have to re-interpolate the line and have an array store a set of (x,y) positions which I constantly check against? that seems rather unwieldy, but I simply can't think of any other (simpler) ways?

The code I tried for the regular bounded collision is as follows:

observableBooleanValue colliding = Bindings.createBooleanBinding(new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                return particle.getBoundsInParent().intersects(path.getBoundsInParent());
            }

        }, particle.boundsInParentProperty(), path.boundsInParentProperty());

        System.out.println("path bounds: " + path.boundsInParentProperty());
        colliding.addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> obs,
                    Boolean oldValue, Boolean newValue) {
                if (newValue) {
                    System.out.println("Colliding");
                } else {
                    System.out.println("Not colliding");
                }
            }
        });

note that a particle is simply a circle with radius 2, and the path is just an SVG path loaded up with the aforementioned svg.

-Will

small edit

So after looking into a few other methods, I got it where it'll get close to be an accurate collision, but it's still about 20% too early (it detects a collision before one actually h appens). almost as if the edges of the path are a bit 'blurry'.

code:

particle.layoutYProperty().addListener(new ChangeListener<Number>(){

            @Override
            public void changed(ObservableValue<? extends Number> ov, Number t, Number t1) {
               Shape intersect = Shape.intersect(path, particle);
                if ((intersect.getBoundsInLocal().getHeight() != -1) && (intersect.getBoundsInLocal().getWidth() != -1)) {
                    System.out.println("Collison!");
                }
            }

        });

I'm really pretty stumped as to why the edge of the svgpath would have such a large buffer on it.

code that creates the path:

/**
     * This function helps to make the path for animating particles
     *
     * @throws IOException
     */
    public void makePaths() throws IOException {
        PathLoader loader = new PathLoader();
        path = new SVGPath();
        path.setContent(loader.getPath(1));
        path.setStroke(Color.AQUA);
        //path.setFill(Color.TRANSPARENT);
        //path.setEffect(boxBlur);


    }

the pathloader simply loads in exactly the path I mentioned up above, tried with and w/o the blur and the transparency, no effect in tightening up the path collision.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
WillBD
  • 1,919
  • 1
  • 18
  • 26
  • possible duplicate of [Checking Collision of Shapes with JavaFX](http://stackoverflow.com/questions/15013913/checking-collision-of-shapes-with-javafx) – jewelsea May 01 '14 at 17:29
  • I thought it was at first, but because the SVG paths are all interpolated lines based on bezier curves, I can't find any way to produce a 'set of bounds' around this type of javafx construct, thus the separate question. – WillBD May 01 '14 at 20:04
  • check edit for information regarding trying to use the 'shape.intersect' method to find the visual intersection on the screen. – WillBD May 01 '14 at 20:37

0 Answers0