2016-09-18 42 views
1

我需要计算角度,通过球在该方向上给定的速度和投掷后应该着陆的点。如何计算投影在3D中的角度以在给定点步进的物体?

水平角度很容易(我们知道开始点和步骤点)。如何计算投影的垂直角度。有物体上应用肉汁。

旅行时间通常为保龄球时间(球释放时间和发生步数之间的时间)。

在unity3d中有直接的方法吗?

观看this视频持续8秒,以清楚地理解此问题。

回答

1

根据维基百科页面Trajectory of a projectile,该 “范围的角度”(你想知道的角度)的计算方法如下:

θ= 1/2 * ARCSIN(GD /V²)

在这个公式中,g是引力常数9.81,d是你想让弹丸传播的距离,v是物体投掷的速度。

代码来计算,这可能是这个样子:

float ThrowAngle(Vector3 destination, float velocity) 
{ 
    const float g = 9.81f; 
    float distance = Vector3.Distance(transform.position, destination); 
    //assuming you want degrees, otherwise just drop the Rad2Deg. 
    return Mathf.Rad2Deg * (0.5f * Asin((g*distance)/Mathf.Pow(velocity, 2f))); 
} 

这会给你假设没有空气阻力等,存在于你的游戏的角度。 如果您的目的地和您的“投掷点”不在同一高度,您可能需要先设置y = 0,否则可能会发生错误。

编辑:

考虑到你的启动点了比目标更高,在同一页面这个公式应该工作:

θ=反正切(V²(+/-)√(V^g(gx 2 + 2yv 2))/ gx)

这里,x是距离或距离,y是高度(相对于发射点)。

代码:

float ThrowAngle(Vector3 start, Vector3 destination, float v) 
    { 
     const float g = 9.81f; 
     float xzd = Mathf.Sqrt(Mathf.Pow(destination.x - start.x, 2) + Mathf.Pow(destination.z - start.z, 2)); 
     float yd = destination.y - start.y; 
     //assuming you want degrees, otherwise just drop the Rad2Deg. Split into two lines for better readability. 
     float sqrt = (Mathf.Pow(v,4) - g * (g*Mathf.Pow(xzd,2) + 2*yd*Mathf.Pow(v,2))/g*xzd); 
     //you could also implement a solution which uses both values in some way, but I left that out for simplicity. 
     return Mathf.Atan(Mathf.Pow(v, 2) + sqrt); 
    } 
+0

没有既不能以相同的高度。投影在某个高度开始,目标点在地面上,如视频中所示,与保龄球(球移动)相同。 – djkp

+0

该文章有一部分不是从y = 0开始,而是有y = 0的着陆点。(你可以把最低点看作y = 0,我想如果实际上没有一个是y = 0)。只需看右边的图形即可找到正确的公式。 –

+0

正在撰写答案。请给我一点时间。 –