LibGDX 绘制圆圈以某种方式缩放
LibGDX drawing circles scales somehow
我尝试使用 Java 和 LibGDX 中的纹理绘制 2 个圆圈。
我的 classes 非常简单,GameObject 有一个圆圈(用于逻辑)和一个绘制方法。
这是我的绘制方法:
@Override
public void draw(Batch batch, float alpha){
batch.draw(sprite, circle.x-circle.radius, circle.y-circle.radius, circle.x+circle.radius, circle.y+circle.radius);
}
这是我执行的代码:
GameObject go = new GameObjectBuilder().createNew()
.withPosition(32, 32)
.withRadius(32)
.withTexture(new Texture(Gdx.files.internal("ball.png")))
.withId(0)
.build();
GameObject go2 = new GameObjectBuilder().createNew()
.withPosition(64, 64)
.withRadius(32)
.withTexture(new Texture(Gdx.files.internal("ball.png")))
.withId(1)
.build();
stage.addActor(go);
stage.addActor(go2);
这是结果:
以某种方式使用相同大小的圆圈,当它们远离原点时会缩放它们。目前我的主要 class 扩展了 ApplicationAdapter 就是这样。
这是我在构建器中调用的堆栈。
circle.setPosition(x, y); // LibGDX.Circle class call
circle.setRadius(radius); // LibGDX.Circle class call
sprite = new Sprite(texture);
使用 ShapeRenderer,调用相同的对象并绘制实心圆会产生以下结果:
shapeRenderer.setColor(Color.BLACK);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
for(GameObject goo : objects.values()) {
shapeRenderer.circle(goo.getX(), goo.getY(), goo.getRadius());
}
shapeRenderer.end();
最终编辑:
batch.draw(sprite, circle.x-circle.radius, circle.y-circle.radius, circle.radius*2, circle.radius*2);
成功了!
自从我使用 libGDX 以来已经有一段时间了,我可以看出有些事情已经发生了变化。然而...
void draw(TextureRegion region,
float x,
float y,
float width,
float height)
在 x,y 处绘制一个左下角的矩形并拉伸该区域以覆盖给定的宽度和高度。
Batch.draw 从 x,y 到 x+width,y+height 绘制一个矩形。您所做的是根据对象的内部位置缩放形状。
@Override
public void draw(Batch batch, float alpha){
batch.draw(sprite, circle.x-circle.radius, circle.y-circle.radius, circle.x+circle.radius, circle.y+circle.radius);
}
对于圆圈 1,我们有
batch.draw(精灵, 32-32, 32-32, 32+32, 32+32);
这会将精灵从 0,0 绘制到 64,64
对于圆圈 2,我们有
batch.draw(精灵, 64-32, 64-32, 64+64, 64+64);
这会将精灵从 32,32 绘制到 128,128
这应该可以修复您的代码:
@Override
public void draw(Batch batch, float alpha){
batch.draw(sprite, circle.x, circle.y, circle.radius, circle.radius);
}
这将根据圆的半径缩放对象。
我尝试使用 Java 和 LibGDX 中的纹理绘制 2 个圆圈。 我的 classes 非常简单,GameObject 有一个圆圈(用于逻辑)和一个绘制方法。
这是我的绘制方法:
@Override
public void draw(Batch batch, float alpha){
batch.draw(sprite, circle.x-circle.radius, circle.y-circle.radius, circle.x+circle.radius, circle.y+circle.radius);
}
这是我执行的代码:
GameObject go = new GameObjectBuilder().createNew()
.withPosition(32, 32)
.withRadius(32)
.withTexture(new Texture(Gdx.files.internal("ball.png")))
.withId(0)
.build();
GameObject go2 = new GameObjectBuilder().createNew()
.withPosition(64, 64)
.withRadius(32)
.withTexture(new Texture(Gdx.files.internal("ball.png")))
.withId(1)
.build();
stage.addActor(go);
stage.addActor(go2);
这是结果:
以某种方式使用相同大小的圆圈,当它们远离原点时会缩放它们。目前我的主要 class 扩展了 ApplicationAdapter 就是这样。
这是我在构建器中调用的堆栈。
circle.setPosition(x, y); // LibGDX.Circle class call
circle.setRadius(radius); // LibGDX.Circle class call
sprite = new Sprite(texture);
使用 ShapeRenderer,调用相同的对象并绘制实心圆会产生以下结果:
shapeRenderer.setColor(Color.BLACK);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
for(GameObject goo : objects.values()) {
shapeRenderer.circle(goo.getX(), goo.getY(), goo.getRadius());
}
shapeRenderer.end();
最终编辑:
batch.draw(sprite, circle.x-circle.radius, circle.y-circle.radius, circle.radius*2, circle.radius*2);
成功了!
自从我使用 libGDX 以来已经有一段时间了,我可以看出有些事情已经发生了变化。然而...
void draw(TextureRegion region,
float x,
float y,
float width,
float height)
在 x,y 处绘制一个左下角的矩形并拉伸该区域以覆盖给定的宽度和高度。
Batch.draw 从 x,y 到 x+width,y+height 绘制一个矩形。您所做的是根据对象的内部位置缩放形状。
@Override
public void draw(Batch batch, float alpha){
batch.draw(sprite, circle.x-circle.radius, circle.y-circle.radius, circle.x+circle.radius, circle.y+circle.radius);
}
对于圆圈 1,我们有 batch.draw(精灵, 32-32, 32-32, 32+32, 32+32); 这会将精灵从 0,0 绘制到 64,64
对于圆圈 2,我们有 batch.draw(精灵, 64-32, 64-32, 64+64, 64+64); 这会将精灵从 32,32 绘制到 128,128
这应该可以修复您的代码:
@Override
public void draw(Batch batch, float alpha){
batch.draw(sprite, circle.x, circle.y, circle.radius, circle.radius);
}
这将根据圆的半径缩放对象。