Sign in to follow this  
Bosduif

Calculating rotation from direction vector

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. img 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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this