TheDudeAbides
TheDudeAbides

Reputation: 430

LibGDX drawing circles scales somehow

I try to draw 2 circles using textures in Java with LibGDX. My classes are pretty straightforward, GameObject has a circle (for logic) and a draw method.

Here is my draw method:

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

This is the code I execute:

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

And this is the result: Strange scaling on the balls

Somehow using circles with the same size, scales them when placed further away from the origin. Currently my main class extends ApplicationAdapter and that's it.

Here is the stack of calls I make in my builder.

circle.setPosition(x, y); // LibGDX.Circle class call
circle.setRadius(radius); // LibGDX.Circle class call
sprite = new Sprite(texture);

Using a ShapeRenderer, calling the same objects and drawing a filled circle yields these results:

shapeRenderer.setColor(Color.BLACK);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);

for(GameObject goo : objects.values()) {
    shapeRenderer.circle(goo.getX(), goo.getY(), goo.getRadius());
}

shapeRenderer.end();

black balls on top

Final edit:

batch.draw(sprite, circle.x-circle.radius, circle.y-circle.radius, circle.radius*2, circle.radius*2);

Did the trick!

Upvotes: 0

Views: 1035

Answers (1)

Michael Hobbs
Michael Hobbs

Reputation: 1693

Been a while since I used libGDX and I can tell some things have changed. However...

void draw(TextureRegion region,
      float x,
      float y,
      float width,
      float height)

Draws a rectangle with the bottom left corner at x,y and stretching the region to cover the given width and height.

Batch.draw draws a rectangle from x,y to x + width, y + height. What you have done is to scale the shape based on the objects internal position.

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

for circle 1 we have batch.draw(sprite, 32-32, 32-32, 32+32, 32+32); This draws the sprite from 0,0 to 64,64

for circle 2 we have batch.draw(sprite, 64-32, 64-32, 64+64, 64+64); This draws the sprite from 32,32 to 128,128

This should fix your code:

@Override
public void draw(Batch batch, float alpha){
    batch.draw(sprite, circle.x, circle.y, circle.radius, circle.radius);
}

This will scale the objects base on the circle's radius.

Reference: http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/Batch.html#draw-com.badlogic.gdx.graphics.Texture-float-float-float-float-

Upvotes: 1

Related Questions