Rotating a vector to another vector has been asked numerous times on this forum according to my search but none of the answers seem to directly help me with my problem, I understand the theory (almost) but I can't get the results, anyway here goes, I am using c# and managed directx:
I have a textured quad defined as follows
verts[0] = new CustomVertex.PositionTextured(0.0f, 1.0f, 0.0f, 0.0f, 0.0f);
verts[1] = new CustomVertex.PositionTextured(0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
verts[2] = new CustomVertex.PositionTextured(1.0f, 0.0f, 0.0f, 1.0f, 1.0f);
verts[3] = new CustomVertex.PositionTextured(1.0f, 1.0f, 0.0f, 0.0f, 1.0f);
laserDirection is the direction my quad is facing, I assume the direction could be either on in the x or y direction considering how its defined above, i could be wrong, here is the code that constructs the rotation matrix to rotate the laserDirection to my desired direction:
_Position = startPos;
// The direction vector of the shot
Vector3 shootDirection = endPos - startPos;
// the current direction of our laser quad
Vector3 laserDirection = new Vector3(0,1,0);
// store the length of the shot direction (to scale our quad to length)
_Length = shootDirection.Length();
// normalize our shot direction
shootDirection.Normalize();
Vector3 rotAxis = Vector3.Cross(shootDirection,laserDirection);
rotAxis.Normalize();
float rotAngle = (float) Math.Acos(Vector3.Dot(shootDirection,laserDirection));
_WorldTransform = Matrix.RotationAxis(rotAxis,rotAngle);
Now here is my final transformation:
Matrix worldMatrix = Matrix.Scaling(1,_Length,1) * _WorldTransform * Matrix.Translation(_Position);
For some reason my laser never points in the direction I expect it to, I must be doing something wrong but I am not sure what, the code looks fine to me, anyone see if I am doing something totally wrong?
regards
Fluxtah
A wise man can learn more from a foolish question, than a fool can learn from a wise answer - Bruce Lee