2016-08-17 152 views
0

我有一個遊戲世界,玩家圍繞一個點(如行星)旋轉。我怎樣才能圍繞中心點旋轉身體?我也想以某種方式能夠使用類似動畫類插值的東西來移動東西,這有可能嗎?謝謝!圍繞中心點旋轉身體

回答

2

您可以通過vector2輕鬆旋轉。

Vector2 vectorbody = new Vector2(50f, 50f); 
Vector2 vectorcenter = new Vector2(100f,100f); 
Vector2 vectordis= vectorbody.cpy().sub(vectorcenter);//Displacement vector center to body 
vectordis.setAngle(vectordis.angle() + rotatespeed);//Rotate speed can be negative that means it will rotates to other side. 
vectordis.add(vectorcenter); //vectordis now became rotated vectorbody 
vectorbody.set(vectordis); //vectorbody updated 

您也可以使用actor方法。

只需定義像is_in_orbit這樣的新變量,並且如果它的真實(在軌道)然後旋轉,否則用一個actor類插值方法移動。

順便說一句,你也有一個意見來使用物理學,因爲牛頓的萬有引力定律也是物理的,但它會變得複雜,並且會在出現更多中心點(行星如你所說)的情況下導致意想不到的結果。

+0

我知道我可以使用演員類,但我也想使用box2d作爲碰撞。我仍然對某些事情感到困惑,我怎樣才能真正旋轉身體?我看到你正在使用矢量來獲得一個角度,我如何設置身體到那個角度? – Wyatt

+0

其實我有這個位置。 vectorbody.x和vectorbody.y是body的新座標。你可以用bodyDef.position.set(vectorbody.x,vectorbody.y)設置body的位置; –

+0

我猜想你錯過了。我說的是身體圍繞中心旋轉而不是身體旋轉時的位置。你可以研究一下body.setTransform(body.getPosition(),angle); –