Question

A weirrrrd problem I'm having here. I've implemented a field of view for my enemies, so that when the player travels inside the detectable area (120 degree cone in front) he gets chased by the bad guy until he gets outside the FOV.

Problem is there is a 120 FOV cone coming out of teh enemies back as well, so theres two cones, one in front, one behind, i only want to use the one in front. Only thing i can think of is I'm overflowing passed PI? I'm really not sure.

I used this tutorial to implement this using vector math. Dot product section. http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-2/

here's the code related to 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;

this is the statement that makes the enemy face towards teh player once detected

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

i'm wondering why i need to add -PI/2 for it to work, i think this could be related to teh issue. Any help is greatly appreciated!

Was it helpful?

Solution

I think that add Math.PI / -2 is to rotate 90º the texture.

You should use 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) );  

With that it should be enough... Atant2 will take care of the right angle sign... though you will inspect the new calculated angles to choose the adecuate range...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top