2

我正在尝试将实体指向 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

我目前正忙于将方向向量转换为角度。非常感谢您的帮助。

4

1 回答 1

2

这不是它的工作原理,你错过了一个重要的信息:你需要弄清楚你的实体现在指向什么方向,而不是使用实体位置?获取该信息可能就像使用当前的旋转矩阵并将其与沿 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;
于 2010-10-16T19:24:14.390 回答