0

I am making a parking game in JavaFX. I am not satisfied with the current movement of my vehicle, but I don't know how I could make it better. If anyone could point me to the right direction or maybe even provide some code i would be really thankful.

Here is my current code for the movement:

Scene scene = new Scene(layout, 1380,720);
stage.setScene(scene);
layout.setStyle("-fx-background-color: green");

DoubleProperty x = new SimpleDoubleProperty(0);
DoubleProperty y = new SimpleDoubleProperty(-3);
scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
    @Override
    public void handle(KeyEvent keyEvent) {
        if (keyEvent.getCode() == KeyCode.UP) {
            ucar.setTranslateY(ucar.getTranslateY() + y.getValue());
            ucar.setTranslateX(ucar.getTranslateX() + x.getValue());
        } else if (keyEvent.getCode() == KeyCode.DOWN) {
            ucar.setTranslateY(ucar.getTranslateY() - y.getValue());
            ucar.setTranslateX(ucar.getTranslateX() - x.getValue());
        }
        else if(keyEvent.getCode() == KeyCode.LEFT){
            ucar.setRotate(ucar.getRotate()%360 -45);
        }
        else if(keyEvent.getCode() == KeyCode.RIGHT){
            ucar.setRotate(ucar.getRotate()%360 +45);
            double d = x.getValue();
        }
        if(ucar.getRotate() == 0){
            x.setValue(0);
            y.setValue(-3);
        }
        else if(ucar.getRotate() == -90 || ucar.getRotate() == 270) {
            x.setValue(-3);
            y.setValue(0);
        }
        else if(ucar.getRotate() == -180 || ucar.getRotate() == 180){
            x.setValue(0);
            y.setValue(3);
        }
        else if(ucar.getRotate() == 90 || ucar.getRotate() == -270){
            x.setValue(3);
            y.setValue(0);
        }
        else if(ucar.getRotate() == -360 || ucar.getRotate() == 360){
            x.setValue(0);
            y.setValue(-3);
        }
        else if(ucar.getRotate() == -45 || ucar.getRotate() == 315) {
            x.setValue(-2);
            y.setValue(-2);
        }
        else if(ucar.getRotate() == -135 || ucar.getRotate() == 225) {
            x.setValue(-2);
            y.setValue(2);
        }
        else if(ucar.getRotate() == 45 ) {
            x.setValue(2);
            y.setValue(-2);
        }
        else if(ucar.getRotate() == 135) {
            x.setValue(2);
            y.setValue(2);
        }

        for (int i = 0; i < cars.length; i++) {
            if(cars[i] == ucar){
                break;
            }
            if(cars[i].getBoundsInParent().intersects(ucar.getBoundsInParent())){
                loseGame(stage);
                return;
            }
        }
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Your coordinate system is a bit off. Zero degrees is normally increasing X. 90 degrees is increasing Y. Use polar coordinates (angle, distance) to move the car at a consistent pace and convert the polar coordinates to cartesian coordinates for plotting on a scene. Hint: 45 degrees should be X + 1.4 and Y + 1.4 (square root of 3). – Gilbert Le Blanc Dec 27 '21 at 09:55
  • Correction: 45 degrees should be X + 1.73, Y + 1.73 (square root of 3). – Gilbert Le Blanc Dec 27 '21 at 10:03
  • 1
    An alternate approach is shown [here](https://stackoverflow.com/a/69608238/230513). – trashgod Dec 27 '21 at 13:43
  • If you go with the alternate animation timer approach linked by trashgod, a more sophisticated input handler is documented in [How to write a KeyListener for JavaFX](https://stackoverflow.com/questions/29962395/how-to-write-a-keylistener-for-javafx). – jewelsea Dec 28 '21 at 02:20

0 Answers0