#### Archived

This topic is now archived and is closed to further replies.

# Vector -> Angle

This topic is 5239 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, could please anyone help me with my problem? It should be simple but I can''t solve it. Believe me, I really tried it. I also would have used the search function, but it seems not to work... Problem is, that I need to get the yaw/pitch/roll of a normalized normal vector. I need that, to rotate a model to the same direction, the normal is pointing. Please dont tell me anything about using matrices and stuff, I simply need the yaw pitch roll degree vars from 0° to 360°. So that I can rotate my model using D3DXMatrixRotationYawPitchRoll(&matworld, D3DXToRadian(m_selModel->yaw), D3DXToRadian(m_selModel->pitch), D3DXToRadian(m_selModel->roll)) I'', using a standard directx left handed coordinate system (with y pointing to the sky). Please, is there anyone who could help me. I need to solve that problem very fast! Thx!!!

##### Share on other sites
A dot B = mag(A) * mag(B) * cos(AB)
where A and B are vectors and AB is the angle between them.

If you can''t deduce from that, compute the dot product of your normal with the unit x-, y- and z-axis vectors: they will give you the angles between the normal and the respective axes.

##### Share on other sites
Hmm I tried the following

float yaw = acosf(D3DXVec3Dot(&vNormal, &D3DXVECTOR3(0.0f, 1.0f, 0.0f)));
float pitch = acosf(D3DXVec3Dot(&vNormal, &D3DXVECTOR3(1.0f, 0.0f, 0.0f)));
float roll = acosf(D3DXVec3Dot(&vNormal, &D3DXVECTOR3(0.0f, 0.0f, 1.0f)));

Note that vNormal is already normalized...

But that does not gives right results. Am I doing something wrong? (Of course I'm doing something wrong)...

[edited by - To_N on January 17, 2004 11:12:15 AM]

##### Share on other sites
Are you sure you aren't getting screwed up with degrees and radians? Obviously the method you are using gives you radians, but perhaps you are somehow using degrees elsewhere?

EDIT:
I potentially see some error in your thinking as well, I could be wrong but I'm throwing this out there anyway:

float yaw = acosf(D3DXVec3Dot(&vNormal, &D3DXVECTOR3(0.0f, 1.0f, 0.0f)));

That seems like it is incorrect to me, remember Yaw is the angle about the Y axis, and the 'origin' vector is typically the x axis. So, it seems it should be:

float yaw = acosf(D3DXVec3Dot(&vNormal,&D3DXVECTOR3(1.0f,0,0)));
or simply
float yaw = acosf(&vNormal.x);

[edited by - Shadow12345 on January 17, 2004 4:39:25 PM]

##### Share on other sites
Hm, nope I''m pretty shure that I don''t getting screwed up with degrees and radians.
float yaw = acosf(vNormal.x);
float pitch = acosf(vNormal.y);
float roll = acosf(vNormal.z);

but it also wont work.
For a normal on a flat plane with 0.0f, 1.0f, 0.0f (whitch is straight pointing to the sky) it calculates
yaw = 90°
pitch = 0°
roll = 90°

but this is wrong. Yaw, the rotation around the y - axis in my opinion can not be calculated with a single normal vector (there is no need for that also).
Pitch, the rotation around the x axis and roll, the rotation around the z axis should be 0° for a good result.
So a normal 0.0f, 1.0f, 0.0f should give the following results:
yaw = something between 0° and 360°
pitch = 0°
roll = 0°

I simply dont understand what I''m doing wrong...
Thx

##### Share on other sites
I would agree that you don't understand what you're doing. Unfortunately, neither do I... but I do think you could accomplish what you're trying to do without finding the yaw, pitch and roll of the vector you want your model aligned to.

From Andypike.com, I located the following bit of code...

void SetupRotation(){    //Here we will rotate our world around the x, y and z axis.    D3DXMATRIX matWorld, matWorldX, matWorldY, matWorldZ;        //Create the transformation matrices    D3DXMatrixRotationX(&matWorldX, timeGetTime()/400.0f);    D3DXMatrixRotationY(&matWorldY, timeGetTime()/400.0f);    D3DXMatrixRotationZ(&matWorldZ, timeGetTime()/400.0f);    //Combine the transformations by multiplying them together    D3DXMatrixMultiply(&matWorld, &matWorldX, &matWorldY);    D3DXMatrixMultiply(&matWorld, &matWorld, &matWorldZ);    //Apply the tansformation    g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld);}

which you might consider trying a variation of. I may have done this myself, but it's been a while. Instead of converting to yaw, pitch and roll, you convert to rotations about the x, y and z axes, which is simpler... I would approach it by assuming your vector to be the look direction, and assuming your "up" vector to be aligned with the y axis... (you need two vectors to specify both direction to look and rotation about the vector along which you're looking, which is roll). Then project your lookat vector onto the xz plane and get the angle between that and the x (or perhaps z?) axis, which should be your yaw... the pitch could be determined by finding the angle between the vector and the xz plane.

Alternatively, I'm pretty sure there's a D3DXMatrixLookAt() function this would do this all automatically...

[edited by - Geoff the Medio on January 18, 2004 2:49:48 PM]

##### Share on other sites
to_n, you are doing something wrong, the normal vector (0,1,0) cannot have any yaw. Here's a piece of code that I wrote for calculating the yaw so that the monster (bert in this case) would always face the player. Note that when I calculated the yaw vector, I left out the y component:

	Vector3	toplayer(Export.mPlayer.mPosition.x-mPosition.x,0,Export.mPlayer.mPosition.z-mPosition.z);	toplayer.Normalize();		mYaw	=	RAD2DEG(acosf(toplayer.z)); 		if(Export.mPlayer.mPosition.x < mPosition.x)		mYaw = -mYaw;		mModel.Draw(&mPosition,0,mYaw,0);

toplayer only has x and z components, but the normal vector you are trying to calculate the y for only has a y component, and yaw doesn't apply nor make sense.

EDIT: and, not to confuse you, I used toplayer.z in this case because the z axis is my origin vector instead of my x axis (because my models all start facing down the z axis)

[edited by - Shadow12345 on January 18, 2004 6:21:11 PM]

##### Share on other sites
I new realized that matrices are the way to go.
Finally I''m using D3DXMatrixLookAtLH function, which seems to work properly. The only problem is, that my model seems to point with the Z-Direction at the target. What I want, is the model to point with the Y direction at the target. Question1: How is that possible?

My aim is to create a right rotation matrix with D3DXMatrixLookAtLH, and to extract the euler angles from trat matrix. Question2: How can I do that?

Thx you all for your help and patience

##### Share on other sites
Ok, forget about the first question. I solved it by multiplying the "LookAt" matrix with a "XRotation" matrix of 90 degrees.
Now I need to solve that second question...

• 16
• 9
• 13
• 41
• 15