Surfpup's tConfig Mod Wiki
Advertisement

Angles in terraria are used to position items when using, or when holding. This is done via the Player.itemRotation property. This article covers what these angles actually are, and how to use them.

This article assumes you have a good knowledge of measuring angles in radians.

Basics[]

Angles1

Angles on a player

Angles are measured in radians, with zero being the horizontal in the direction the player is facing. The diagram on the right shows the angles on a playing facing left. When facing right, these angles are exactly the same, but still in the direction the player is facing, so effectively a mirror image.

When used with items, the sprite is rotated through the angle. An item pointing at pi radians is not just pointing in the opposite direction to the one that the player is facing, it will also appear upside down.

Getting Angles[]

The angle that is most commonly needed is the one between the player and the mouse. Both player position and mouse position can be found from the Terraria classes. The following code would sets an item to point at the mouse. It can easily be adapted to do anything else, using the angle given in itemRot.

float mX = (float)(Main.mouseX + Main.screenPosition.X);
float mY = (float)(Main.mouseY + Main.screenPosition.Y);
float pX = player.position.X + player.width * 0.5f;
float pY = player.position.Y + player.height * 0.5f; 

float itemRot = (float)Math.Atan2(mY - pY, mX - pX);
if (player.direction == -1) itemRot -= (float)Math.PI; 



player.itemRotation = itemRot;

In the code, (mX, mY) is the cartesian coordinate of the mouse (adjusted to be absolute coordinates within the world), and (pX, pY) is the coordinate of the centre of the player (Note that player.position.* is actually the top left corner of the player sprite, so is adjusted).

The code also takes into account the player direction, so if the player is looking left, then the angle is rotated through pi radians.

Problems with Angles[]

Whilst angles themselves are fairly simple, positioning items on the player is not. Simply copying and pasting the above code will result in the item being off centre, and sometimes no where near the player.

Advertisement