OK, if you have a direction ‘ToN’ and the direction ‘FromN’, and you want to find the angle/axis rotation that transforms ‘FromN’ into ‘ToN’, do the following:
BTW, I don’t have my linear algebra book with me, but I seem to recall something about using the arcsin directly from the cross product to get the angle, but I’m not certain.
I think my misttake is in the VectorSubstract
the vector(1,1,1) is the point I want to “look at” but its not correctly working, sometimes it even screw my rotation up.
Well I tried around, nothing helps, maybe its the best I paste my current code here, its VB
Dim toN As tagVECTOR, FromN As tagVECTOR
Dim Axis As tagVECTOR, Angle As GLfloat
FromN = Camera.m_Quat.GetDirectionVector
FromN = VectorNormalize(FromN)
toN = VectorSubstract(Vector(Camera.PosX, Camera.PosY, Camera.PosZ), Vector(1, 1, 1))
toN = VectorNormalize(toN)
Axis = VectorMultiply(FromN, toN)
Angle = ArcCos(VectorDotProduct(FromN, toN))
Debug.Print Angle, Axis.x, Axis.Y, Axis.Z
Debug.Print VectorLength(toN) & " " & "ToN" & " " & VectorLength(FromN) & " " & "FromN"
' Camera.m_Quat.ResetQuaternion
Dim aquat As New CQuaternion
aquat.SetAngleXYZ Angle, Axis.x, Axis.Y, Axis.Z
Camera.m_Quat.PostMult aquat
the ’ = //
rest should be clear
the main problem is: sometimes this screws my rotation to the object up, but if im near to it, Iam smoothly turning to it.
s.th. must be wrong with the way i calculate my normal to the vector I want to look at, or s.th. may be wrong with the cross product…
Zadkiel is right. I had similar troubles when trying to convert points into different coordinate systems. Examination of the matrices revealed strange behavior.
A function that converted the Degrees to Radians solved this problem.
Oops, my mistake… Yes, you need to have a function the other way around.
I needed to compute a matrix and my angles were specified in degrees, so I had to convert to Rad.
This might be a bit OT, but I’m wondering.
Is it better to query OpenGL for a matrix, or to compute the matrices yourself?
The red book doesn’t say much about this (I might have missed something though, and I feel it is better to compute them yourself).
What it does say is that it is better to use glRotate* instead of glMultMatrix or glLoadMatrix. It fails to explain why though. Performance reasons? Surely storing a matrix is cheaper than performing three matrix calculations?
Ritchie
[This message has been edited by Ritchie (edited 08-30-2001).]