Próbuję użyć Renderer debugowania Box2D wraz z moich Sprites i ciała LibGDX. Problem polega na tym, że Renderer rysuje Ciało pudła na środku ekranu, a następnie Sprite rysuje w domyślnej lokalizacji (0,0) w lewym dolnym rogu ekranu. Kiedy poruszam Sprite Samochodowe, poruszam się zarówno Samochodem jak i Debugowaniem, sprawiając, że nie nachodzą na siebie.Jak używać kamer LibGDX z Box2D Debugowanie Renderer
Wiem, że problem tkwi w kamerze, ponieważ od paru dni robię z różnymi wartościami kamery. Czasem nakładają się na siebie, ale wtedy Box2D Debug Body porusza się szybciej niż Sprite Samochodu.
Czasami korpus Box2D znajduje się w tej samej pozycji co Sprite, ale bardzo mały. Używam 2 kamer. Jeden, który jest 720 x 480. Aparat jest Debug w metrach tak jego, 24 x 16.
Oto niektóre kodu gdzie problem może leżeć (używam Etapy i aktorów):
BattleScreen.java :
public void show() {
battleStage = new Stage(720, 480, false);
// The Box2D Debug Renderer will handle rendering all physics objects for debugging
debugRenderer = new Box2DDebugRenderer(true, true, true, true);
debugCam = new OrthographicCamera(24, 16);
}
public void render() {
// Set the Camera matrices
battleStage.getCamera().update();
// Update the Physics World, use 1/45 for something around 45 Frames/Second for mobile devices
physicsWorld.step(1/45.0f, 8, 3); // 1/45 for devices
// Again update the Camera matrices and call the debug renderer
//debugCam.update();
debugRenderer.render(physicsWorld, debugCam.combined);
// Update all Game Objects then Draw them
battleStage.act(delta);
battleStage.draw();
}
Car.java: (również aktorem)
public Car(Texture texture) {
super("Car");
mSprite = new Sprite(texture);
mSprite.setSize(54, 105);
mSprite.setOrigin(mSprite.getWidth()/2, mSprite.getHeight()/2); // set the origin to be at the center of the body
FixtureDef carFixtureDef = new FixtureDef();
mBody = Physics.createBoxBody(BodyType.DynamicBody, carFixtureDef, mSprite);
}
public static Body createBoxBody(final BodyType pBodyType, final FixtureDef pFixtureDef, Sprite pSprite) {
final BodyDef boxBodyDef = new BodyDef();
boxBodyDef.type = pBodyType;
// Temporary Box shape of the Body
final PolygonShape boxPoly = new PolygonShape();
final float halfWidth = pSprite.getWidth() * 0.5f/Consts.PIXEL_METER_RATIO;
final float halfHeight = pSprite.getHeight() * 0.5f/Consts.PIXEL_METER_RATIO;
boxPoly.setAsBox(halfWidth, halfHeight); // set the anchor point to be the center of the sprite
pFixtureDef.shape = boxPoly;
final Body boxBody = BattleScreen.getPhysicsWorld().createBody(boxBodyDef);
boxBody.createFixture(pFixtureDef);
boxPoly.dispose();
return boxBody;
}
I m pogorszyć sprawę. To naprawdę komplikuje się, kiedy staram się, aby kamera główna podążała za samochodem.
krok bliżej! I właśnie próbował losowo 'debugCam.unproject (battleStage.getCamera() pozycji.);' A to prawie udało się! Pole jest teraz wyłączone tylko o kilka pikseli. –