How to get the vector direction between two (p1, p2)?

女生的网名这么多〃 提交于 2019-12-11 20:09:25

问题


I have been working on Unigine and been trying to code a flight program for weeks, I need to find the direction between two dummy nodes so I can use this direction to rotate the wings of the aircraft. Any explanation would be appreciated.


回答1:


First you have to calculate the length of the distance between P1 and P2.

distance = abs(P2(y) - P1(y))

Then you can use the angular functions to calculate the angle.

a = sin(distance / length(P12))




回答2:


If under the direction you mean angle w.r.t. an arbitrary vector (let it be (p1,p3)), then you can calculate the angle using:

angle = arcos( (p1,p2) * (p1,p3) / (modulus((p1,p2)) * modulus((p1,p3)) ))

where * is the dot product. The angle will be in radians. To change it to degrees you can multiply it by 180/PI (PI=3.1415926...). Modulus is length of vector: modulus((p1,p2))=square root((p1,p2) * (p1,p2)).

The answer is rather about math than C++ but the implementation of the simple formula is straightforward.




回答3:


As already stated in the comments, the vector from P1 to P2 is given by P = P2 - P1.

The direction can be attained in two ways.
1. Directly compute angle = tan_inverse( P.y() / P.x() ).

In this method however, 1st quadrant and 3rd quadrant are treated in the same way as the signs cancel out.

2.You can normalize this vector to get a unit vector. This is the preferred way since it alleviates the quadrant issues.

P(normalized) = P / (mod(p))

Now you can get the projection of any vector in this direction by just calculating the dot-product by this unit vector.



来源:https://stackoverflow.com/questions/19564229/how-to-get-the-vector-direction-between-two-p1-p2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!