LibGDX - How to rotate one point around another

Hi guys,

I am really close to solving this issue, but my math skills are lacking so I need some input. I have a player, that player has a separate “arm” object that can swing and show the selected item the player is holding. I have the arm swinging motion working fine but now I am trying to display the selected item in his “hand” swinging around the same origin point that his arm is attached. See the video below for the problem.

A6MYCWTN4ZU

I’ve slowed his swing speed quite a bit to illustrate the problem and there is a noticeable gap between the item and his arm at certain points of the rotation. I think the fact that LibGDX offsets the rectangle of the item by originX and originY is what is messing it up, but I don’t know how to fix it. This code determines the position of the arm and the item:


public void update(float dt)
{
	selectedItem = player.getSelectedItem();

        // This set the position of the arm itself
	position.set(player.position.x + 0.3f, player.position.y + 0.4f);

        // This code sets the items position to rotate around the arm position
        // height is the length of the arm which I'm using as the radius of the "circle"
	itemPosition.set(position.x + height * (float)Math.cos((rotation - 90) * MathUtils.degRad), position.y + height * (float)Math.sin((rotation - 90) * MathUtils.degRad));
		
}

This code renders everything which is where I believe I’m going wrong:


public void render(SpriteBatch sb)
{
        // This one draws the arm and works perfectly.  I change the originX to half width
	sb.draw(region, position.x, position.y, width * 0.5f, height, width, height, 1, 1, rotation);

        // This draws the selected item sprite according to the same rotation and where I believe the issue is occuring
	if(selectedItem != null)
	{
		sb.draw(Game.res.getTextureRegion(selectedItem.getID()), 
				itemPosition.x, itemPosition.y, 
				0.2f, 0.6f,  // originX and originY - Been playing with these to get close to desired effect
				0.6f, 0.6f,  // width and height of item texture region
				1,1,
				rotation);
		}
	}

I should mention that I am using a Y-DOWN coordinate system with my batch. Really banging my head with this one! Thanks in advance.

The ‘itemPosition’ uses position.xy as the origin, while the SpriteBatch.draw uses the projection’s origin + origin.xy. These are apparently mismatched. What is the SpriteBatch’s origin?

you can do something like this:


Vector2 baseArmPos = new Vector2(1f, 0f, 0f); // arm is offset 1 unit to the right of player
float armRotation = 45f; // determine this however you need to, we just define it here
Vector2 armPos = new Vector2(baseArmPos).setAngle(armRotation);
Vector2 finalArmPos = new Vector2(playerPos).add(armPos);