如何在与给定输入向量 3 正交的所有向量中找到最接近给定目标向量的向量 3 ? [英] How can I find the closest vector3 to a given target vector among all vectors orthogonal to a given input vector3?

查看:21
本文介绍了如何在与给定输入向量 3 正交的所有向量中找到最接近给定目标向量的向量 3 ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个非零幅值的输入向量:

Suppose I have two input vectors with nonzero magnitudes:

Vector3 t;
Vector3 o;

我需要找到归一化向量 v 使得在所有与 o 正交的归一化向量中,v 是最接近这个 t 目标"向量.我还需要知道 to 是否共线,在这种情况下,不存在这样的单个 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.

to不正交时,指南针将无法直接指向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.

  1. to的叉积.

Vector3 x = Vector3.Cross(t, o);

  • 如果ot 是共线的,x 将等于Vector3.zero.

  • If o and t are colinear, x will be equal to Vector3.zero.

    if (x == Vector3.zero) 
    {
        // Handle colinear situation
    }
    

  • 否则,x 将是一个与 ot 正交的向量,因此也与 正交v:

  • Otherwise, x will be a vector that is orthogonal to both o and t and therefore also orthogonal to v:

    else
    {
    

  • 现在我们有两个与 v 正交的向量(即 ox),我们可以做另一个叉积以找到与 v 共线的向量.如果与我们用来计算 x 的叉积相比,我们仔细考虑第二个叉积的顺序,则第二个叉积的输出将指向与 v<相同的方向/code> 也是如此.然后剩下的就是对其进行标准化:

  • 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
    }
    

    这篇关于如何在与给定输入向量 3 正交的所有向量中找到最接近给定目标向量的向量 3 ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆