2016-09-03 19 views
1

我想弄清楚如何計算球將落地。基本上,「球」被設置在約2英尺的位置,該球的手在哪裏。如何找到添加力量/衝動後SCNNode會落到的位置?

然後我想拿球的當前位置,並施加一個力量/衝動,它會推動它前進。並在它着陸之前,我想試着預測球將要撞到地面的位置。此外,場景中的地面高度,矢量在0位置都是明智的。

所以基本上可以計算出你的球將會落地嗎?

Ball.position = SCNVector3Make(Guy.presentationNode.position.x, Guy.presentationNode.position.y, Guy.presentationNode.position.z)   
var Currentposition = Ball.presentationNode.position 
var forceApplyed = SCNVector3(x: 50.0, y: 20.0 , z: 0.0) 
var LandingPiont = Currentposition + forceApplyed // Error on this line of code saying "+" cannot be applyed to CGVector 
Ball.physicsBody?.applyForce(forceApplyed, atPosition: Ball.presentationNode.position, impulse: true) 
+1

我不知道SceneKit是否提供了一個方法t o這樣做。然而,假設你的'forceApplied'實際上是一個衝動,我可以提供一系列方程來計算水平位移。你將不得不把它們編碼。 – bpedit

+0

如果可以的話,那將是驚人的。請發佈! @bpedit – Hunter

回答

1

下面介紹如何使用均勻運動方程計算水平位移。 g的值設置爲SceneKit中的默認值9.8,這意味着您在mks系統中(米,千克,秒)。

以下假設爲正y方向和正方向,球運動的方向爲正,x爲正。請務必注意y方向的動作標誌。 (雖然它顯示格式這種方式以下不是代碼。)

首先找到的初始垂直速度(v0y)由於沿y脈衝:

v0y = Jy/m 
    m is ball’s mass (in kilograms) 
    Jy is impulse along the y (forceApplied.y) 
    (v0y will be negative if Jy is negative) 

接着找到垂直速度分量,當球到達地面(vy)。因爲你找到了一個平方根,你將得到+和 - 答案,使用負值。

vy ^2 = v0y ^2 + 2 * g * y 
    g is your gravitational constant 
    y is ball’s initial height 
    both g and y are negative in your case 
    use the negative root, i.e. vy should be negative 

查找時間(t)球花費在空中:

t = (vy – v0y)/g 
    remember, vy and g are both negative 

現在你需要沿x速度:

vx = Jx/m 
    Jx is impulse along x (forceApplied.x) 
    m is the ball’s mass 
    (the velocity along the x remains constant) 

最後,解決位移( x)沿x:

x = vx * t 
    t is the value you got from the vertical motion equations