2014-07-16 35 views
0

我試圖做一個簡單的磁石效應的時候我的性格是附近的一個硬幣這應該移動到他的位置移動身體到特定點[Box2D的,Libgdx]

我有這樣的:

if(Mycharacter.position.x +2 >= position.x) //position = coin position 
       { 

      body.setTransform(Mycharacter.position.x, Mycharacter.position.y, 0); 
       } 

這與我想要的很接近,但我需要能夠看到硬幣對我角色的移動。

我對Box2d和Libgdx還是很新的,所以如果可能的話讓它變得非常簡單,但這並不能幫助我在物理學上變得糟糕透頂。提前致謝。

+0

也許這更適合遊戲開發? –

+0

這個問題似乎幾乎每週都會出現......這可能會有所幫助:http://stackoverflow.com/questions/24516945/calculate-correct-impluse-or-force-to-move-a-box2d-body-to -a-specific-position – iforce2d

回答

2

我找到了答案:

if("Any condition") 
{ 
    body.setLinearVelocity((Character.position.x - position.x) * Velocity, (Character.position.y - position.y)* Velocity); 
} 

提醒,這只是實現我想要的最簡單的方法。我確定有更合適的方法來做到這一點。

0

你可以這樣做:

1. Compute vector from you to coin (vx = coin.x - player.x, same for y) 
2. Compute the vectors's distance (using MathUtils.sqrt(vx*vx+vy*vy)) 
3. If distance is shorter than magnet range, then reduce the distace. 
4. recompute new vector from player to coin (vx *= (newDistance/oldDistance)) 
5. update coin'S position 
+0

這將是一個非Box2D解決方案。當使用Box2D時,步驟4將對Box2D計算步驟5的身體施加力。 –

相關問題