2015-10-23 32 views
0

我想根據玩家手臂的旋轉來拍攝一個物體(咒語)。這個咒語應該是從手中出來並射向鼠標所在的地方(手臂旋轉並指向鼠標的位置)。這是手臂在遊戲中旋轉的方式。在libgdx中基於手臂旋轉的拍攝對象java

public boolean mouseMoved(int screenX, int screenY) { 
    tmp.x = screenX; 
    tmp.y = screenY; 
    tmp.z = 0; 
    cam.unproject(tmp); 
    rot = MathUtils.radiansToDegrees * MathUtils.atan2((float)tmp.y - (float)player.getArmSprite().getY() - player.getArmSprite().getHeight(), 
      tmp.x -player.getArmSprite().getX() - player.getArmSprite().getWidth()); 
    if (rot < 0) rot += 360; 

    //Last right or left means if hes looking left or right 
    if(player.lastRight) 
     player.setObjectRotation(rot + 80); 
    if(player.lastLeft) 
     player.setObjectRotation(-rot - 80); 

這是法術應該如何拍基於脫旋轉

//destination is a vector of where on screen the mouse was clicked 
    if(position.y < destination.y){ 
     position.y += vel.y * Gdx.graphics.getDeltaTime(); 
    } 
    if(position.x < destination.x){ 
     position.x += vel.x * Gdx.graphics.getDeltaTime(); 
    } 

然而,這是非常靠不住的,從來沒有真正反應也應該有一些例外的方式。它從手中發射,然後如果y軸相等,它完全平均並且一直到它到達x位置,我希望它從手中發射到從點a到點b完全直線的位置,這很清楚一個旋轉問題,我似乎無法弄清楚如何解決。

這裏是發生了什麼事

Example image

的圖像

的紅色顯示我點擊,你可以看到它首先到達X位置,現在正行駛到Y,當它應該已經達到我首先點擊的x和y位置

對此問題的任何幫助都非常感謝!

+0

目前還不清楚你有什麼問題。 「won and,從不真實地反應它應該怎樣」是什麼意思?請更具體一些,比如「它只是中途移動」或「手臂繞着一圈旋轉並將屏幕彈出到用戶的鼻子中」 –

+0

@RyanBemrose我編輯它 – Temo

回答

1

我在弧度和切線方面很不好,但幸運的是我們有矢量。

由於您擁有的手臂角度爲rot。我建議使用矢量現在用於任何矢量相關數學。

//A vector pointing up 
Vector2 direction = new Vector2(0, 1); 

//Let's rotate that by the rotation of the arm 
direction.rotate(rot); 

現在方向是手臂指向的方向。如果您的rotation計算在up = 0。所以你可能需要將它旋轉180度,90度或-90度。或者你在某種程度上做了一些愚蠢的事情。

你的法術也應該有一個向量的位置。把它放在手上或你想從哪裏開始。現在你所需要做的就是擴大direction,因爲它現在的長度爲1.如果你想移動5個單位,每幀你可以做direction.scl(5)現在它的長度爲5.但從技術上講,現在沒有方向了,現在大家都稱之爲所以我們來做吧。

//when you need to fire 
float speed = 5; 
Vector2 velocity = direction.cpy().scl(speed); 

//update 
position.add(velocity); 
draw(fireballImage, position.x, position.y); 

我先複製了方向,否則它也會被縮放。然後,我只是將速度添加到該位置並使用該Vector進行繪製。

並顯示向量是真棒,你應該看到這個真棒badlogic與我創建的鼠標程序。 https://github.com/madmenyo/FollowMouse只有我自己的代碼的視圖線。它只需要一點點的矢量知識,而且非常易讀。