Ball kollidiert nicht mit Rand

absoblume

Mitglied
Hallo,

bei meinem Code, soll ein Ball mit dem linken, rechten und oberen Rand kollidieren und die Richtung wechseln.
Bei meinem jetzigen Code wird dies jedoch ignoriert und fliegt einfach durch.

Würde mich über jede Hilfe freuen.

Vielen Dank!

Code:
//Klasse Ball


public void reactOnBorder() {

        double posX = position.getX();
        double dirX = direction.getDx();

        double posY = position.getY();
        double dirY = direction.getDy();

        double width = Constants.SCREEN_WIDTH;

        posX += dirX;
        posY += dirY;

        if(posY < 0){
            dirX = -dirX;
        }
        if(posX < 0){
            dirX = -dirX;
        }
        if(posX < 0 || posX > width){
            dirX = -dirX;
        }
    }

Code:
//Klasse Constants


    public static final Integer SCREEN_WIDTH = 880;

    public static final Integer SCREEN_HEIGHT = 750;

Code:
//Klasse Level

public void run() {   
        game.notifyObservers();
            
        while (true) {
            // if ballWasStarted is true, the ball is moving
            if (ballWasStarted) {

                // Call here the balls method for updating his position on the playground
                ball.updatePosition();
                                        
                // Call here the balls method for reacting on the borders of the playground
                ball.reactOnBorder();
                
                                  
                // Tells the observer to repaint the components on the playground
                game.notifyObservers();   
                    
            }
        }   
    }
 
Hier
Java:
        if(posY < 0){
            dirX = -dirX;
        }
        if(posX < 0){
            dirX = -dirX;
        }
        if(posX < 0 || posX > width){
            dirX = -dirX;
        }
passieren wundersame Dinge:
1. wenn die y-Koordinate < 0 ist, wird die x-Richtung geändert.
2. wenn die x-Koordinate < 0 ist, wird die x-Richtung geändert
3. und dann nochmal: wenn die x-Koordinate < 0 ist, wird die x-Richtung geändert.
 
Ich sehe da zwei Punkte:
a) Wenn die y-Koordinate nicht passt, dann muss natürlich die Richtung in der y-Richtung angepasst werden.
b) Je nach Schrittweite und so: Falls es zu dem Fall kommen kann, dass nach dem Richtungswechsel der Ball bis zum nächsten Check nicht wieder vor der Grenze ist, dann bleibt er außerhalb der Bereiches und wechselt ständig die Richtung. Den Fall würde ich ggf. aktiv verhindern. Hier könnte z.B. helfen, wenn der Ball den gültigen Bereich nicht verlassen kann, also z.B. Wenn x-Koordinate < 0 dann auch x-Koordinate = 0.
 
Hab es hinbekommen. Vielen Dank für die Hilfe. 🙂

Code:
if (position.getX() <= 0) {
            position.setX(0);
            direction.setDx(-1 * direction.getDx());
        }
        if (position.getX() >= Constants.SCREEN_WIDTH - Constants.BALL_DIAMETER) {
            position.setX(Constants.SCREEN_WIDTH - Constants.BALL_DIAMETER);
            direction.setDx(-1 * direction.getDx());
        }
        if (position.getY() <= 0) {
            position.setY(0);
            direction.setDy(-1 * direction.getDy());
        }
        if (position.getY() >= Constants.SCREEN_HEIGHT - Constants.BALL_DIAMETER) {
            position.setY(Constants.SCREEN_HEIGHT - Constants.BALL_DIAMETER);
            direction.setDy(-1 * direction.getDy());
        }
 

Zurück
Oben