>Firstly I think i'll move into using quarternions in the future. For
>now i'll try to make sense of Eulers - I have a lot to learn still.
>Matt, I don't think the code you supplied will help me too much. It's
>geared towards rotation an object to orient it in the same direction
>as another object's orientation. I want to do something different, to
>aim an object at another - regardless of the direction that 2nd object
>I think I need to project my aim vector onto 3 vectors representing
>the axis, and then calculate the dot product with each rotation with
>each. Does this seem reasonable?
I know it's tempting to think Euler angles are the simplest approach,
so save the more complicated ideas like quaternions for later. It just
isn't true. A much better intermediate step is to keep only one angle
but also specify an axis vector. (See the end of my post for how to
solve your problem this way.)
Here's why. In 2D all rotations are planar, because the whole space is
just a plane. In 3D all rotations are planar, though the reason is
more subtle, and we have many planes to choose from. (In 4D and beyond
all rotations are *not* planar.) Euler angles artificially cut up this
single planar turn into three somewhat arbitrary axis-aligned turns.
Again and again in many different ways that dissection proves awkward.
In 2D we have two coordinate axes, but only one rotation angle. That
should give you a hint. In 3D we have three coordinate axes and three
Euler angles, but that is accidental and Euler angles do not act at
all like vectors! (In 4D we have four axes and six angles.) Although
in 2D you can reverse a rotation by negating that single angle, no
such simple prescription works with 3D Euler angles; it *does* work
with axis-angle. In 2D we can combine two rotations by adding their
angles, but in 3D with Euler angles that completely fails.
There is nothing wrong with learning Euler angles; I encourage you to
do so. Just don't delude yourself into thinking that's the simple way
Specifically, your proposal for computing the three angles is *not*
correct. A correct calculation is much more complicated to describe,
and involves the fact that Euler angles describe rotations in sequence
with a critical dependence on order. You are naively ignoring the need
to consider the sequential aspect.
Your dismissal of Matt's code is also not correct. He gives what is
essentially an axis-angle approach to do exactly what you want, if you
use it properly. He *does not* orient one object to match another. I'm
sure he expected you to understand how his code applies to your needs;
I will be more explicit.
Here's the setup. Some vector in the object you want to aim acts as a
"nose direction". In 2D you took that to be the +y unit vector (0,1),
and in 3D you seem happy to again use +y vector (0,1,0). You calculate
a desired aim vector U as a difference of object centers, normalized,
and that is correct. Now let's look at where you went wrong.
In 3D you *must* consider the plane of rotation along with the angle.
The angle between Y and U is still to be found in their dot product,
but only if you rotate *in their plane*. In axis-angle form we give
the plane of rotation by a perpendicular vector, the axis. As Matt
told you, the cross product produces it. The rotation axis will not
usually be a coordinate axis; it will be some arbitrary direction.
To summarize, you want a rotation by angle
angle = arccos(Y.U)
and axis (which you must normalize)
axis = Y x U
Be careful, because the axis calculation can give a zero vector. In
that case either you are already aiming correctly, or you must look
directly behind you by rotating 180 degrees around z. Also be careful
that you are not positioned on the point at which you wish to aim!