如何在与给定输入vector3正交的所有向量中找到最接近给定目标向量的vector3? [英] How can I find the closest vector3 to a given target vector among all vectors orthogonal to a given input vector3?
问题描述
假设我有两个大小不为零的输入向量:
Suppose I have two input vectors with nonzero magnitudes:
Vector3 t;
Vector3 o;
我需要找到归一化向量v
,以便在与o
正交的所有归一化向量中,v
是最接近此t
目标"向量的向量.我还需要知道t
和o
是否共线,在这种情况下,没有一个这样的v
存在.
I need to find the normalized vector v
such that out of all normalized vectors that are orthogonal to o
, v
is the one that is closest to this t
"target" vector. I also need to know if t
and o
are colinear in which case no one such single v
exists.
这种关系的说明:
举个具体的例子,假设我有一个游戏,玩家偶尔会在环境中的表面上看到某个类似罗盘的物体.玩家必须将指南针上的针指向一个随机放置在游戏世界某处的图片对象.当玩家选择针的位置时,我需要知道玩家的选择与物体的真实"方向相距多远.
For a concrete example, suppose I have a game where the player will occasionally see a certain compass-like object on a surface in the environment. The player will have to point the needle on this compass towards a pictured object that was placed randomly somewhere in the game world. When the player selects the needle's position, I need to know how far off the player's selection is from the "true" direction of the object.
指南针固定为绕着世界方向为o
的轴旋转(也许这是指南针的transform.forward
),并且所显示的游戏对象的方向为t
.
The compass needle is fixed to rotate around an axis with world direction o
(perhaps this is the compass's transform.forward
), and the direction of the pictured game object is t
.
当t
和o
不正交时,指南针将无法直接指向t
,因此我想知道:如何获取指南针的最接近方向可以指向v
?这样,我可以测量该目标方向与玩家选择的方向之间的角度.而且,当该角度低于某个阈值时,玩家就成功了.
When t
and o
are not orthogonal, the compass needle will not be able to point directly in t
, so I want to know: How can I get the closest possible direction the compass needle can point at, which is v
? This way, I can measure the angle between this goal direction and the player's selected direction. And, when that angle is below some threshold, the player is successful.
推荐答案
您可以使用矢量叉积来解决此问题.请注意,Vector3.Cross
的参数顺序很重要-参数的顺序不一致会产生意外的结果.
You can use vector cross products to solve for this. Note that the order of the parameters of Vector3.Cross
matters--inconsistently ordering the parameters can produce unexpected results.
-
取
t
和o
的叉积.
Vector3 x = Vector3.Cross(t, o);
如果o
和t
是共线的,则x
将等于Vector3.zero
.
If o
and t
are colinear, x
will be equal to Vector3.zero
.
if (x == Vector3.zero)
{
// Handle colinear situation
}
否则,x
将是与o
和t
都正交的向量,因此也与v
正交:
Otherwise, x
will be a vector that is orthogonal to both o
and t
and therefore also orthogonal to v
:
else
{
现在,我们有两个与v
正交的向量(即o
和x
),我们可以做另一个叉积来查找与v
共线的向量.并且,如果与第二个叉积相比用于计算x
的叉积更加谨慎,则第二个叉积的输出也将指向与v
相同的方向.然后剩下的就是将其标准化:
Now that we have two vectors that are orthogonal to v
(that is, o
and x
), we can do another cross product to find a vector that is colinear with v
. And if we are careful about the ordering of this second cross product compared to the cross product we used to calculate x
, the output of the second cross product will point in the same direction as v
as well. All that is then left is to normalize it:
Vector3 v = Vector3.Cross(o, x).normalized;
// Use v
}
一起:
Vector3 x = Vector3.Cross(t, o);
if (x == Vector3.zero)
{
// Handle colinear situation
}
else
{
Vector3 v = Vector3.Cross(o, x).normalized;
// Use v
}
这篇关于如何在与给定输入vector3正交的所有向量中找到最接近给定目标向量的vector3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!