【发布时间】:2014-11-16 14:47:21
【问题描述】:
我正在尝试在 C# 中为 vector rejection 实现一个函数。那就是:
我一直在尝试用 C# 编写这个公式,但是由于某种原因它总是返回零。这是我到目前为止所拥有的:
private Vector3 Projection(Vector3 vectorA, Vector3 vectorB) {
Vector3 a2 = Vector3.Scale ( Divide (Vector3.Scale (vectorA, vectorB), Vector3.Scale (vectorB, vectorB)), vectorB);
return a2;
}
private Vector3 Rejection(Vector3 vectorA, Vector3 vectorB) {
Vector3 a2 = vectorA - Projection(vectorA, vectorB);
return a2;
}
private Vector3 Divide(Vector3 a, Vector3 b) {
Vector3 c = new Vector3 ();
c.x = a.x / b.x;
c.y = a.y / b.y;
c.z = a.z / b.z;
return c;
}
private void Example() {
Vector3 a = new Vector3 (5, 5, 0);
Vector3 b = new Vector3 (0, 10, 0);
Vector3 c = Rejection(a, b); // Returns (NaN, 0, NaN)
}
// The Vector3 class represents a 3D vector and it's x, y and z components are of float type. It's [scale method][3] multiplies two vectors component wise.
例子:
假设矢量 A 是施加到沿地面移动的物体的力,矢量 B 是重力(垂直于地面)。当向量 A 作用于物体时,它运动的轨迹应该是垂直于重力的第三个向量,可以认为是 A 垂直于 B 行进。这就是 A 对 B 的排斥。
【问题讨论】:
-
能贴出
Vector的定义(尤其是x,y,z的类型)和Scale的定义吗?另外,您输入的参数是什么?它在返回什么?你说它返回零,但它返回一个向量,所以这是不可能的。 -
您可能希望将此问题标记为特定于 Unity,因为您询问的是 Unity 的
Vector3类的功能。 -
嗨,我没有意识到这个类是 Unity 特有的。我可以使用不同的,但我会将它标记到 Unity。
-
那么您能否举例说明您投入其中的输入类型以及预期和实际输出是什么?
-
我认为你不能有一个既垂直于 B 又指向与 A 相同方向的向量,因为 A 本身并不垂直于 B。你的意思是别的吗?