title: Checking Collision of Shapes with JavaFX
body: <p>I am trying to do some collision detection. For this test I am using simple rectangular <code>Shape</code>, and checking their <code>Bound</code>, to figure if they are colliding. Although the detection does not work as expected. I have tried using different ways to move the object(relocate, setLayoutX,Y) and also different bound checks (boundsInLocal,boundsInParrent etc) but I still cannot get this to work. As you can see the detection works only for one object, even when you have three objects only one detects collision. This is some working code demonstrating the problem:</p>
  
  <pre><code>import javafx.application.Application;
  import javafx.event.EventHandler;
  import javafx.scene.Cursor;
  import javafx.scene.Group;
  import javafx.scene.Scene;
  import javafx.scene.input.MouseEvent;
  import javafx.scene.paint.Color;
  import javafx.scene.shape.Rectangle;
  import javafx.stage.Stage;
  
  import java.util.ArrayList;
  
  
  public class CollisionTester extends Application {
  
  
      private ArrayList&lt;Rectangle&gt; rectangleArrayList;
  
      public static void main(String[] args) {
          launch(args);
      }
  
      public void start(Stage primaryStage) {
          primaryStage.setTitle("The test");
          Group root = new Group();
          Scene scene = new Scene(root, 400, 400);
  
          rectangleArrayList = new ArrayList&lt;Rectangle&gt;();
          rectangleArrayList.add(new Rectangle(30.0, 30.0, Color.GREEN));
          rectangleArrayList.add(new Rectangle(30.0, 30.0, Color.RED));
          rectangleArrayList.add(new Rectangle(30.0, 30.0, Color.CYAN));
          for(Rectangle block : rectangleArrayList){
              setDragListeners(block);
          }
          root.getChildren().addAll(rectangleArrayList);
          primaryStage.setScene(scene);
          primaryStage.show();
      }
  
      public void setDragListeners(final Rectangle block) {
          final Delta dragDelta = new Delta();
  
          block.setOnMousePressed(new EventHandler&lt;MouseEvent&gt;() {
              @Override
              public void handle(MouseEvent mouseEvent) {
                  // record a delta distance for the drag and drop operation.
                  dragDelta.x = block.getTranslateX() - mouseEvent.getSceneX();
                  dragDelta.y = block.getTranslateY() - mouseEvent.getSceneY();
                  block.setCursor(Cursor.NONE);
              }
          });
          block.setOnMouseReleased(new EventHandler&lt;MouseEvent&gt;() {
              @Override
              public void handle(MouseEvent mouseEvent) {
                  block.setCursor(Cursor.HAND);
              }
          });
          block.setOnMouseDragged(new EventHandler&lt;MouseEvent&gt;() {
              @Override
              public void handle(MouseEvent mouseEvent) {
  
                  block.setTranslateX(mouseEvent.getSceneX() + dragDelta.x);
                  block.setTranslateY(mouseEvent.getSceneY() + dragDelta.y);
                  checkBounds(block);
  
              }
          });
      }
  
      private void checkBounds(Rectangle block) {
          for (Rectangle static_bloc : rectangleArrayList)
              if (static_bloc != block) {
                  if (block.getBoundsInParent().intersects(static_bloc.getBoundsInParent())) {
                      block.setFill(Color.BLUE);        //collision
                  } else {
                      block.setFill(Color.GREEN);    //no collision
                  }
              } else {
                  block.setFill(Color.GREEN);    //no collision -same block
              }
      }
  
      class Delta {
          double x, y;
      }
  }
  </code></pre>