如何在不知道速度的情况下计算轨迹的角度

时间:2014-11-27 15:36:19

标签: c# math unity3d angle

如何在不知道速度的情况下计算撞击目标的轨迹角度。我只知道最大高度,偏移高度和到目标的距离。

这是我到目前为止所得到的(我不知道如何计算偏移高度):

 float GetAngle(Vector3 startLocation, Vector3 endLocation, float maxHeight)
 {
         float distance = Mathf.Sqrt(Mathf.Pow(startLocation.x - endLocation.x,2) + Mathf.Pow(startLocation.z - endLocation.z,2));

         float offsetHeight = endLocation.y - startLocation.y;
         //how do I calculate offset height in this equation ?
         return -Mathf.Atan (4 * maxHeight/ distance ) + Mathf.PI;
 }

我用它来计算速度(工作正常我只需要正确的角度):

float LaunchVelocity (Vector3 startLocation, Vector3 endLocation, float angle)
 {
         float range = Mathf.Sqrt(Mathf.Pow(startLocation.x - endLocation.x,2) + Mathf.Pow(startLocation.z - endLocation.z,2));
         float offsetHeight = endLocation.y - startLocation.y;
         float gravity = Physics.gravity.y;

         float velocity = range * range * gravity;
         velocity /= range * Mathf.Sin(2 * angle) + 2 * offsetHeight * Mathf.Pow(Mathf.Cos(angle),2);
         return Mathf.Sqrt(velocity);
 }

1 个答案:

答案 0 :(得分:3)

我得到了解决方案:

float GetAngle(float height, Vector3 startLocation, Vector3 endLocation)
    {
        float range = Mathf.Sqrt(Mathf.Pow(startLocation.x - endLocation.x,2) + Mathf.Pow(startLocation.z - endLocation.z,2));
        float offsetHeight = endLocation.y - startLocation.y;
        float g = -Physics.gravity.y;

        float verticalSpeed = Mathf.Sqrt(2 * gravity * height);
        float travelTime = Mathf.Sqrt(2 * (height - offsetHeight) / g) + Mathf.Sqrt(2 * height / g);
        float horizontalSpeed = range / TravelTime;
        float velocity = Mathf.Sqrt(Mathf.Pow(verticalSpeed,2) +  Mathf.Pow(horizontalSpeed, 2));

        return -Mathf.Atan2(verticalSpeed / velocity, horizontalSpeed / velocity) + Mathf.PI;
    }