# Anchoring a sprite to a specific x & y on another, regardless of rotation [Gamemaker]

I have a sprite, sDetritus, with a corresponding object, oDetritus, and an sTurret and oTurret.

If I apply no rotation (i.e. changing the image_angle) to oDetritus, I can anchor the oTurret to a specific position on the ODetritus, using its sprite as a guideline. In this case, the desired x and y for the oTurret, in relation to the oDetritus's x and y origin, are x - 37 and y + 192.

I can make the oDetritus the Owner of the oTurret, and make sure that the oTurret 'sticks' with the following in its Step event:

if instance_exists(Owner) {
x = Owner.x - 37
y = Owner.y + 192
}


However, how would I adjust this to account for rotating the image_angle of the oDetritus object underneath? Obviously then the x and y values would be wrong. I have tried using lengthdir_x and lengthdir_y, but this doesn't seem to be the correct function here.

## 1 Answer

This is classic rotation of a vector. You have your offset vector (-37, 192) which you need to rotate.

xoffset = -37;
yoffset = 192;

rotatedxoffset = xoffset * cos(angle) - yoffset * sin(angle);
rotatedyoffset = xoffset * sin(angle) + yoffset * cos(angle);

x = Owner.x + rotatedxoffset;
y = Owner.y + rotatedyoffset;


If you are interested in learning why this is the case, you can learn about matrices and 2d transformations.

• Thanks! It's about time I actually learnt about cos and sin :) – Bonfire Dog Jan 22 '16 at 11:49