2012-05-01 57 views
0

我在這裏有一個weirrrrd問題。我已經爲我的敵人實施了一個視野,所以當玩家在可探測區域(前方120度錐體)內行駛時,他會被壞人追趕,直到他出現在FOV之外。FOV測試正在檢測玩家前方和後面

問題是還有一個120視野圓錐出來的敵人回來,所以theres兩個錐體,一個在前面,一個在後面,我只想用前面的那個。唯一我能想到的是我滿溢通過PI?我真的不確定。

我用這個教程來實現這個使用向量數學。點積產品部分。 http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-2/

這裏是與FOV相關的代碼。

Vector2 EnemyVecAngle = new Vector2((float)Math.Cos(EnemyAnimation.Angle), (float)Math.Sin(EnemyAnimation.Angle)); 

     //normalize 
     Vector2 EnemyFacingN = Vector2.Normalize(EnemyVecAngle); 
     Vector2 EnemytoPlayer = Vector2.Normalize(player.Position - Position); 

     //check angle 
     float CheckAngle = (float)Math.Acos(Vector2.Dot(EnemyFacingN, EnemytoPlayer)); 

if ((CheckAngle >= 60) && (CheckAngle <= 120) 
following = true; 

這是使敵人面朝德球員的說法,一旦檢測到

EnemyAnimation.Angle = (float)Math.PI/-2 + (float)Math.Atan2(Position.Y - player.Position.Y, Position.X - player.Position.X); 

我不知道爲什麼我需要添加-PI/2,它的工作,我認爲這可能與此問題有關。任何幫助是極大的讚賞!

回答

1

我認爲加Math.PI/-2是旋轉90度的質感。

你應該使用ATAN2:

Vector2 EnemyVecAngle = new Vector2((float)Math.Cos(EnemyAnimation.Angle), (float)Math.Sin(EnemyAnimation.Angle)); 

    //normalize 
    Vector2 EnemyFacingN = Vector2.Normalize(EnemyVecAngle); 
    Vector2 EnemytoPlayer = Vector2.Normalize(player.Position - Position); 

    //check angle 
    float CheckAngle = (float) (Math.Atan2(EnemyFacingN.Y, EnemyFacingN.X) 
           - Math.Atan2(EnemytoPlayer.Y, EnemytoPlayer.X)); 

有了它應該是足夠了... Atant2會照顧直角標誌... 雖然你會檢查新計算的角度來選擇adecuate範圍...