我对Libgdx框架有疑问。我试图创建一个类扩展Sprite,但是当我渲染它时,我意识到body
没有与精灵纹理对齐。
以下是显示问题的图片:
您可以看到以下代码:
public class GamePlay implements Screen {
private GameMain game;
private OrthographicCamera mainCamera;
private Viewport viewport;
private OrthographicCamera b2dcam;
private Box2DDebugRenderer debugRenderer;
private World world;
private Texture background;
private Brick brick;
float timer = 0;
public GamePlay(GameMain game)
{
this.game = game;
mainCamera = new OrthographicCamera(GameInfo.WIDTH, GameInfo.HEIGHT);
mainCamera.position.set(GameInfo.WIDTH/2, GameInfo.HEIGHT/2,0);
viewport = new FitViewport(GameInfo.WIDTH, GameInfo.HEIGHT, mainCamera);
b2dcam = new OrthographicCamera();
b2dcam.setToOrtho(false, GameInfo.WIDTH/GameInfo.PPM, GameInfo.HEIGHT / GameInfo.PPM);
b2dcam.position.set(GameInfo.WIDTH/2f, GameInfo.HEIGHT/2f, 0);
debugRenderer = new Box2DDebugRenderer();
world = new World(new Vector2(0,0), true);
background = new Texture("BGS/GameBG.png");
brick = new Brick(world, "my brick");
brick.setBricksPosition(cgp(20), cgp(20));
}
//region Screen Methods
@Override
public void show() {
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
game.getBatch().begin();
game.getBatch().draw(background,0,0);
brick.draw(game.getBatch());
game.getBatch().end();
debugRenderer.render(world, b2dcam.combined);
world.step(Gdx.graphics.getDeltaTime(), 6,2);
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
background.dispose();
}
//endregion
private float cgp(int gridPos)
{
return gridPos * GameInfo.gridSize;
}}
public class Brick extends Sprite {
private World world;
private Body body;
private String name;
private Fixture fixture;
public Brick(World world, String name)
{
super(new Texture("Bricks/brick.png"));
this.world = world;
this.name = name;
}
void createBody()
{
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set((getX() - getWidth())/ GameInfo.PPM, (getY() + getHeight()/2)/ GameInfo.PPM);
body = world.createBody(bodyDef);
PolygonShape shape = new PolygonShape();
shape.setAsBox((getWidth()/2)/GameInfo.PPM, (getHeight()/2)/GameInfo.PPM);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixture = body.createFixture(fixtureDef);
shape.dispose();
}
public void setBricksPosition(float x, float y)
{
setPosition(x,y);
createBody();
}
public void draw(SpriteBatch batch)
{
batch.draw(this, getX(), getY());
}}
答案 0 :(得分:0)
box2d调试渲染(显示身体的实际位置),你的精灵“非常接近”,所以我猜你不远了,问题很可能在你的box2d位置vs的计算/翻译中屏幕位置与像素对米。
你做PPM转换,这样做很好。我没有时间查看您的所有代码,但由于2项非常接近,请再次检查您的计算。这一行:
bodyDef.position.set((getX() - getWidth())/ GameInfo.PPM, (getY() + getHeight()/2)/ GameInfo.PPM);
引起了我的注意。就y而言,渲染调试和精灵是正确的,但在x上是关闭的。你的身高使用getHeight()/ 2,但getWidth不会除以2.也许这就是你所有的缺失(将宽度除以2)?
答案 1 :(得分:0)
最后我找到了解决方案。 出现问题的原因是我的PPM值。我把它设置为100,当你把它分成屏幕尺寸时,它不会产生整数。所以我将它改为160,可以分为800和480,问题解决了。