For example, I have two objects: a & b.

a's position is 12, 16, -2 and b's position is 14, 16, -3

I need to know how to find a relative rotation between them.

By relative rotation I mean this: a's pitch and yaw so a is looking at b.

I have no helping methods like lookingAt() etc. I only know a's x,y,z,yaw,pitch and b's x,y,z.

Any help?

  • 1
    \$\begingroup\$ How do you compute a rotation from these three angles at present? There are multiple different conventions for coordinate system & rotation order that give different results, so we'll need to know what conventions you're working with to offer a good answer. \$\endgroup\$
    – DMGregory
    Dec 20 '18 at 18:36
  • \$\begingroup\$ @DMGregory What do you mean? I have x,y,z,pitch,yaw,(no roll),vec3d \$\endgroup\$
    – GxDD
    Dec 20 '18 at 18:44
  • 1
    \$\begingroup\$ Simple conventions like "which direction do your x, y, and z axes point?" or "when applying a pair of pitch & yaw angles, which one gets applied first, and is it applied relative to the corresponding world axis or the object's local axis? Counter-clockwise?" — not every system agrees about these choices, so we need to know which ones you're working with. \$\endgroup\$
    – DMGregory
    Dec 20 '18 at 19:04
  • \$\begingroup\$ @GxDD Wecome to GDSE. As an example of what DMGregory is talking about, find something & and aim your camera/phone at it. Next, keeping the object in focus at the same position on the screen, rotate the camera so that the picture is in landscape mode. The position of your camera & the focal point were the same in both cases, but the rotations were not. And there's not always an obvious orientation: imagine taking a photo exactly overhead - which orientation is the right one? \$\endgroup\$
    – Pikalek
    Dec 21 '18 at 15:24

Assuming that:

  1. +Z is up in your world.
  2. Your objects look in the direction of +X.
  3. No roll.

And vector delta is posB - posA...

Then object a first needs to yaw (around Z) with angle atan2f( delta.y, delta.x ).

And then object a needs to tilt with angle -asin( delta.normalized().z ).

As DMGregory mentioned, this all changes if you have different conventions in your world (Which axis is up, what is rotation sequence, left-handed coordinate systems or right-handed ones.)

Aside from this, note that it is often beneficial not to use euler angles, and just set a's transformation matrix by choosing x-axis as normalized view dir, y-axis is cross between (0,0,1) and this x-axis, normalized. And then z-axis as cross between x and y. After setting the translation component to the position, the resulting matrix can be used for object a.

  • \$\begingroup\$ Wait, I realized just now, Y is up in my world, X is left-right and Z is backward-forward \$\endgroup\$
    – GxDD
    Dec 21 '18 at 15:21
  • \$\begingroup\$ @GxDD That would be important info to add to your question. When editing, you should also indicate whether Z+ moves forward or backward. \$\endgroup\$
    – Pikalek
    Dec 21 '18 at 18:50

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.