Kollision funktioniert unten aber nicht oben

DasDirt

Mitglied
Hi der Titel sagt alles mein Problem ist es das meine Kollision unten aber nicht oben funktioniert.

Hier meine Kollisions Klasse:
Java:
public class CollisionBox {
    private double x, y, width, height;

    public CollisionBox(double x, double y, double width, double height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    public boolean hasCollided(double x, double y) {
        //Check if the given coordinates are in the area of the collision box
        return x > this.x && y > this.y && x < this.width && y < this.height;
    }

    public void updateCollisionBox(double x, double y, double width, double height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    public boolean checkCollisionWithAnotherCollisionBox(CollisionBox collisionBox){
        return collisionBox.getX() > this.getX() && collisionBox.getY() > this.getY() && collisionBox.getWidth() < this.width && collisionBox.getHeight() < this.height;
    }

    public void renderCollisionBox() {
        RenderUtil.drawRect(x, y, width, height, Color.RED.getRGB());
    }

    /*Setter & Getter*/

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }
}

und hier der Code, wo ich die box erstelle, update und abfrage:

Java:
private CollisionBox upper;

this.upper = new CollisionBox(this.x, this.level.getHeight(), this.x + this.width, this.y + this.height + Main.mainInstance.getSpace());

this.upper.updateCollisionBox(this.x, this.level.getHeight(), this.x + this.width, this.y + this.height + Main.mainInstance.getSpace());

this.pipe.getUpper().checkCollisionWithAnotherCollisionBox(this.bird.getCollisionBox())//ist ein boolean in einer if abfrage

das ganze soll ein Flappy Bird werden ich weiß nicht warum die Kollisions box verbuggt ist da sie unten funktioniert
so habe ich es unten gemacht:
Java:
private CollisionBox under;

this.under = new CollisionBox(this.x, this.y, this.x + this.width, this.y + this.height);

this.under.updateCollisionBox(this.x, this.y, this.x + this.width, this.y + this.height);

this.pipe.getUnder().checkCollisionWithAnotherCollisionBox(this.bird.getCollisionBox())//ist ein boolean in einer if abfrage

ich lasse mit die boxen rendern und sie werden angezeigt also denke ich das es an der Kollisions abfrage liegt.
Ich hoffe das mir jemand von euch helfen kann dies zu fixen.
 
width und height sind irreführende Bezeichnungen. Tatsächlich werden in CollisionBox die zwei Punkte links oben und rechts unten verwaltet. Die Methode checkCollisionWithAnotherCollisionBox (NB: WithAnotherCollisionBox kanst Du Dir sparen, das ergibst sich aus dem Typ des Parameters) prüft lediglich ab, ob die betreffende Box die übergebene Box enthält. Sie prüft nicht, ob es Überschneidungen gibt.
 
width und height sind irreführende Bezeichnungen. Tatsächlich werden in CollisionBox die zwei Punkte links oben und rechts unten verwaltet. Die Methode checkCollisionWithAnotherCollisionBox (NB: WithAnotherCollisionBox kanst Du Dir sparen, das ergibst sich aus dem Typ des Parameters) prüft lediglich ab, ob die betreffende Box die übergebene Box enthält. Sie prüft nicht, ob es Überschneidungen gibt.
Danke für die schnelle Antwort. Das width und height irreführende Bezeichnungen sind ist mir gestern Abend bewusst geworden. Allerdings habe ich keine Ahnung wie ich dann Überschneidungen überprüfe und warum es unten aber nicht oben funktioniert
 
Naja, überleg Dir mal für den eindimensionalen Fall, dass sich bei
Code:
   a        b   
|-----| |-------|
a1   a2 b1     b2

    b        a  
|-------| |-----|
b1     b2 a1    a2
a und b nicht überschneiden. Salopp formuliert liegt keine Überschneidung vor, genau dann wenn ein Element vor dem anderen endet, also a2 < b1 oder (OR) b2 < a1 gilt. Umgekehrt liegt also eine Überschneidung vor, genau dann wenn a2 >= b1 und (AND) b2 >= a1 gilt: Nachtrag: ich gehe natürlich stillschweigend davon aus, dass a1 < a2 und b1 < b2 gilt.

Das machst Du jetzt für beide Dimensionen und bist fertig.
 
soll ein Flappy Bird werden
Wenn ich mir das Spiel anschaue, dann wird fortlaufend eine Kollision von Kreis(Vogel) und Rechteck(Wand) geprüft.
Java:
public boolean circleRectCollision(int mx, int my, int r, int x, int y, int width, int height) {
        int mrx = mx < x ? x : mx > x + width ? x + width : mx;
        int mry = my < y ? y : my > y + height ? y + height : my;
        int dx = mrx - mx;
        int dy = mry - my;
        return dx * dx + dy * dy < r * r;
}
Man könnte jetzt noch den parametrisierten Kopf etwas aufräumen, so dass dieser nur die Klasse Circle und Rectangle übergeben bekommt, aber im Großen und Ganzen würde ich persönlich wohl so das Problem angehen.

edit: Java selbst bietet eine Möglichkeit, Rechtecke auf Kollision zu prüfen. Schau dir dafür die Klasse Rectangle an, die Methoden intersection(Rectangle r) und intersects(Rectangle r) könnten nützlich für dich sein.
 

Zurück
Oben