2014-03-25 33 views
1

我在互聯網上遇到了一個簡單的公式,根據計算字符X位置和鼠標X位置之間的距離並將其除以兩個實體像下面困惑一個簡單的公式將玩家移動到鼠標位置

//the below statement moves the player to the left towards the mouse pointer 

    if(player.position.x > Mouse_Position_X) 
     player.setPosition(player.position.x - ((player.position.x - Mouse_Position_X)/distance * 2.0f)); 
    ... 
    .. 
    . 
// similar statements for other three checks to move player to right, top and bottom 
// distance value is found out by a getDistance function which calculates the distance between two entites(player and mouse pointer in this case) 

我想知道上述公式確實比較簡單的公式如下圖所示 也移動玩家如左圖。

if(player.position.x > Mouse_Position_X) 
    player.setPosition(player.position.x - 2.0f); 

爲什麼我們會用鼠標位置減去玩家當前位置並將其除以距離?

非常感謝!

+0

第一種情況是將玩家移動到鼠標位置。它與player.setPosition(Mouse_Position_X)相同。第二個將它左移2個單位。很奇怪,沒有垂直運動。 – cup

回答

0

第一個公式還取決於某些未定義的變量distance。它似乎將玩家的位置朝着鼠標座標的方向稍微移動。

相關問題