Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

This question already has an answer here:

Heres a picture that explains better than my words can

http://weskamouse.net/help.png

For rotating an object I'm using quaternions. CreateFromAxisAngle(vector3.Right,yaw)

Maybe this isn't how I should be doing it but I also keep my yaw as radians that are always between 0 and 360;more or less than 0 or 360 puts it at 0 or 360 respectively.

(object is build up of vertices and indices)

I can rotate it no problem. Move it all about. But what I really want to do is to find how much I should alter the yaw so that the my models right vector will sync up with the line I can draw between the objects center and the camera's position(y is always 0).

Which I guess is the problem now, but long term I am trying to find how I modify the yaw to match any direction.

Say I am travelling Left, I may want to rotate my yaw so that my objects forward is also left.

I have been researching here and elsewhere and there seem to be solutions, I just can't wrap my head around any. Maybe someone could help shed some light?

share|improve this question

marked as duplicate by Josh Petrie, Sean Middleditch, Tetrad Feb 26 '13 at 3:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
From your comment ".. so that it faces a direction" it sounds like my answer to this question: "Orienting a model to face a target" might be what you are looking for. It is using quaternions and XNA code. –  Maik Semder Jan 26 '13 at 11:44
    
Sorry, I missed this comment. This is precisely what I was going for with my answer. –  RobCurr Feb 25 '13 at 16:16

4 Answers 4

XNA makes this surprisingly easy with Matrix.CreateBillboard and Matrix.CreateConstrainedBillboard. The first method returns a matrix with the orientation determined by the input vector you choose, which is where you use the camera's lookAt vector. Simple enough. The second method returns a matrix with a constrained orientation- that is, it's limited to one of three axes (it can be an arbitrary axis, not limited to the orthogonal directions).

You probably want to use the latter. The Rotate axis should be Vector3.Up for yaw. The resulting matrix can be applied directly to your object's transformation.

share|improve this answer
    
Very cool. Never even knew that function existed. I might have to revisit my code to utilize that instead of my own computation. –  RobCurr Feb 25 '13 at 21:08

I'm not really sure I understand your question. But maybe what you're trying to do is billboarding?

If so, the only thing you have to do is make sure each sprite points to the camera. If both your camera and the sprites use the same coordinate system, the Y rotation for your object can either be the inverse of the Y rotation for your camera (for planar billboarding), or the angle between the camera and the object, whose cosine is the dot product between the normalized vector between the camera and the object, and the normalized vector of the camera's forward vector (for cylindrical billboarding)

share|improve this answer
    
Well im not using sprites. I'm trying to rotate the object using quaternion rotation. I want to know what I should add or subtract to my yaw var so that it faces a direction( such as the camera or anything). I guess I'm asking it in a complicated way I'm going to relax and come back to the problem tomorrow and see if I can figure it all out –  Theodore Enderby Jan 26 '13 at 8:01

If you normalize the vector that represent Right(Rn) and you normalize the vector resulting from the subtraction of the objects position from the camera position(Cn) then you can use the dot product to determine the angle that you want to apply to your Yaw.

Rn . Cn = |Rn|x|Cn| cos a

Since Rn and Cn are normalized |Rn|x|Cn| = 1 and this reduces to Rn.Cn = cos a

so (Rn.X)(Cn.X) + (Rn.Y)(Cn.Y) + (Rn.Z)(Cn.Z) = cos a

The only gotcha with this approach is that there are two possibilities that you have to be able to identify and handle due to the fact that cosine is defined between 0 and 180(0 and Pi). In order to handle this you have to take the crossproduct of the two vectors and check the results Y component to determine if you should use a negative 'a' or a positive 'a' in your yaw.

share|improve this answer

You should use

Matrix.CreateWorld(Position, Forward, Up) to create the right matrix...

where forward is the vector you want to face...

http://msdn.microsoft.com/en-us/library/bb975261.aspx

I think you don't need it, but if you want to get the yaw from that matrix...

you can use the matrix method

transform.Decompose(out scale, out rotation, out translation)...

and then you can get the axis rotation values from the quaternion you can extract it with this function

public static Vector3 QuaternionToEulerAngleVector3( this Quaternion rotation )
    {
        Vector3 rotationaxes = new Vector3( );
        Vector3 forward = Vector3.Transform( Vector3.Forward, rotation );
        Vector3 up = Vector3.Transform( Vector3.Up, rotation );

        rotationaxes = AngleTo( new Vector3( ), forward );

        if ( rotationaxes.X == MathHelper.PiOver2 )
        {
            rotationaxes.Y = ( float ) Math.Atan2( ( double ) up.X, ( double ) up.Z );
            rotationaxes.Z = 0;
        }
        else if ( rotationaxes.X == -MathHelper.PiOver2 )
        {
            rotationaxes.Y = ( float ) Math.Atan2( ( double ) -up.X, ( double ) -up.Z );
            rotationaxes.Z = 0;
        }
        else
        {
            up = Vector3.Transform( up, Matrix.CreateRotationY( -rotationaxes.Y ) );
            up = Vector3.Transform( up, Matrix.CreateRotationX( -rotationaxes.X ) );

            rotationaxes.Z = ( float ) Math.Atan2( ( double ) -up.Z, ( double ) up.Y );
        }

        return rotationaxes;
    }
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.