Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate rotations to look at a 3D point?

I need to calculate the 2 angles (yaw and pitch) for a 3D object to face an arbitrary 3D point. These rotations are known as "Euler" rotations simply because after the first rotation, (lets say Z, based on the picture below) the Y axis also rotates with the object.

This is the code I'm using but its not working fully. When on the ground plane (Y = 0) the object correctly rotates to face the point, but as soon as I move the point upwards in Y, the rotations don't look correct.

// x, y, z represent a fractional value between -[1] and [1]
// a "unit vector" of the point I need to rotate towards

yaw = Math.atan2( y, x )
pitch = Math.atan2( z, Math.sqrt( x * x + y * y ) )

Do you know how to calculate the 2 Euler angles given a point?


The picture below shows the way I rotate. These are the angles I need to calculate. (The only difference is I'm rotating the object in the order X,Y,Z and not Z,Y,X)

pic

enter image description here


This is my system.

  • coordinate system is x = to the right, y = downwards, z = further back
  • an object is by default at (0,0,1) which is facing backward
  • rotations are in the order X, Y, Z where rotation upon X is pitch, Y is yaw and Z is roll

my system

like image 656
Robin Rodricks Avatar asked Aug 09 '09 17:08

Robin Rodricks


1 Answers

Rich Seller's answer shows you how to rotate a point from one 3-D coordinate system to another system, given a set of Euler angles describing the rotation between the two coordinate systems.

But it sounds like you're asking for something different:

You have: 3-D coordinates of a single point

You want: a set of Euler angles

If that's what you're asking for, you don't have enough information. To find the Euler angles, you'd need coordinates of at least two points, in both coordinate systems, to determine the rotation from one coordinate system into the other.

You should also be aware that Euler angles can be ambiguous: Rich's answer assumes the rotations are applied to Z, then X', then Z', but that's not standardized. If you have to interoperate with some other code using Euler angles, you need to make sure you're using the same convention.

You might want to consider using rotation matrices or quaternions instead of Euler angles.

like image 190
Jim Lewis Avatar answered Oct 12 '22 13:10

Jim Lewis