這裏我是計算演員和接觸點之間的度數,並將這個度數傳遞給演員作爲旋轉角度。我希望演員應該指向手指接觸或手指拖動方向(考慮我拉車在路上通過手指觸摸並拖動)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我需要改變上面的公式。
它不能,只要你輸入沒有變化而變化(假設你有一個確定性的CPU,應該是這種情況...)。所以你的接觸點改變了,或者你的鱷魚的位置。 此外:類似於'touchPoint.sub(crocodile.position).angle()'的東西可能更容易理解? – noone
@noone,我認爲通過使用 '代碼' touchPoint.sub(crocodile.position).angle() 我可以找到角度正確嗎? 但crocodile.position是Vector2值,並且子未採用Vector2值,並且如果我正在使用 'code' touchPoint.sub(crocodile.position.x,crocodile.position.y,0).angle(); 然後它也不符合任何API文檔功能。意味着角度()不解決那裏.... 在上面的答案 「你說你的鱷魚的位置改變」是「我想告訴你,是的,我正在更新鱷魚的位置到接觸點。 那麼我該怎麼辦,如果我想在那裏移動.. 謝謝。 –
'Vector3'沒有角度(),但是'Vector2'。所以嘗試一下'flot angle =(new Vector2(touchPoint.x,touchPoint.y))。sub(crocodile.position).angle()'。 – noone