How do you rotate a 3D vector by XYZ corrdinates without using matrices or any of that junk?
Printable View
How do you rotate a 3D vector by XYZ corrdinates without using matrices or any of that junk?
It's not junk, it's a very fast way to do transformations. who limited you from not using them?
Ok then, how do you rotate a vector by XYZ corridenates around a certain location (with matrices or not ;p)? Also, you do you rotate a XYZ vector by a quaternion? Hey, I've only had Algebra I! Thnx in advance :)
here's a tutorial for you:
http://www.manning.com/Lussier/Chapter3.html
Ok... Let's see if I got this right: Take a matrix, rotate it by ur xyz corridinates, and then multiply it by ur original vector. It that right?
that is if you need to do a transformation. X x Y x Z can be done in one step and then transform, any vector you multiply with the result will be rotated in respect to the transformation.
This is in C but the theory's sound...
Where m_qTotal is a rotation quaternion.Code:void BuildRotMatrix() {
m_pfMatrix[0][0] = 1.0 - 2.0 * (m_qTotal[1] * m_qTotal[1] + m_qTotal[2] * m_qTotal[2]);
m_pfMatrix[0][1] = 2.0 * (m_qTotal[0] * m_qTotal[1] - m_qTotal[2] * m_qTotal[3]);
m_pfMatrix[0][2] = 2.0 * (m_qTotal[2] * m_qTotal[0] + m_qTotal[1] * m_qTotal[3]);
m_pfMatrix[0][3] = 0.0;
m_pfMatrix[1][0] = 2.0 * (m_qTotal[0] * m_qTotal[1] + m_qTotal[2] * m_qTotal[3]);
m_pfMatrix[1][1]= 1.0 - 2.0 * (m_qTotal[2] * m_qTotal[2] + m_qTotal[0] * m_qTotal[0]);
m_pfMatrix[1][2] = 2.0 * (m_qTotal[1] * m_qTotal[2] - m_qTotal[0] * m_qTotal[3]);
m_pfMatrix[1][3] = 0.0;
m_pfMatrix[2][0] = 2.0 * (m_qTotal[2] * m_qTotal[0] - m_qTotal[1] * m_qTotal[3]);
m_pfMatrix[2][1] = 2.0 * (m_qTotal[1] * m_qTotal[2] + m_qTotal[0] * m_qTotal[3]);
m_pfMatrix[2][2] = 1.0 - 2.0 * (m_qTotal[1] * m_qTotal[1] + m_qTotal[0] * m_qTotal[0]);
m_pfMatrix[2][3] = 0.0;
m_pfMatrix[3][0] = 0.0;
m_pfMatrix[3][1] = 0.0;
m_pfMatrix[3][2] = 0.0;
m_pfMatrix[3][3] = 1.0;
}
Well I coded a little sub, but it won't rotate anything:
Whats Wrong? BTW, Thx Parksie for the sub, but is there a way I can rotate a vector by a quaternion(luv that word :D !)without having to make a matrix out of it first? That seems kind of slow...Code:Function RotateVector(ByRef VectorIn As D3DVECTOR, X As Single, Y As Single, Z As Single) As D3DVECTOR
Dim Matrix(1 To 2, 1 To 3) As Single
'X rotation
Matrix(1, 1) = VectorIn.X
Matrix(2, 1) = 0
Matrix(1, 2) = Cos(X) * VectorIn.Y
Matrix(2, 2) = -Sin(X) * VectorIn.Z
Matrix(1, 3) = Sin(X) * VectorIn.Y
Matrix(2, 3) = Cos(X) * VectorIn.Z
VectorIn.X = Matrix(1, 1) + Matrix(2, 1)
VectorIn.Y = Matrix(1, 2) + Matrix(2, 2)
VectorIn.Z = Matrix(1, 3) + Matrix(2, 3)
'y rotation
Matrix(1, 1) = Cos(Y) * VectorIn.X
Matrix(2, 1) = -Sin(Y) * VectorIn.Z
Matrix(1, 2) = VectorIn.Y
Matrix(2, 2) = 0
Matrix(1, 3) = -Sin(Y) * VectorIn.X
Matrix(2, 3) = Cos(Y) * VectorIn.Z
VectorIn.X = Matrix(1, 1) + Matrix(2, 1)
VectorIn.Y = Matrix(1, 2) + Matrix(2, 2)
VectorIn.Z = Matrix(1, 3) + Matrix(2, 3)
'z rotation
Matrix(1, 1) = Cos(Z) * VectorIn.X
Matrix(2, 1) = -Sin(Z) * VectorIn.Y
Matrix(1, 2) = Sin(Z) * VectorIn.X
Matrix(2, 2) = Cos(Z) * VectorIn.Y
Matrix(1, 3) = VectorIn.Z
Matrix(2, 3) = 0
VectorIn.X = Matrix(1, 1) + Matrix(2, 1)
VectorIn.Y = Matrix(1, 2) + Matrix(2, 2)
VectorIn.Z = Matrix(1, 3) + Matrix(2, 3)
RotateVector = VectorIn
End Function
looks like you don't care about preserving the matrix, you could use this directly then:
http://www.manning.com/Lussier/oth0326.gif
there's no need for quaternions if you don't have combinations of several rotation transformations.
Well, I cam up with this:
But it still doesn't work!? Thanks guys for your help.Code:Function RotateVector(ByRef VectorIn As D3DVECTOR, X As Single, Y As Single, Z As Single) As D3DVECTOR
Dim Temp(10) As Single
Dim xx As Single, yy As Single, zz As Single
yy = VectorIn.Y
xx = VectorIn.X
zz = VectorIn.Z
Temp(0) = Sin(X)
Temp(1) = Cos(X)
Temp(2) = Sin(Y)
Temp(3) = Cos(Y)
Temp(4) = Sin(Z)
Temp(5) = Cos(Z)
Temp(6) = yy * Temp(5)
Temp(7) = xx * Temp(4)
Temp(8) = Temp(5) * xx
Temp(9) = Temp(4) * yy
Temp(10) = Temp(3) * zz
RotateVector.X = (Temp(3) * (Temp(8) - Temp(9)) + Temp(2) * Z)
RotateVector.Y = (Temp(0) * (Temp(2) * Temp(8) - Temp(2) * _
Temp(9) - Temp(10)) + Temp(1) * (Temp(7) + Temp(6)))
RotateVector.Z = (Temp(1) * (-Temp(2) * Temp(8) + Temp(2) * _
Temp(9) + Temp(10)) + Temp(0) * (Temp(7) + Temp(6)))
End Function
don't use arrays when unnessesary. replace that Z with ZZ for the X component expression.
The trigonometric functions in VB (actually, in nearly all programming languages) accept angles in radians. If you use degrees you'll be waaaay out :(.
To convert from degrees to radians multiply by (pi / 180).
so if the angle isn't specified by the user, screw degrees and store radians.