Archived

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

Fastest vector angle method

This topic is 5147 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

Right now I''m calculating vector angles using dot product, like this: vRL=D3DXVec3Length(&vR); vSunL=D3DXVec3Length(&vSun); fDot= D3DXVec3Dot(&vSun,&vR)/(vSunL*vRL); fAngle=float(acos(fDot)); Is there a more efficent, faster or easier way...

Share this post


Link to post
Share on other sites
Also, the angle given is always positive. I am rolling a ship and need to know if it should tilt left or right, so is there a way to tell if it''s destination vector is left or right.

Right now if its destination vector is 90deg to left I get angle=+pi/2, but if it''s destination vector is 90deg to the right I also get angle=+pi/2....

Share this post


Link to post
Share on other sites
I don''t know of a faster way to get the angle between two vectors, but maybe somebody else does.

As to your other question, I''m not completely clear on what you''re trying to do, but you''re right that just getting the angle to your goal vector will not give you enough information to orient your ship properly.

As to what you should do, it depends on how your ship and its orientation are being represented, i.e. with a quaternion, a matrix, Euler angles, etc. One thing that might be useful to you is that even though the angle will be the same whether the goal vector is on your left or right, the cross product of the current direction vector and the goal vector will not, that is, the cross product will point in opposite directions depending on whether the goal vector is on your left or right.

Again, to tell you how to make use of this, we''d probably need to know more about your simulation.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
just find the perpendicular of the heading vector (the ship''s right, for example) and dot against the destination direction.

In 3d, you''d take the cross product of the heading vector and the ''up'' vector. Based on the code you wrote:

vRightSide = vR.cross(vUp);
fDirection = vRightSide.dot(vSun);

the sign of fDirection will tell you left or right.

And no, there''s not a faster way... but it''s not worth speeding this up unless it''s happening like 10000 times per frame.

Share this post


Link to post
Share on other sites