【问题标题】:Controlling Ship Rotation as it Orbits a Target在围绕目标运行时控制船舶旋转
【发布时间】:2020-06-08 09:02:12
【问题描述】:

我试图让我的飞船实体看向它们正在移动的方向或它们正在环绕的目标。在这一点上,我会对任何一个结果感到满意。不幸的是,尽管我在 Google 上研究了如何实现这一点,但我似乎对底层数学的理解还不够。

这是我目前在我的 RotateSystem 中拥有的

public void Execute(ref Translation shipPosition, ref Ship ship, ref Rotation rotation)
{

      ship.Theta += .0075f;
      float3 delta = new float3(
          ship.Radius * Mathf.Sin(ship.Theta) * Mathf.Cos(ship.Phi),
          ship.Radius * Mathf.Cos(ship.Theta),
          ship.Radius * Mathf.Sin(ship.Theta) * Mathf.Sin(ship.Phi));


      rotation.Value = Quaternion.Slerp(rotation.Value,
          Quaternion.LookRotation(ship.OrbitTarget + delta-shipPosition.Value),0.5f);


      shipPosition.Value = ship.OrbitTarget + delta;           

}

这样做的结果是,船只有 1/2 的时间面向其轨道目标,而另一半的时间则面向远离轨道目标。

【问题讨论】:

    标签: c# unity3d entity-component-system


    【解决方案1】:

    如果没有第二个参数,Quaternion.LookRotation 假定您希望本地 up 尽可能接近 Vector3.up。相反,使用从轨道体到轨道体的方向:

    public void Execute(ref Translation shipPosition, ref Ship ship, ref Rotation rotation)
    {
    
        ship.Theta += .0075f;
        float3 delta = new float3(
                ship.Radius * Mathf.Sin(ship.Theta) * Mathf.Cos(ship.Phi),
                ship.Radius * Mathf.Cos(ship.Theta),
                ship.Radius * Mathf.Sin(ship.Theta) * Mathf.Sin(ship.Phi));
    
        Vector3 previousPos = shipPosition.Value;
    
        shipPosition.Value = ship.OrbitTarget + delta;   
    
        rotation.Value = Quaternion.Slerp(rotation.Value, Quaternion.LookRotation(
                shipPosition.Value - previousPos,
                ship.OrbitTarget - shipPosition.Value), 0.5f);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-13
      • 2019-03-21
      • 1970-01-01
      相关资源
      最近更新 更多