libGDX weird sprite rotation error

I am currently working on a project where the character’s arm is rotated around based on the mouse’s location, using Math.atan2(). My problem is, I want the player to be holding weapons in their hand, and it gets a little weird. I’m using the sprite class for rotated images, so I have to position and then rotate the sprite in question. Currently, this is accomplished by this:

primaryWeaponl.setRotation(aimAngle + 90);
		primaryWeaponl.setPosition(
				playerScreenX + armXDis
						+ (float) Math.sin(Math.toRadians(aimAngle))
						* (characterArm.getHeight()), 
				playerScreenY + armYDis
						- (float) Math.cos(Math.toRadians(aimAngle))
						* characterArm.getHeight());

my issue is that when I position the image, it looks just fine:

http://imageshack.us/a/img541/134/error1f.png

http://imageshack.us/a/img515/186/error2es.png

but when I add in the rotation (the first line):

http://imageshack.us/a/img515/8813/error4w.png

http://imageshack.us/a/img577/6536/error3mo.png

I’m not sure what is causing this, but any help would be great ( sorry for the image shapes, I simply cropped them by hand :-\ ) could it be a problem with the sprite class? I’ve tried setting the origin for rotation, but it doesn’t seem to help.

You’ll need to set the sprite origin to the point you want to rotate around. The origin is relative to the sprite’s position (the bottom left corner).

For some reason, setting the sprite origin is doing nothing for me. I set it to (Sprite.getWidth() - 8, 6) ( the location of the handle) and I’m still having the same issue, in the exact same location.

Be sure you are calling sprite.draw(batch) and not batch.draw(sprite). This is an unfortunate API gotcha (Sprite is-a TextureRegion and should have used composition instead, can’t change it now).

I have been using sprite.draw, I’m really not sure what I’m doing wrong. Would it be helpful if I posted the entire render() loop? in the create() loop it sets the player’s arm’s origin just fine, so I’m not sure what the error here is :confused:

Yes of course.

Here it is:
http://pastebin.java-gaming.org/43eff31693d

Don’t call new in your render method.

You are changing the primaryWeaponl position based on the rotation angle. Just set the position, origin, and rotation, then draw.

I recommend creating a simpler test case where you can experiment more easily. You can use this as a base:
https://code.google.com/p/libgdx/wiki/GettingHelp#Barebones_SpriteBatch

Thanks nate, i’ll just use the bare-bones and then see wht my issue is. Thanks :slight_smile:

EDIT: decided to go the easy route since this has been bugging me for the past few days, and it looks nicer than it ever could the other way.