Jump to content
• Advertisement

LibGDX - Rotating sprites from their corners.

This topic is 932 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been struggling with a problem regarding rotations and relative positions between sprites (in this case, the sprites are just lines) - a bit off-topic, but should i be using the ShapeRenderer class to do this, though?

Anyway, it might be quite simple but i can't seem to figure it out properly. Heres a picture of the situation: https://gyazo.com/78988e07991fedc778eaeb70f8a04d8e

Now, after trying a bunch of different approaches, this piece of code is the one i'm testing right now:

   public class TestState extends AbstractGameState {

private static final String LINE = "lineEffect.png";

private Sprite[] sprites;

public TestState(GameStateManager gsm) {
super(gsm);

sprites = new Sprite[2];
for (int i = 0; i < 2; i++) {
sprites[i] = new Sprite(new Texture(LINE));
// both lines starting at the same X and Y
sprites[i].setPosition(20, 50);
// both lines have the same size
sprites[i].setSize(100, 2);
// guess i might be messing up here
sprites[i].setOrigin(sprites[i].getX(), sprites[i].getY());
}

// for testing purposes
sprites[0].setRotation(-20);
sprites[1].setRotation(45);
}

@Override
public void render(SpriteBatch batch) {
batch.setProjectionMatrix(camera.combined);
batch.begin();
for (Sprite sprite : sprites)
sprite.draw(batch);
batch.end();
}


As it's my first ever post here i'm not sure if i'm posting correctly / in the right place so please tell me if that's the case.
So please, any help on this would be appreciated, thanks in advance.

Share this post

Share on other sites
Advertisement

The origin is relative to the sprite's upper-left corner. So, if you want rotations around the upper-left corner, the origin should be 0,0. If you want rotations around the sprite's center, you would use width/2,height/2 (or use setOriginCenter). Note that the origin is also used as an offset to the sprite's position. Essentially, the origin is subtracted from the position when drawing.

Share this post

Share on other sites

The origin is relative to the sprite's upper-left corner. So, if you want rotations around the upper-left corner, the origin should be 0,0. If you want rotations around the sprite's center, you would use width/2,height/2 (or use setOriginCenter). Note that the origin is also used as an offset to the sprite's position. Essentially, the origin is subtracted from the position when drawing.

Fixed by seting the origin to (0, 0), thank you so much!

Share this post

Share on other sites

The origin is relative to the sprite's upper-left corner. So, if you want rotations around the upper-left corner, the origin should be 0,0. If you want rotations around the sprite's center, you would use width/2,height/2 (or use setOriginCenter). Note that the origin is also used as an offset to the sprite's position. Essentially, the origin is subtracted from the position when drawing.

The origin is relative to bottom left in libgdx.

Share this post

Share on other sites

The origin is relative to bottom left in libgdx.

Oops! You are absolutely correct.

Share this post

Share on other sites

• Advertisement
• Advertisement

• Popular Contributors

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5
• Advertisement

• 38
• 23
• 13
• 13
• 17
• Forum Statistics

• Total Topics
631706
• Total Posts
3001835
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!