Java libgdx-box2d,以一定角度创建时纹理/主体不在正确位置
当我创建一个实体并在该实体所在的位置绘制纹理时,如果角度设置为0.0f,它将显示在预期的位置。竖直放置在球员的x和y中心(黑色圆圈)。当此角度更改时,纹理的x和y看起来完全关闭。事实上,它们往往会离开屏幕,因为我有时只能在重力作用下看到它们 下面是我创建新项目符号的方法:Java libgdx-box2d,以一定角度创建时纹理/主体不在正确位置,java,libgdx,box2d,Java,Libgdx,Box2d,当我创建一个实体并在该实体所在的位置绘制纹理时,如果角度设置为0.0f,它将显示在预期的位置。竖直放置在球员的x和y中心(黑色圆圈)。当此角度更改时,纹理的x和y看起来完全关闭。事实上,它们往往会离开屏幕,因为我有时只能在重力作用下看到它们 下面是我创建新项目符号的方法: private void shoot(final float delta) { gameTime += delta; final float timeSinceLastShot = gameTime - las
private void shoot(final float delta) {
gameTime += delta;
final float timeSinceLastShot = gameTime - lastBulletShotTime;
//5 bullets a second, kerpow
if (timeSinceLastShot > 0.2f) {
lastBulletShotTime = gameTime;
shot.play();
final float shotX = player.getX() + ((player.getWidth() / 2) - (bulletTexture.getWidth() / 2));
final float shotY = player.getY() + ((player.getHeight() / 2) - (bulletTexture.getHeight() / 2));
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(shotX, shotY);
Body body = world.createBody(bodyDef);
float angle = player.getRotation() * MathUtils.degreesToRadians;
body.setTransform(body.getPosition().x, body.getPosition().y, angle);
System.out.println("angle rad: " + angle);
bullets.add(body);
PolygonShape shape = new PolygonShape();
shape.setAsBox(bulletTexture.getWidth() / 2, bulletTexture.getHeight() / 2);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 1f;
Fixture fixture = body.createFixture(fixtureDef);
shape.dispose();
}
}
这里是渲染方法的一部分,我循环并绘制项目符号:
for (Body bullet : bullets) {
final float x = bullet.getPosition().x;
final float y = bullet.getPosition().y;
final float originX = x + (bulletTexture.getWidth() / 2);
final float originY = y + (bulletTexture.getHeight() / 2);
final float width = bulletTexture.getWidth();
final float height = bulletTexture.getHeight();
final float scaleX = 1.0f;
final float scaleY = 1.0f;
final float rotation = bullet.getAngle() * MathUtils.radiansToDegrees;
final int sourceX = 0;
final int sourceY = 0;
final int sourceWidth = bulletTexture.getTextureData().getWidth();
final int sourceHeight = bulletTexture.getTextureData().getHeight();
final boolean flipX = false;
final boolean flipY = false;
batch.draw(bulletTexture,
x, y,
originX, originY,
width, height,
scaleX, scaleY,
rotation,
sourceX, sourceY,
sourceWidth, sourceHeight,
flipX, flipY);
}
我做错了什么,有什么建议吗?我希望子弹总是从球员圆圈的中心开始,但也要以正确的角度开始。然后我打算增加一些速度,让他们“射击”
完整的代码可以在Github上找到
我还附上了一个屏幕截图,在拍摄和拍摄之间,所有的东西都随着重力下降了一点,但是左下角的子弹最初出现在正确的位置,而其他的都从屏幕上掉了下来(我“拍摄”的一些子弹从来都不可见)
我的错误在于originX和originY。更正如下,它现在可以工作了:
final float originX = bulletTexture.getWidth() / 2;
final float originY = bulletTexture.getHeight() / 2;