2013-08-20 116 views
1

這裏我是計算演員和接觸點之間的度數,並將這個度數傳遞給演員作爲旋轉角度。我希望演員應該指向手指接觸或手指拖動方向(考慮我拉車在路上通過手指觸摸並拖動)Libgdx之間的兩點計算

float degrees = (float) ((Math.atan2(touchPoint.x - crocodile.position.x, -(touchPoint.y - crocodile.position.y)) * 180.0d/Math.PI)); 

其計算程度正確。只是有時在它之間有一點不同的程度。所以當結果與預期完全不同時,我的演員會波動。

結果,我印刷是:

塊引用

degree is :: 141.93233 
    degree is :: 180.0 
    degree is :: 180.0 
    degree is :: 158.61426 
    degree is :: 90.0 
    degree is :: 180.0 
    degree is :: 180.0 
    degree is :: 90.0 
    degree is :: 180.0 
    degree is :: 180.0 
    degree is :: 180.0 
    degree is :: 141.93057 
    degree is :: 180.0 
    degree is :: 180.0 
    degree is :: 180.0 
    degree is :: 141.93306 
    degree is :: 180.0 
    degree is :: 180.0 
    degree is :: 141.93257 
    degree is :: 180.0 
    degree is :: 180.0 
    degree is :: 141.93134 
    degree is :: 141.93257 
    degree is :: 180.0 
    degree is :: 180.0 
    degree is :: 180.0 
    degree is :: 141.93134 
    degree is :: 180.0 
    degree is :: 180.0 
    degree is :: 141.93306 
    degree is :: 180.0 
    degree is :: 180.0 
    degree is :: 141.93257 
    degree is :: 180.0 
    degree is :: 141.93134 
    degree is :: 180.0 
    degree is :: 180.0 
    degree is :: 141.93257 
    degree is :: 180.0 
    degree is :: 180.0 
    degree is :: 141.93257 
    degree is :: 180.0 
    degree is :: 141.93134 
    degree is :: 180.0 
    degree is :: 141.93257 
    degree is :: 180.0 
    degree is :: 180.0 
    degree is :: 141.93008 
    degree is :: 180.0 
    degree is :: 180.0 
    degree is :: 141.93306 
    degree is :: 180.0 
    degree is :: 180.0 
    degree is :: 141.93257 
    degree is :: 180.0 
    degree is :: 180.0 
    degree is :: 180.0 
    degree is :: 141.93134 
    degree is :: 180.0 
    degree is :: 180.0 
    degree is :: 180.0 
    degree is :: 180.0 
    degree is :: 180.0 
    degree is :: 180.0 
    degree is :: 180.0 

現在,180和141爲什麼它與那些值波動。 考慮到我期望不斷地或者不斷地180我需要改變上面的公式。

+5

它不能,只要你輸入沒有變化而變化(假設你有一個確定性的CPU,應該是這種情況...)。所以你的接觸點改變了,或者你的鱷魚的位置。 此外:類似於'touchPoint.sub(crocodile.position).angle()'的東西可能更容易理解? – noone

+0

@noone,我認爲通過使用 '代碼' touchPoint.sub(crocodile.position).angle() 我可以找到角度正確嗎? 但crocodile.position是Vector2值,並且子未採用Vector2值,並且如果我正在使用 'code' touchPoint.sub(crocodile.position.x,crocodile.position.y,0).angle(); 然後它也不符合任何API文檔功能。意味着角度()不解決那裏.... 在上面的答案 「你說你的鱷魚的位置改變」是「我想告訴你,是的,我正在更新鱷魚的位置到接觸點​​。 那麼我該怎麼辦,如果我想在那裏移動.. 謝謝。 –

+3

'Vector3'沒有角度(),但是'Vector2'。所以嘗試一下'flot angle =(new Vector2(touchPoint.x,touchPoint.y))。sub(crocodile.position).angle()'。 – noone

回答

2

第一個參數必須是y,第二個參數是x。

double atan2(double y, double x) 

ATAN2實際上是因爲,通常第一參數指定的Y/X

的反正切函數被取收率

此外,當您交換x和y時,您可以獲得適當的程度,因爲在某些邊框(如x < 0和y < 0)中,交換與否都無關緊要。

Look here for more explanation

+0

請[格式](http://stackoverflow.com/help/formatting)提供您的答案,以提高其可讀性。 – ryanyuyu

0

結合的問題和答案幫助我儘快解決我的問題。只是爲了澄清溶液一點:

double degrees = Math.atan2(
    towards.getY() - pointer.getY(), 
    towards.getX() - pointer.getX() 
) * 180.0d/Math.PI; 
pointer.setRotation((float) degrees); 

還要注意的是,爲了圍在中間旋轉,你需要做的:

pointer.setOrigin(Align.center);