C# 如何在不知道速度的情况下计算轨迹的角度
在不知道速度的情况下,如何计算弹道击中目标的角度。我只知道最大高度、偏移高度和到目标的距离 这是我到目前为止得到的结果(我不知道如何计算偏移高度): 我用它来计算速度(很好,我只需要正确的角度): 我得到了解决方案:C# 如何在不知道速度的情况下计算轨迹的角度,c#,math,unity3d,angle,C#,Math,Unity3d,Angle,在不知道速度的情况下,如何计算弹道击中目标的角度。我只知道最大高度、偏移高度和到目标的距离 这是我到目前为止得到的结果(我不知道如何计算偏移高度): 我用它来计算速度(很好,我只需要正确的角度): 我得到了解决方案: float GetAngle(float height, Vector3 startLocation, Vector3 endLocation) { float range = Mathf.Sqrt(Mathf.Pow(startLocation.x -
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;
}
看这里开始…非常好。别忘了接受它,让别人知道它已经解决了。
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);
}
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;
}