5

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?

Bonfire Dog
  • 285
  • 5
  • 19
  • 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. – Bonfire Dog Nov 23 '15 at 21:51

2 Answers2

3

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.

madneon
  • 295
  • 1
  • 2
  • 12
2

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

Dmi7ry
  • 1,050
  • 2
  • 8
  • 13
  • 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. – Bonfire Dog 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 – Dmi7ry Nov 25 '15 at 04: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. – Bonfire Dog Nov 26 '15 at 10:04
  • x = Owner.x - lengthdir_x(34, Owner.image_angle) – Bonfire Dog Nov 26 '15 at 10:05
  • y = Owner.y + lengthdir_y(192, Owner.image_angle) – Bonfire Dog Nov 26 '15 at 10:06
  • It's not right.It's not delta of each coordinate. Here must be same distance and angle. If you don't know values, you can calculate it like dist = point_distance(0, 0, delta_x, delta_y); angle = point_direction(0, 0, delta_x, delta_y);. And usage: x = Owner.x + lengthdir_x(dist, angle + Owner.image_angle); y = Owner.y + lengthdir_y(dist, angle + Owner.image_angle); – Dmi7ry Nov 26 '15 at 14:07
  • Thanks Dmi7ry, but I'm a bit confused; there doesn't seem to be any such value in Gamemaker as delta_x or delta_y... – Bonfire Dog Dec 13 '15 at 21:38
  • You need use numbers instead delta_x and delta_y. See example https://www.dropbox.com/s/2n4run1nby5207b/RelativePosition.gmz?dl=0 – Dmi7ry Dec 14 '15 at 07:19
  • I see! So I use the values of the distance from the sprite's origin? I think the problem was that I wasn't using -34, but rather 34. Your solution works, thanks again! – Bonfire Dog Dec 15 '15 at 17:23