JavaFX Kollisionserkennung

Cromewell

Top Contributor
Hallo 🙂
Ich programmiere gerade ein kleines Game.
Aber ich weiß nicht, wie ich die Kollision passend handeln soll.

In meinem loop checkt er jeden Tick, ob es eine Kollision von Player und einem Soliden Objekt gibt:

Java:
for (Shape s: collisionObjects) {

    boolean collision = false;
    Shape intersect = Shape.intersect(s, ply.getPlayer());
    if(intersect.getBoundsInLocal().getWidth() != -1){
        collision = true;
    }
    if(collision){
        ply.setVelY(0);
        if(s.getBoundsInLocal().getMaxY() > ply.getY()+50){
            //stößt von oben auf einen Block -> steht also drauf
            if(ply.isJumping()){
                ply.setJumping(false);
            }
            ply.setY(s.getBoundsInLocal().getMinY()-50-0.1);

        } else if(s.getBoundsInLocal().getMaxY() > ply.getY()){
            ply.setY(s.getBoundsInLocal().getMaxY()+0.0000001);
            //springt von unten an einen Block
        }
    }

}

Ideen ?
 
Für die Interessierten - es klappt nun ^^
Java:
for (Shape s: collisionObjects) {

    boolean collision = false;
    Shape intersect = Shape.intersect(s, ply.getPlayer());
    if(intersect.getBoundsInLocal().getWidth() >= 0){
        collision = true;
    }
    if(collision){
        ply.setVelY(0);
        if (s.getBoundsInLocal().getMaxY() > ply.getPlayer().getBoundsInLocal().getMinY() && s.getBoundsInLocal().getMaxY() < ply.getPlayer().getBoundsInLocal().getMaxY()) {
            //ply hits block with its top side
            ply.setY(s.getBoundsInLocal().getMaxY() + 0.1);
            System.out.println("t");

        }
        if (s.getBoundsInLocal().getMinY() < ply.getPlayer().getBoundsInLocal().getMaxY() && s.getBoundsInLocal().getMinY() > ply.getPlayer().getBoundsInLocal().getMinY()) {
           //ply hits block with its bottom side
            ply.setY(s.getBoundsInLocal().getMinY()-50.1);
            if(ply.isJumping()){
                ply.setJumping(false);
            }
            System.out.println("b");
        }
        if(!ply.isJumping()) {
            if (s.getBoundsInLocal().getMinY() <= ply.getPlayer().getBoundsInLocal().getMinY()) {
                if (s.getBoundsInLocal().getMaxX() > ply.getPlayer().getBoundsInLocal().getMinX() && s.getBoundsInLocal().getMaxX() < ply.getPlayer().getBoundsInLocal().getMaxX()) {

                    ply.setX(s.getBoundsInLocal().getMaxX() + 0.1);
                    System.out.println("L");

                } else if (s.getBoundsInLocal().getMinX() < ply.getPlayer().getBoundsInLocal().getMaxX() && s.getBoundsInLocal().getMinX() > ply.getPlayer().getBoundsInLocal().getMinX()) {

                    ply.setX(s.getBoundsInLocal().getMinX() - 50.1);
                    System.out.println("R");

                }
            }
        }


    }
}

PS: in den meisten Fällen 😀
 
So, ich habe es jetzt vollständig gelöst xD
Java:
if(collision){
    ply.setVelY(0);
    ply.setVelX(0);
    if(intersect.getBoundsInLocal().getHeight() > intersect.getBoundsInLocal().getWidth()){
        //left/right
        if(ply.getPlayer().getBoundsInLocal().getMinX() < s.getBoundsInLocal().getMaxX() && ply.getPlayer().getBoundsInLocal().getMaxX() > s.getBoundsInLocal().getMaxX()){
            //player hits the left side of a block
            ply.setX(ply.getPlayer().getX()+intersect.getBoundsInLocal().getWidth());
            System.out.println("left");
        }else{
            //player hits the right side of a block
            System.out.println("right");
            ply.setX(ply.getPlayer().getX()-intersect.getBoundsInLocal().getWidth());
        }
    }else if(intersect.getBoundsInLocal().getWidth() > intersect.getBoundsInLocal().getHeight()){
        //top/bottom
        if(ply.getPlayer().getBoundsInLocal().getMinY() < s.getBoundsInLocal().getMinY() && ply.getPlayer().getBoundsInLocal().getMaxY() > s.getBoundsInLocal().getMinY()){
            //player is on the top
            ply.setY(ply.getPlayer().getY()-intersect.getBoundsInLocal().getHeight());
            ply.setJumping(false);
        }else{
            //player hits the bottom of a block
            ply.setY(ply.getPlayer().getY()+intersect.getBoundsInLocal().getHeight());
        }
    }
}
 

Zurück
Oben