5
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – MAnd Nov 23 '15 at 21:39
  • \$\begingroup\$ 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. \$\endgroup\$ – Bonfire Dog Nov 23 '15 at 21:51
2
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Bonfire Dog Nov 24 '15 at 23:13
  • \$\begingroup\$ 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 \$\endgroup\$ – Dmi7ry Nov 25 '15 at 4:15
  • \$\begingroup\$ 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. \$\endgroup\$ – Bonfire Dog Nov 26 '15 at 10:04
  • \$\begingroup\$ x = Owner.x - lengthdir_x(34, Owner.image_angle) \$\endgroup\$ – Bonfire Dog Nov 26 '15 at 10:05
  • \$\begingroup\$ y = Owner.y + lengthdir_y(192, Owner.image_angle) \$\endgroup\$ – Bonfire Dog Nov 26 '15 at 10:06
2
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.