[LibGDX] [Solved] How to rotate a single texture in a spritebatch?

     All the methods that have the ability to rotate only take in Texture Regions not textures, and I really need to only rotate a single texture. If I just put in place a Texture in place of the texture region it doesn't work and spits out an error.

Wrap the texture in a region encompassing the whole thing and pass it that?

I was hoping for a less clunky way of doing but I guess there is none.

What makes [icode]new TextureRegion(texture);[/icode] clunky? Just curious.

The fact that this is the constructor

batch.draw(region, x, y, originX, originY, width, height, scaleX, scaleY, rotation, clockwise)

and even doing this is not displaying the texture at all.

Maybe what you actually want is a Sprite? (which is a texture region)
It inverts the control, you call sprite.draw(batch) instead of doing it all yourself. (and possibly writing broken code)

Well I’ve got the darn thing to at least draw but now I’m facing another problem. I have an Array List of planets that are supposed to be rotating, but instead of all of them staying put rotating, only the bottom left planet of the grid of them is staying put while the rest contract and expand (While also rotating). What am I doing wrong here?

public class Planet {

	public boolean isMouseHovered;
	private Texture PlanetWhite, PlanetRed, PlanetGreen, PlanetSelect;
	private TextureRegion planetRegion;
	private int x, y, OriginX, OriginY, Width, Height;
	private int rotation = 0;
	

	public Planet(int x, int y, Texture PlanetWhite, Texture PlanetRed,
			Texture PlanetGreen, Texture PlanetSelect, TextureRegion PlanetRegion) {
		
		this.x = x;
		this.y = y;
		this.PlanetWhite = PlanetWhite;
		this.PlanetRed = PlanetRed;
		this.PlanetGreen = PlanetGreen;
		this.PlanetSelect = PlanetSelect;
		this.planetRegion = PlanetRegion;
		Width = PlanetWhite.getWidth();
		Height = PlanetWhite.getHeight();
		OriginX = (x + Width)/2;
		OriginY = (y + Height)/2;

		
	}

	public void render(Vector3 mousePos, SpriteBatch batch) {

		batch.draw(planetRegion, x, y, OriginX, OriginY, Width, Height, 1, 1, rotation, false);

		if (mousePos.x < x + PlanetWhite.getWidth() && mousePos.x > x
		&& mousePos.y < y + PlanetWhite.getHeight() && mousePos.y > y) {
			batch.draw(PlanetSelect, x, y);
		}

	}

	public void update() {
		rotation += 1;
	}

}

Where I created all my planets:


		/*Planets */	for(int i = 0; i < 25; i++){
				for(int u = 0; u < 25; u++){
					planetList.add(new Planet(400*i, 400*u, PlanetWhite, PlanetRed, PlanetGreen, PlanetSelect, planetRegion));
				}
			}

From Sprite.java: (emphasis mine)

Try:


Width = PlanetWhite.getWidth();
Height = PlanetWhite.getHeight();
-OriginX = (x + Width)/2;
-OriginY = (y + Height)/2;
+OriginX = Width / 2;
+OriginY = Height / 2;

(All of those fields should really be floats.)

Again, all of this behavior has already been encapsulated by Sprite.

Alright then thank you very much! And alright, I’ll try to switch over to sprite. Sorry for the code mess by the way, didn’t mean to put you through all that, I’ve just been testing a butt load of different things that libGdx offers so that I can learn it. ;D

Could you perhaps explain to me WHY it is that works? I don’t understand how such a simple change fixed everything.

When exploring the library, read as much of the docs as you can, so that you discover what has already been done for you, is possible, etc. Good practice for anything really.

[quote]The origin is given relative to the bottom left corner of the Sprite, its position.
[/quote]
The point about which the batcher rotates stuff is not defined by you as an absolute position, you are to define it as a position relative to the Position value of the sprite (region).
RotationPoint = Position + X, where you provide the x. You were adding in position as if the origin is absolutely defined, so it was being counted twice, and was thus wrong.


void	draw(Texture texture, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation, int srcX, int srcY, int srcWidth, int srcHeight, boolean flipX, boolean flipY)
//Draws a rectangle with the bottom left corner at x,y having the given width and height in pixels.


There is this monstrosity what you should call when you want rotation and not use TextureRegion/Sprite.

Thank you very much for this explanation. How stupid of me not to read the full documentation. :cranky: