Kollisionserkennung und -behandlung

liann

Mitglied
Hallo, ich baue gerade ein jump 'n' run und die Art, wie ich Wände gebaut habe, ist weder effizient noch zuverlässig. Ich weiß nicht, wie man Wände effizient baut.
Bei meiner methode habe ich Object des Interfaces Solid, welche nur die Methode getBounds() hat, und gucke, ob sich der Spieler innerhalb des Solid Objekt befindet. Wenn ja, drücke ich ihn raus. Nur klappt das nicht immer. Code:
Code:
public void pushOut(Solid b) {
        Rectangle bounds = b.getBounds(x, y);
        Rectangle tBounds = getBounds();
        if (!bounds.intersects(tBounds))
            return;
        short bx = (short) bounds.x;
        short by = (short) bounds.y;
        short x = (short) tBounds.x;
        short y = (short) tBounds.y;
        boolean rOverlap = x < bx && x < bx + bounds.width && x + tBounds.width > bx + bounds.width;
        boolean lOverlap = x < bx && x + tBounds.width > bx;
        boolean dOverlap = y < by && y < by + bounds.height && y + tBounds.height > by + bounds.height;
        boolean uOverlap = y < by && y + tBounds.height > by && y + tBounds.height < by + bounds.height;
        if (uOverlap || dOverlap) {
            verVelocity = 0;
            if (uOverlap) {
                this.y += by - tBounds.height - tBounds.y;
            } else {
                this.y += by - tBounds.y;
            }
        } else if (lOverlap) {
            this.x += bx - tBounds.width - tBounds.x;
        } else if (rOverlap) {
            this.x += bx - tBounds.x;
        }
    }
 
Nicht böse gemeint, aber: "Effiziente Wand Benutzung" als Threadthema... ehrlich jetzt? Das, was du da eigentlich tust/willst, nennt sich "Kollisionserkennung und -behandlung". Und eine "Wand" ist bei dir nichts anderes als ein axis-aligned rectangle. Nach den Begriffen "collision detection and response axis-aligned rectangle" könntest du mal suchen.
Eine sehr gute Quelle ist: https://www.gamedev.net/tutorials/p...-aabb-collision-detection-and-response-r3084/ (es geht hier zwar um den 3D-Fall, aber lass einfach die z-Koordinate weg)
 
Also, ganz plumpe Frage aber, du willst feststellen ob ein Männeken innerhalb eines Vierecks ist?
 

Zurück
Oben