# Calculating rotation from direction vector

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

## Recommended Posts

I have a class that draws an arrow. The red one (X) is the default as created in the vertex buffer. The blue one (Y) is rotated over the z axis and the green one (Z) over the y axis with D3DXMatrixRotationZ and D3DXMatrixRotationY respectively. Now when I want to rotate over more than 1 axis, the rotation is all wrong. I want to draw an arrow based on a direction vector. In this case, the yellow one, that's D3DXVECTOR3(1,1,1). As you can see, the arrow point isn't where it should be. I calculate the x,y and z angles between the original vector D3DXVECTOR3(1,0,0) and the direction vector and the result is how it should be, the yaw, pitch and roll values are all 0.78f (PI/4) so 45°. But still the arrow point is wrong. Here's my code:
void Arrow::Draw(D3DXVECTOR3 origin, D3DXVECTOR3 direction, float length, D3DXCOLOR color)
{
//Normalize direction
D3DXVec3Normalize(&direction, &direction);

D3DXVECTOR3 destination = origin + (direction * length);

// Render line
m_Line->Draw(origin,destination,color);

D3DXMATRIX world;
D3DXMATRIX rotation;

D3DXMatrixIdentity(&world);

// Normalized so the length is 1
roll = asin(direction.y);
yaw = -(asin(direction.z));
pitch = asin(direction.y);

D3DXMatrixRotationYawPitchRoll(&world,yaw,pitch,roll);
}

What am I doing wrong?? Thanks in advance.

##### Share on other sites
Is that the entire function? You're declaring the world and rotation matrices, but it doesn't look like you ever actually use them for anything.

Also, I'm not sure that your code for computing the angles of rotation for the arrow head (I assume that's what you're trying to do there) is correct. In this context, you generally only need two angles, not three (for more info, try Googling 'spherical angles').

##### Share on other sites
Well no, the rest of the method is just setting the world view and projection matrices to the shader and drawing it, so I left that out.

Thanks for the tip, I was able to fix it with this article: http://en.citizendium.org/wiki/Spherical_polar_coordinates

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631434
• Total Posts
3000051
×