Search code examples
javajavafxgraphicsgraphics2d

Graphics in JavaFX game : Help me solve this


I am currently making a simple 2d game like flappy bird. For those who are unfamiliar with this, the game is a side-scroller where the player controls a object, attempting to fly between rows of neon pipes without hitting them. In this Example You can see that the UFO(initFrog) is not quite touching the neon pipe. However, because the glow effect on the neon pipe is a part of the .png file, the UFO recognizes it as a point of intersection. There is a timer which is set to stop when the two intersect (view last block of code below).

So how can I make the timer stops when the UFO hits the neon itself, an not just the neons glow?

    private Node initFrog() {
    ImageView falc = new ImageView();
    falc.setImage(milFalc);
    falc.setTranslateY(300-39);
    falc.setTranslateX(240);
    falc.setScaleX(.3);
    falc.setScaleY(.3);
    return falc;
    }

private Node ships() {
    int haut = (int)(Math.random()*600);
    ImageView sheep = new ImageView();
    sheep.setImage(Orbs);
    sheep.setTranslateY(haut-500);
    sheep.setTranslateX(800);
    sheep.setScaleX(.017);
    sheep.setScaleY(.017);
    root.getChildren().add(sheep);
    return sheep;
    }

private Node initNeon() {
    int haut = (int)(Math.random()*260);
    ImageView neona = new ImageView();
    neona.setImage(neon1);
    neona.setTranslateY(haut-200);
    neona.setTranslateX(800);
    neona.setRotate(90);
    neona.setScaleX(1.);
    neona.setScaleY(.8);
    root.getChildren().add(neona);
    return neona;
    }


 private Node SpawnzCar() {  
   int hauts = (int)(Math.random() *280);
    ImageView neona = new ImageView();
    neona.setImage(neon2);
    neona.setTranslateY(hauts+520);
    neona.setTranslateX(800);
    neona.setRotate(90);
    neona.setScaleX(1.);
    neona.setScaleY(.8);
    root.getChildren().add(neona);
    return neona;
 }  private void onUpdate() {

for (Node car : cars) 
    car.setTranslateX(car.getTranslateX() -  11); 
 if (Math.random() <= 0.07 ) {
    cars.add(SpawnzCar());
   // cars.add(ships());
    cars.add(initNeon());}
    checkState(); 
    }

private void checkState() {
    for (Node car : cars) {
     if (car.getBoundsInParent().intersects(frog.getBoundsInParent())) {
        frog.setTranslateX(frog.getTranslateX());
        timer.stop();
        frog.setTranslateY(frog.getTranslateY());
        return;
        } }

Solution

  • Instead of testing if the png files intersect each other, you can manually check a modified hit box using coordinates. I don't see where you are storing the values for the coordinates of the ufo and the pipes so I'll be using pseudocode for the example.

    public boolean isCollision(){
        if(ufo.getX() + ufo.width() > pipe.getX() + pipe.getOffSetWidth() && ufo.getX() < pipe.getX() + pipe.width() - pipe.getOffSetWidth()){
            if(\\check for y value of height, this can be different depending on your way of storing pipes){
                return true;
            }
        } 
    }
    

    You want your offSetWidth() value to be the length in pixels between the edge of your pipe image and the portion of the pipe you want the collision to occur at.