   Aligning an object to a sphere by using a normal
  Usually I'm trying to solve such problems by myself, but my math skills are not the best and I've been trying on this for roughly four days now without getting a good result. I also searched the net and looked up wikipedia for hours with no real result.Here is what I want to do :I've added nuclear explosions to my game (Projekt "W", posted it sometime ago here on the IOTD) and what I now want is to align that explosion so that it explodes "away" from the point where the user want's to start the nuke (if you're interested, ">here is a video to the nuke atop of the globe). So I get the normal on the globe under the selected position and now want to align the nuke (which is a normal particle system) to that normal.I've tired dozens of different methods with none really working. The last one I tried was to use spherical coordinates, and that one at least partially worked (see screenshot here), but especially at the equator it won't work anymore, although it should. After trying to figure out how to do this for days now I hope that some math gurus here at gamedev.net can help me. Oddly I didn't found any info on that topic in this form or elsewhere, though I'm pretty sure that there are games that align objects to a sphere normal somehow.   
  What forces etc. are in your particle system?If your nuke is at the top of the globe, the x axis is (1,0,0), y is (0,1,0), and z is (0,0,1). If you have some vector of (x,y,z), and want to align that to a different point, you need the axises at that point, and then you find the new vector by x*x_axis + y*y_axis + z*z_axis.For example if you have the point on the right edge of the sphere, then the y-axis will be (1,0,0), which is the normal. There can be any number of x/z axises at that point, since wherever you stand on a globe you can turn around, and have any direction be to the "right" of you. Which you pick shouldn't matter, you just have to pick one vector that is perpendicular to your normal, and choose that as the x-axis. Then you find the z-axis as the cross-product of the y and x axises.The following should get an x_axis from a normal.. though there might be better ways =)n = normalf = n.x*n.x + n.z*n.zif(f > 0.0) { f = sqrt(f) x_axis.x = (n.x / f) * abs(n.y) x_axis.z = (n.z / f) * abs(n.y)}else { x_axis.x = 1.0 x_axis.z = 0.0}if(n.y < 0.0) x_axis.y = felse x_axis.y = -f;  Let's talk about the sphere embedded in 3-space.  I won't use spherical coordinates at all.  For simplicity I'll assume a unit sphere centered at the origin.You know how to make an explosion happen at the point (0,0,1) in 3-space, on your sphere.  All you need now is a way to move this to some other point p on the sphere?  All you need to do is rotate all the points around the origin to the correct location?If so, let's find a rotation matrix to do this.First, let me point out that such a rotation matrix is not unique, since you can "spin" your explosion about its central axis freely.  Therefore, one answer is to pick a rotation "randomly."  However, there is also a way to choose one uniquely (though there is no real benefit to doing so, unless it turns out to be computationally cheaper, of if you just plain like determinism).  I'll describe both approaches below.Answer 1 ("random"): 1 - The third column of your rotation matrix is p.  If your sphere does not have unit radius, it's p/||p|| (that is, p normalized). 2 - The first and second columns can be anything you want so long as they (a) are orthonormal to each other and the third column and (b) form a right-handed coordinate system.  Here's one way to do this:Algorithm: Set the third column of R to p/||p||.  Now pick a random unit vector.  Subtract off the component along the third column and normalize the result.  Make this the second column.  To get the first column, compute the cross product of the second and third columns.This will give you a "random" rotation matrix that does what you want.Answer 2 ("closest" rotation):  1 - Let p0=(0,0,1).  The axis of rotation, and the sine of its angle, are given by the cross product of p0 and p (the axis is the normalized cross product of these vectors; the sine of the angle is its magnitude).  2 - Convert this axis-angle representation of the rotation to a rotation matrix using the standard formulae.  3 - Optimization: Note that you never actually need to compute the angle; you only need trig functions thereof, which you get from the dot and cross products.  I could describe this in more detail, but I think this is sufficient...Using either method, you end up with rotation matrix that you can just apply to everything to move it to the correct location.   ;Quote:Original post by EmergentI could describe this in more detail, but I think this is sufficient...Meh, I might as well give the details...Let's do this a little more generally.  Say you want to rotate from point p1 to p2 over the globe.  Here's how.  I'll assume a unit sphere centered at the origin.Let a = normalize(p1 x p2)where 'x' denotes the cross product.  This is the axis of rotation.  Also let v1 = p1 x aand v2 = p2 x a .These are tangent vectors* to a geodesic (shortest path, a great circle) joining p1 to p2.  Then you're looking for the linear transformation that transforms the basis (represented as a matrix whose columns are the basis vectors),B1 = [p1, v1, a]toB2 = [p2, v2, a] .This transformation is represented simply the matrixR = B2 B1^(-1) .Notice that we never talked about angles in any of this.*( I may have the sign wrong here, but if it's backwards for v1 then it's backwards for v2 also and these will cancel... )   ;  Man, that's a great explanation, and since I'm not that good at math I would've never come up with such a solution.Actually I can't thank you enough for posting this, as I just implemented your solution and it's working fine. My explosion now aligns perfect to the globe, no matter where I place it. A thousand thanks!   ;  Cool, glad to be of service.  Also, I just noticed that, since B1 is orthonormal, B1^(-1) = B1^T (i.e., the inverse is the transpose), so really we can just write,R = B2 B1^Twhich is even simpler.   