# Rotating a billboard to the direction of a vector

## Recommended Posts

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

##### Share on other sites
I seemed to have got a bit further now, it points in the right direction when I negate the rotation angle:

_WorldTransform = Matrix.RotationAxis(rotAxis,-rotAngle);

If anyone can still shed some light on this I would appreciate it, all I need to do now is make the quad always face the camera :)

##### Share on other sites
It's probably a confusion in the definition of your coordinate system. Are you using left handed or right handed? If you negate the axis instead of the angle, does it still work correctly? If so, it's just that you need to reverse the cross product parameters. Picture it in your head to convince yourself that it's all defined right.

##### Share on other sites
Hi again,

Sorry to keep bumping this thread but I still need a little help on this, I will elaborate on what I am doing.

I am making a laser beam from a quad scaled to the distance of ship to the target and then rotated to the shooting direction, the only problem I have now is making the quad roughly face the camera.

I know that I should be rotating it on the axis of the shooting direction but I am not sure what angle, or how to obtain a suitable angle to rotate it to.

I was thinking of using the Right axis of the camera and the Right axis of the quad, taking the dot product of them and then inverse cosine to get the angle between them, then rotate the quad along the shooting direction by that angle.

not sure.. will try when I can get to my PC at home but if anyone has any ideas on this please let me know, how is it ussualy done, and are there any good tutorials that explain how to make laser beams?

Thanks!

Fluxtah

EDIT: Thanks for the reply Supernat02, I am using left handed, I think I need to refer to my 3D math book, I had no idea that it mattered which way round the vectors where when taking a cross product!

##### Share on other sites
I am getting the picture in my head that you are above the ship, looking down, and it's firing a laser beam toward a target. You just want to know how to make the quad face you (the camera). Right?

Maybe a more simple solution is to use a cube.

I haven't tried this, but who knows.

If you take the two vectors:

you can dot the camToMid vector onto the stToEnd and determine the angle between them.

Then temporarily rotate the stToEnd vector by that many degrees so that it lies parallel to the view vector (use the cross product of the two vectors to determine the rotation axis like you have done before). Now calculate the normal of the stToEnd that would be required for the quad to face the camera (i.e. the normal dot'd with the camera normal should give an angle of 0). Find the error between the current normal and your calculated normal, then rotate by that much. Finally rotate back into the shooting axis by the negative of the first rotation you did (between camToMid and stToEnd).

Now I'm curious. I may try this later on. I have no idea if it will work, and there's probably a MUCH easier way to do it that I'm not thinking of yet. :)

[Edited by - Supernat02 on November 8, 2005 3:54:44 PM]

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628308
• Total Posts
2981973

• 9
• 13
• 11
• 12
• 11