找到两点之间的 Look At Rotation 的算法步骤是什么?

问题描述 投票:0回答:0

我想在一段时间内将 ObjectA 的旋转 SLERP 朝向 ObjectB 的位置。我正在工作的插值,我 can't 弄清楚的是如何产生 DestinationRotation(Look to Rotation,或旋转 ObjectA需要最终达到)

实现该目标的步骤是什么?

我有什么:

  1. vector3 两个对象的旋转
  2. vector3 两个对象的位置
  3. 两个对象的vector3轴
  4. 每个物体的角度
  5. 两个对象之间位置 X,Y 的点积
  6. 两个向量之间的欧氏距离
  7. 每个物体的局部向上向量
  8. 每个物体的局部前向向量

我如何计算 Look to Rotation?

注意:这是使用 Unreal Engine 空间数学库的 Verse 编程语言,但我的问题不在于代码,而在于知道以何种顺序使用哪些工具(数学)。

loop:
        Sleep(0.0)
        CurrentTime := GetSimulationElapsedTime()
        DeltaTime := CurrentTime - PreviousTime
        set PreviousTime = CurrentTime
        var MoveToPosition : vector2 = vector2{X := 0.0, Y := 0.0}

        # Player Current Position
        if (Agent := agent[Player], FortChar := Agent.GetFortCharacter[], Trans := FortChar.GetTransform(), Position := Trans.Translation):
                set MoveToPosition = vector2{X := Position.X, Y := Position.Y}
        
        SpiderTrans := Spider.GetTransform()
        SpiderVec := SpiderTrans.Translation
        CurrentSpiderRotation := SpiderTrans.Rotation
        CurrentSpiderPos := vector2{X := SpiderVec.X, Y := SpiderVec.Y}
        DeltaSpeed := SpiderSpeed * DeltaTime
        NewPos := Lerp(CurrentSpiderPos, MoveToPosition, DeltaSpeed)
        
        FinalMoveVector := vector3{X := NewPos.X, Y := NewPos.Y, Z := SpiderVec.Z}
       
        # This is where my rotation gets wonky
        DP := DotProduct(CurrentSpiderPos, MoveToPosition)
        var NewRotation : rotation = CurrentSpiderRotation.ApplyLocalRotationZ(DP)
        if (set NewRotation = Slerp[CurrentSpiderRotation,NewRotation, SpiderSpeed]) {}

        # Movement works fine, but rotation is all over the place
        if (Spider.TeleportTo[FinalMoveVector, NewRotation]) {}
c++ rotation game-engine unreal-engine4 quaternions
© www.soinside.com 2019 - 2024. All rights reserved.