Skip to content

Instantly share code, notes, and snippets.

@nhooyr
Created January 21, 2018 10:23
Show Gist options
  • Save nhooyr/8332f0fd4127c16c4cb1785b991495c9 to your computer and use it in GitHub Desktop.
Save nhooyr/8332f0fd4127c16c4cb1785b991495c9 to your computer and use it in GitHub Desktop.
import javafx.application.Application;
import javafx.geometry.Bounds;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class CollisionDetection extends Application {
@Override
public void start(Stage primaryStage) {
Label label = new Label();
Pane paneA = new Pane();
Pane paneB = new Pane();
Rectangle objectA = new Rectangle(50, 30);
objectA.setFill(Paint.valueOf("red"));
objectA.setX(100);
objectA.setY(100);
objectA.setRotate(10);
objectA.setSmooth(true);
Rectangle objectB = new Rectangle(80, 40);
objectB.setFill(Paint.valueOf("blue"));
objectB.setRotate(-10);
objectB.setX(115);
objectB.setY(50);
paneB.getChildren().add(objectB);
paneA.getChildren().addAll(objectA, paneB);
Bounds objA = objectA.localToScene(objectA.getBoundsInLocal());
Bounds objB = objectB.localToScene(objectB.getBoundsInLocal());
if (objA.intersects(objB)) {
label.setText("ObjectA intersects ObjectB");
} else {
label.setText("ObjectA does not intersect ObjectB");
}
BorderPane root = new BorderPane(paneA);
root.setBottom(label);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Collision Detection");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment