2012-04-01 2 views
6

내 LibGDX 스프라이트 및 바디와 함께 Box2D 디버그 렌더러를 사용하려고합니다. 내가 겪고있는 문제는 렌더러가 스크린의 중앙에 박스 바디를 그린 다음 스프라이트가 화면 왼쪽 아래의 (0,0) 기본 위치에 그려진다는 것입니다. Car Sprite를 움직이면 Car와 Debug Box가 모두 움직이면서 겹치지 않게됩니다.Box2D 디버그 렌더러에서 LibGDX 카메라를 사용하는 방법

저는 며칠 동안 다른 카메라 값으로 주변을 어지럽 혀 왔기 때문에 문제가 있다는 것을 알고 있습니다. 겹치기도하지만 Box2D Debug Body는 Car Sprite보다 빠르게 움직입니다.

때때로 Box2D 본체는 Sprite와 같은 위치에 있지만 매우 작습니다. 나는 2 대의 카메라를 사용하고있다.

BattleScreen.java : 그것, 여기서 X (16)

(24)이 문제는 (내가 단계와 배우를 사용하고 있습니다) 거짓말을 수있는 몇 가지 코드가 그래서 720 X 480 디버그 카메라가 하나 미터에 :

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 : (또한 액터)

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; 
} 

및 m 행 나쁜 일. 메인 카메라가 차를 따라 가면 정말 복잡해집니다.

+0

한 단계가 더 가깝습니다. 나는 무작위로'debugCam.unproject (battleStage.getCamera(). position);을 시도해 보았다. 그리고 거의 효과가 있었다! 이 상자는 이제 몇 픽셀 만 떨어져 있습니다. –

답변

3

battleStage.getCamera().combined? 결합 된 나를 위해 잘 작동합니다.

+0

도 시도했지만 비슷한 문제가 발생합니다. –

0

몸체와 그려진 텍스처를 결합하려면 다음 문을 적용해야합니다. stage.setCamera (camera);

관련 문제