Pointing an object towards another, in 3d

I'm finally getting back to my XNA series of posts, which will eventually lead to the release of the - cleaned up - source code of my little game (I just hope that I'll be able to do so before XNA 6.0 comes out...).

Anyway, one of the truly biggest problems I had while developing the game, was to figure out HOW exactly to point an object towards another point in space. This sounds really basic, probably is, but my math skills are so weak that I had to think about it almost an entire day before finding a solution. Perhaps this will be useful to similarly counfounded people, like me.  :D

The main problem is that you are trying to orient something along a vector, which starts in one tridimensional point and ends in another one. These two points in space are sufficient to give us a simple directional vector (the distance between the two points), but not enough for a full translation matrix (or a rotational matrix, in this case). This is because there are infinite positions in which the object we are trying to rotate could be in, while still pointing to the correct destination point. To exemplificate:

Awful scheme of the problem.

Yes, I know the drawing is extremely ugly.  :) You try doing something better with Paint.NET!

Anyway, assume the two balls are the two points I was talking about and the black thick line is the distance vector between them. Then, the disc around the red ball (or that what should look like a disc) is the plane on which all possible "up" vectors of the object could be, while still having its nose pointed straight at the blue ball.

The problem thus is that we have too much "correct" solutions: but we must decide for one (and consistently). Therefore, the simple way to do it is to simply pick a completely arbitrary vector:

//Get distance between points
Vector3 dist = ballBlue.Position - ballRed.Position;

//Get angle to arbitrary vector and compute the rotation axis
float theta = (float)Math.Acos(Vector3.Dot(dist, Vector3.Up));
Vector3 cross = Vector3.Cross(Vector3.Up, dist);

Quaternion rotation = Quaternion.CreateFromAxisAngle(cross, theta);

The code is pretty straightforward: first of all you compute theta, the angle between the distance vector and the arbitrary vector (in this case I chose the up vector, but you can use whichever makes more sense in your case). Then we compute the cross product between those two vector and normalize it: this will yield a nice vector (one of those on the "correct" plane I mentioned above) that can be used to generate a rotation quaternion or a matrix.

The result in my game: notice the arrow pointing towards the golden point in space.

And this is the result in my game: notice the golden "thing" above the spaceship? It's a green arrow that points exactly towards the yellow point in space on the right.