Jump to content
  • Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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

		D3DXMATRIX world;
		D3DXMATRIX rotation;


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

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!