方向角度或将3D角度指向空间中的某个位置
问题描述:
我正在尝试将实体指向3D矢量。 (基本上设置实体角度,以便它指向3D空间中的位置)。目前,我坚持从矢量中获取角度。方向角度或将3D角度指向空间中的某个位置
//Vectors in the bracket are 3D, first is the entity position, second is a position in space where I want to point at.
(myEntity.Pos - posToPointAt).Angle
我目前被困在将方向向量转换成角度。非常感谢帮助。
答
这不是如何工作,你缺少一个重要的信息:而不是使用实体位置,你需要找出你的实体指向什么方式?获取该信息可能与使用当前旋转矩阵并将其与单位矢量乘以z轴(或实体的“中性”方向)相乘一样简单,但这一切都取决于您的特定设置。
一旦你有都方向向量(当前的方向,而方向所需的新位置...最后一个是标准化的“myEntity.Pos - posToPointAt”),您可以使用函数像下面计算方向之间的旋转。注意我在这里使用四元数,你可能需要一个旋转矩阵。
function RotationBetweenVectors(const aV1, aV2: TVector3): TQuaternion;
const
EPSILON = 0.000001;
var
v1: TVector3;
v2: TVector3;
dot: FloatT;
s: FloatT;
invS: FloatT;
c: TVector3;
begin
v1 := aV1.normalize;
v2 := aV2.normalize;
dot := VectorDotProduct(v1, v2);
if dot >= 1 then
begin
// INFO: DotProduct = 1 -> vectors are the same
result := QUATERNION_IDENTITY
end
else if (dot < EPSILON - 1) then
begin
raise Exception.create('180 degree rotation currently not supported.');
end
else
begin
s := sqrt(2 * (1 + dot));
invS := 1/s;
c := VectorCrossProduct(v1, v2);
result.x := c.x * invS;
result.y := c.y * invS;
result.z := c.z * invS;
result.w := 0.5 * s;
result := result.normalize;
end;
end;