Gamemaker - Maintain relative position between objects during rotation

I have a large object and a small object - the small object updates its x and y position every step to match the x and y of the larger object. It is easy to keep it in the same position on the larger object without any rotation, as follows (in this instance, slightly above and to the right of the centre of the larger object, here called Owner:

x = Owner.x + 50
y = Owner.y - 50

However, this all goes pearshaped if the larger object is rotated from its sprite's initial position!

How can I maintain the relative position of the two objects even when rotated?

• Not that I am the right person to advice in specifi Game-maker coding, but it would be likely helpful to see the part of the code where you are doing the rotation.
– MAnd
Nov 23 '15 at 21:39
• it's not a step-based rotation; it only occurs once, when the large object is created. It is a random(360) rotation, so the sprite (and therefore the part which I want to bind the smaller object to) changes each time it is spawned, but not again after that. Nov 23 '15 at 21:51

You need to use lengthdir_x and lengthdir_y functions, like:

x = owner.x + lengthdir_x(distance, owner.image_angle); // or owner.direction
y = owner.y + lengthdir_y(distance, owner.image_angle);

Also see answer for similar question, with an example and special tool for calculate values.

P.S. And remember that you need do it in End Step event, and not simple Step

• Thanks Dmi7ry. Could you explain why I need it in an End Step event? I haven't actually had the need to use such an event before. Nov 24 '15 at 23:13
• Usually we change coords in Step event (if doing it manually, like x+=4;). Also GMS updates positions after Step event finished (and before Step End event started)(when used speed, gravity, etc). For example, if you will do hspeed = 4; then you will have position x in Step and x+4 in Step End. And second object must change position after first object changed, otherwise here will be small lag Nov 25 '15 at 4:15
• Thanks for the explanation. I've used the code as above, with the absolute values that I need to position the smaller object in place of the distance value (in this case, 34 for x and 192 for y) and for some reason the y value does not work... the smaller object remains in the centre of the larger object's y-axis. The x-axis does seem to be working correctly, however. Code in next post. Nov 26 '15 at 10:04
• x = Owner.x - lengthdir_x(34, Owner.image_angle) Nov 26 '15 at 10:05
• y = Owner.y + lengthdir_y(192, Owner.image_angle) Nov 26 '15 at 10:06

I don't know about GameMaker, but usually you need to rotate second object with an angle of the first one like this:

// Distances between two object are needed
dx = 50;
dy = 50;

// Cosinus and Sinus are needed
cos = Cos(angle);
sin = Sin(angle);

// Calculate rotation
x = cos * dx - sin * dy + Owner.x;
y = cos * dy + sin * dx + Owner.y;

You can also check Wikipedia article about rotation.