2016-03-28 4 views
0

Ich lade eine Box2D-Szene aus einer JSON-Datei. Diese Szene enthält eine Fixture, die den Begrenzungsrahmen markiert, in den die Kamera eindringen darf. Dieser Mechanismus funktioniert gut für die unteren und linken Grenzen, scheitert jedoch vollständig für die oberen und rechten Grenzen, was ziemlich merkwürdig ist.LibGDX: GrenzenInFrustrum und BoundingBox funktioniert nicht wie erwartet

Hier ist der Teil, den Begrenzungsrahmen aus der Datei geladen:

PolygonShape shape = ((PolygonShape) fixture.getShape()); 
Vector2 vertex = new Vector2(); 
float boundLeft = world.startX, boundRight = world.startX, boundUp = world.startY, boundLow = world.startY; // The location of the camera as initial value 

for (int i = 0; i < shape.getVertexCount(); i++) { // Itarate over each vertex in the fixture and set the boundary values 
    shape.getVertex(i, vertex); 
    vertex.add(body.getPosition()); 
    boundLeft = Math.min(vertex.x, boundLeft); 
    boundLow = Math.min(vertex.y, boundLow); 
    boundRight = Math.max(vertex.x, boundRight); 
    boundUp = Math.max(vertex.y, boundUp); 
} 

// Build the bounding boxes with enough thickness to prevent tunneling on fast pans 
world.boundLeft = new BoundingBox(new Vector3(boundLeft - 5, boundLow - 5, 0).scl(RenderingSystem.PPM), new Vector3(boundLeft, boundUp + 5, 0).scl(RenderingSystem.PPM)); 
world.boundRight = new BoundingBox(new Vector3(boundRight, boundLow - 5, 0).scl(RenderingSystem.PPM), new Vector3(boundRight + 5, boundUp + 5, 0).scl(RenderingSystem.PPM)); 
world.boundUp = new BoundingBox(new Vector3(boundLeft - 5, boundUp, 0).scl(RenderingSystem.PPM), new Vector3(boundRight + 5, boundUp + 5, 0).scl(RenderingSystem.PPM)); 
world.boundLow = new BoundingBox(new Vector3(boundLeft - 5, boundLow - 5, 0).scl(RenderingSystem.PPM), new Vector3(boundRight + 5, boundLow, 0).scl(RenderingSystem.PPM)); 
// world is a class containing some properties, including these BoundingBoxes 
// RenderingSystem.PPM is the amount of pixels per metre, in this case 64 

und den folgenden Teil wird aufgerufen, wenn sich die Kamera um verrissen wird:

public void pan(float x, float y) { 
    Vector3 current = new Vector3(camera.position); 
    camera.translate(-x, y); 
    camera.update(true); 
    if (camera.frustum.boundsInFrustum(world.boundLeft) || camera.frustum.boundsInFrustum(world.boundRight)) { 
     camera.position.x = current.x; // Broke bounds on x axis, set camera back to old x 
     camera.update(); 
    } 
    if (camera.frustum.boundsInFrustum(world.boundLow) || camera.frustum.boundsInFrustum(world.boundUp)) { 
     camera.position.y = current.y; // Broke bounds on y axis, set camera back to old y 
     camera.update(); 
    } 
    game.batch.setProjectionMatrix(camera.combined); 
} 
+0

Warum sprichst du nicht mit den libdx Jungs, es gibt viele Leute, die sich über diese Bibliothek beschweren! – gpasch

+0

Oh, ich beschwere mich nicht. LibGDX ist die Liebe meines Lebens und ich würde es jede Sekunde heiraten, es ist nur so oft, dass ich nicht so schlau bin wie die Jungs von LibGDX. – ionree

Antwort

0

Nun, ich es heraus . Rate mal, worum es bei meiner world.startx und world.startY geht? Das ist richtig, sie waren in Bildschirm-Koordinaten:

world.startX = start.getPosition().x * RenderingSystem.PPM; 
world.startY = start.getPosition().y * RenderingSystem.PPM; 

Dies war die Math.max verursacht immer die world.startX und world.startY zu holen, da diese Werte im Vergleich absolut massiv waren.