我正在尝试使用Box2D Debug Renderer以及我的LibGDX Sprites和Bodies。我遇到的问题是渲染器在屏幕中央绘制Box Body,然后Sprite在屏幕左下角的默认位置(0,0)处绘制。当我移动Car Sprite时,Car和Debug Box都会移动,使它们不重叠。
我知道问题出在相机上,因为我现在已经搞乱了不同的相机值几天了。有时它们会重叠,但Box2D Debug Body的移动速度比Car Sprite快。
有时Box2D机身与Sprite处于同一位置,但非常小。我正在使用2台相机。一个720 x 480.调试摄像头以米为单位,因此它是24 x 16。
这里有一些代码可能存在问题(我正在使用阶段和演员):
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:(也是演员)
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;
}
让事情变得更糟。当我试图让主摄像头跟随汽车时,它变得非常复杂。
答案 0 :(得分:3)
battleStage.getCamera().combined
怎么样?合并对我来说很好。
答案 1 :(得分:0)
您必须应用以下语句将body与绘制的纹理结合起来 stage.setCamera(照相机);