简体   繁体   English

计算两个向量之间的方位,然后将其与通过角度进行比较

[英]Calculating the bearing between two vectors then diff that against a passed angle

I am trying to find the 2D vector in a set that is closest to the provided angle from another vector. 我正在尝试从另一个向量中最接近提供的角度的集合中找到2D向量。

So if I have v(10, 10) and I would like to find the closest other vector along an angle of 90 degrees it should find v(20, 10) , for example. 因此,如果我有v(10, 10)并且我想沿90度角找到最接近的其他矢量,则应该找到v(20, 10) I have written a method that I think returns the correct bearing between two vectors. 我写了一种方法,我认为它可以返回两个向量之间的正确方位。

float getBearing(
    const sf::Vector2f& a, const sf::Vector2f& b)
{
    float degs = atan2f(b.y - a.y, b.x - a.x) * (180 / M_PI);
    return (degs > 0.0f ? degs : (360.0f + degs)) + 90.0f;
}

This seems to work okay although if I place one above another it returns 180, which is fine, and 360, which is just odd. 这似乎可行,尽管如果我将一个放在另一个之上,则返回180(可以)和360(仅是奇数)。 Shouldn't it return 0 if it is directly above it? 如果正好在0之上,它不应该返回0吗? The best way to do that would be to check for 360 and return 0 I guess. 最好的方法是检查360并返回0。

My problem is that I can't work out the difference between the passed angle, 90 degrees for example, and the one returned from getBearing . 我的问题是我无法计算出通过角度(例如90度)与从getBearing返回的角度之间的getBearing I'm not even sure if the returned bearing is correct in all situations. 我什至不确定在所有情况下返回的轴承是否正确。

Can anyone help correct any glaringly obvious mistakes in my bearing method and suggest a way to get the difference between two bearings? 谁能帮助纠正我的轴承方法中任何明显的错误,并提出一种方法来获得两个轴承之间的差异? I have been hunting through the internet but there are so many ways to do it, most of which are shown in other languages. 我一直在通过互联网寻找东西,但是有很多方法可以做到,其中大多数以其他语言显示。

Thanks. 谢谢。

If what you need is just to find the vectors nearest to a certain angle, you can follow @swtdrgn method; 如果只需要查找最接近某个角度的向量,则可以使用@swtdrgn方法; if, instead, you actually need to compute the angle difference between two vectors, you can exploit a simple property of the dot product: 相反,如果您实际上需要计算两个向量之间的角度差,则可以利用点积的简单属性:

点积几何定义

where theta is the angle between the two vectors; 其中,θ是两个向量之间的夹角; thus, inverting the formula, you get: 因此,将公式取反,您将得到:

上面公式的逆

I would suggest to take the two vectors that are being compared and do an unit dot product. 我建议采用被比较的两个向量,并做一个单位点积。 The closest bearing should be greatest, 1 being the maximum (meaning the vectors are pointing to the same direction) and -1 being the minimum (meaning the vectors are pointing to opposite directions). 最接近的方位应该最大,1表示最大值(表示向量指向相同的方向),-1表示最小值(表示向量指向相反的方向)。

I have found a solution for now. 我现在已经找到了解决方案。 I have spent a good few hours trying to solve this and I finally do it minutes after asking SO, typical. 我花了好几个小时来尝试解决这个问题,最终在问了典型的SO之后几分钟就完成了。 There may be a much better way of doing this, so I am still open to suggestions from other answers. 这样做可能会有更好的方法,因此我仍然愿意接受其他答案的建议。

I am still using my bearing method from the question at the moment, which will always return a value between 0 and 360. I then get the difference between the returned value and a specified angle like so. 目前,我仍然使用问题中的方位角方法,该方法将始终返回0到360之间的值。然后,我将获得返回值与指定角度之间的差,如下所示。

fabs(fmodf(getBearing(vectorA, vectorB) + 180 - angle, 360) - 180);

This will return a positive float that measures the distance in degrees between the bearing between two vectors. 这将返回一个正浮点,该浮点将度量两个矢量之间的方位之间的距离(以度为单位)。 @swtdrgn's answer suggests using the dot product of the two vectors, this may be much simpler than my bearing method because I don't actually need the angle, I just need the difference. @swtdrgn的答案建议使用两个向量的点积,这可能比我的方位方法简单得多,因为我实际上不需要角度,我只需要差值即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM