【问题标题】:Unity3d: Position a gameobject such that its forms a right angled triangle with other two gameobjectsUnity3d:定位一个游戏对象,使其与其他两个游戏对象形成一个直角三角形
【发布时间】:2019-07-21 21:04:48
【问题描述】:

如何定位C gameObject,使其与A and B 形成一个直角三角形。我尝试使用(B.transform.position.x,A.transform.position.z),但它仍然给我一些接近A 的东西(它在全球范围内使用)。我希望Clocal red axis of Alocal green axis of B 一致,如图所示。我该怎么办?

【问题讨论】:

  • 我错过了局部轴问题的最后一部分,要解决具体的方向,你只需要计算两个方向的交集通常与叉积。
  • 那么是`Vector3.Cross(A.transform.right, -B.transform.up)`吗?
  • 在该链接有一个 LineLineIntersection 部分,您应该可以将其与 A.transform.right 和 -b.transform.up 及其位置一起使用。

标签: c# unity3d vector position angle


【解决方案1】:

有几种方法可以表达这个问题。


沿着 B 的上轴找到一个与 A 成直角的点。这将忽略 A 的任何旋转。

为此,将 A 的位置沿 B 向上投影。换句话说,找到 (A-B) 和 B 的点积,将其乘以 B 的上,然后将其与 B 相加。

Vector3 cPos = B.transform.position + Vector3.Dot(A.transform.position - B.transform.position, B.transform.up) * B.transform.up;
C.transform.position = cPos;

另一种方法是找到 B 向上和 A 向右的交点。根据 A 和 B 的旋转,这可能会产生非直角或根本没有点。

这有点复杂,the link in the comments 有很好的实现:

public static bool LineLineIntersection(out Vector3 intersection, Vector3 linePoint1, Vector3 lineVec1, Vector3 linePoint2, Vector3 lineVec2){

      Vector3 lineVec3 = linePoint2 - linePoint1;
      Vector3 crossVec1and2 = Vector3.Cross(lineVec1, lineVec2);
      Vector3 crossVec3and2 = Vector3.Cross(lineVec3, lineVec2);

      float planarFactor = Vector3.Dot(lineVec3, crossVec1and2);

      //is coplanar, and not parrallel
      if(Mathf.Abs(planarFactor) < 0.0001f && crossVec1and2.sqrMagnitude > 0.0001f)
      {
          float s = Vector3.Dot(crossVec3and2, crossVec1and2) / crossVec1and2.sqrMagnitude;
          intersection = linePoint1 + (lineVec1 * s);
          return true;
      }
      else
      {
          intersection = Vector3.zero;
          return false;
      }
  }

然后找到你的位置:

Vector3 cPos;
bool doIntersect = LineLineIntersection(out cPos, A.transform.position, A.transform.right, B.transform.position, B.transform.up);
if (doIntersect) {
    C.transform.position = cPos;
} else {
    // do something reasonable, like using projection, or not changing the position
    Vector3 cPos = B.transform.position + Vector3.Dot(A.transform.position - B.transform.position, B.transform.up) * B.transform.up;
    C.transform.position = cPos;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多