Delphi 方向角或将三维角度指向空间中的某个位置
我试图将一个实体指向一个3D向量。(基本上设置实体角度,使其指向三维空间中的某个位置)。目前,我正忙于从一个向量得到一个角度Delphi 方向角或将三维角度指向空间中的某个位置,delphi,vector,point,angle,Delphi,Vector,Point,Angle,我试图将一个实体指向一个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 我目前正在将方向向量转换为角度。非常感谢您的帮助。这不是它
//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;
变换器之间的函数旋转(const aV1,aV2:TVector3):TQuaternion;
常数
ε=0.000001;
变量
v1:TVector3;
v2:TVector3;
点:浮动;
s:浮球;
投资:浮动;
c:TVector3;
开始
v1:=aV1.0,正常化;
v2:=aV2.0标准化;
点:=矢量点积(v1,v2);
如果点>=1,则
开始
//信息:DotProduct=1->向量相同
结果:=四元数_恒等式
结束
否则如果(点<ε-1),则
开始
引发异常。创建('当前不支持180度旋转');
结束
其他的
开始
s:=sqrt(2*(1+点));
存货:=1/s;
c:=矢量叉积(v1,v2);
结果x:=c.x*invS;
结果y:=c.y*invS;
结果.z:=c.z*invS;
结果:w:=0.5*s;
结果:=result.normalize;
结束;
结束;