Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

here is an image show what I am gonna to do in my XNA game , I want to check if the player is inside zone range and if yes then attack the player , I tried to do this using checking ray but it's odd and give null results as player must be in front of enemy so he can be detected !

enter code here

hey every body , I want instead view angle for each enemy ( 3D dimension ):

enter image description here

share|improve this question
There is no code. – Grey May 21 '13 at 0:04
![enter comment here][1] There is still no code but at least there is now a picture – Jimmy May 21 '13 at 0:59
What is "checking ray"? The problem is quite basic, it's just distance(player, enemy) < range where distance(player, enemy) = sqrt((player.x-enemy.x)^2 + (player.y-enemy.y)^2). Also it's quite common to use squared distances to avoid the expensive sqrt. – congusbongus May 21 '13 at 1:43
In fact, since you're using XNA, you can just use the Vector2.DistanceSquared Method. – congusbongus May 21 '13 at 1:55
thanks very very much sorry Jimmy , Cong :) I want it for 3D if you can with view angle instead of circle if you can – Mohammad Ahmed May 21 '13 at 11:10

When you use the XNA class Vector2 to represent the positions of the player and the enemies, you can use its method Distance to calculate the distance to another Vector2. Then you just have to check if this is smaller than the radius of the attack circle.

To calculate the distance between two points yourself, you can use the pythagorean theorem.

distance calculation using pythagorean theorem

// first get the horizontal and vertical distance.
double distanceX = player.x - enemy.x;
double distanceY = player.y - enemy.y;
// then calculate the length of the hypotenuse of the triangle they form
double dist = sqrt( (distanceX * distanceX) + (distanceY * distanceY) );
if (dist < enemy.attackZoneRadius) {

This example is overly verbose for clarity. The distanceX and distanceY calculations can be put right into the calcualtion of dist:

To improve the performance, you can save the attack zone radius of the enemy already squared. You can then omit the call to sqrt (calculating square-roots is a quite expensive operation) or use the faster XNA Vector2 method DistanceSquared.

Edit: You didn't mention that you need this in 3-dimensional space. The XNA Vector3 class supports the methods Distance and DistanceSquared just like the 2d version. When you want to do this manually, the pythagorean theorem still applies in 3d space (I think it applies to any number of dimensions, but that's rather a question for

double distX = player.x - enemy.x;
double distY = player.y - enemy.y;    
double distZ = player.z - enemy.z;

double dist = sqrt( (distX * distX) + (distY * distY) + (distZ * distZ) );
share|improve this answer
Thanks Phulipp so much , but sorry I want this for 3D instead :) if you can pls – Mohammad Ahmed May 21 '13 at 11:05
Philipp it works , again I want it for 3D , then how to make enemy goes in the direction of player ? – Mohammad Ahmed May 21 '13 at 11:50
@MohammadAhmed You should have mentioned that in your question, because the graphic implies two-dimensional space. I updated my answer. – Philipp May 21 '13 at 12:09
thaaanks Philipp sorry :) I will mentioned it in the next time, I will post another question Ok later on , now for the first problem ( zone range ) , enemy moves in another direction , how to make it move toward me ? 2 : As the second picture shown , If I want to make view range ( 40 degree ) in front of each enemy for example , if in the view range then attak . 3 : Phillip , the last question is if I want to make enemy walk a little bit around his position ( from -10 to 10 ) for example , i.e as he is searching for some one , can you give me just steps ! thanks – Mohammad Ahmed May 21 '13 at 12:56
@MohammadAhmed Now that's a whole bunch of new problems which all warrant entirely new questions. – Philipp May 21 '13 at 12:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.