# Check Distance Within Certain Angle

This topic is 1666 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a character and want to check if there is a certain model is in front of the character (cone visibility check)

I'm not sure how do I get cosMaxAngle, I think it has something to do with field of view, if so how do I get the character field of view? (I have the character transformation matrix)

BOOL CheckVisibility()
{
D3DXVECTOR3 view = target->GetPosition() - character->GetPosition();
float distance = GetDistance(character->Position(), target->Position());

if(distance > 100.0f)
return false; //not visible, too far

D3DXVec3Normalize(&view, &view); //view = normalize(view);
D3DXVECTOR3 enemyLookDir;

D3DXMATRIX ZUnitVec;
D3DXMATRIX matRot;
D3DXMatrixTranslation(&ZUnitVec, 0.0f, 0.0f, 1.0f); // matrix holding position of vector pointing to positive z
D3DXMATRIX m = ZUnitVec * matRot;
enemyLookDir = D3DXVECTOR3(m._41,m._42,m._43); // Get vector from the matrix
D3DXVec3Normalize(&enemyLookDir, &enemyLookDir);

// Cosine of the angle between the view vector and the direction the enemy is looking
float cosAngle = D3DXVec3Dot(&enemyLookDir, &view);

float cosMaxAngle = ??? // I think I should use the field of view here? how? I want the character vision cone to be limited to 90 degrees

// Check if player is inside the cone
if(cosAngle < cosMaxAngle)
return false; // Not visible

return true; // Visible
}

##### Share on other sites

When you dot two (normalized) direction vectors you get value in range 1 to -1, so 90 degrees would be 0.5.

Your code is a bit unclear to me, so i will try to explain a bit different.

Say you have character1 position, its target position (look at position) and character2 position, so your function would be something like this:

bool CheckVisibility( const D3DXVECTOR3& char1Pos, const D3DXVECTOR3& char1Target, const D3DXVECTOR3& char2Pos, float cosMaxAngle = 0.5f, float maxDistance = 100.0f)
{
float maxDistSq = maxDistance * maxDistance;
float dist = D3DXVec3LengthSq(&(char2Pos - char1Pos));
if(dist > maxDistSq)
return false;

D3DXVECTOR3 view = char1Target - char1Pos;
D3DXVec3Normalize(&view, &view);

D3DXVECTOR3 toChar2 = char2Pos - char1Pos;
D3DXVec3Normalize(&toChar2, &toChar2);

float cosAngle = D3DXVec3Dot(&toChar2, &view);
if(cosAngle < cosMaxAngle)
return false;
return true;
}


##### Share on other sites

i'd calculate the relative heading to the target. target is in 90 arc of fire if abs(rel_heading) < 45 degrees.

this returns the heading (y rotation) in  degrees from x4,z4 to x5,z5:

// trig function version
int heading(int x4,int z4,int x5,int z5)    // returns heading from point x4,z4 to point x5,z5 in degrees
{
double x3,z3,theta;
int h;
long long x1,z1,x2,z2;
x1=(long long)x4;
z1=(long long)z4;
x2=(long long)x5;
z2=(long long)z5;
x2-=x1;
z2-=z1;
if (x2==0)
{
if (z2==0) return(0);
else if (z2>0) return(0);
else return(180);
}
else if (z2==0)
{
if (x2<0) return(270);
else return(90);
}
else
{
x3=(double)x2;
z3=(double)z2;
if (x2>0)
{
if (z2>0)
{
}
else
{
theta=atan(-z3/x3);
theta+=90.0;
}
}
else
{
if (z2>0)
{
theta=atan(z3/-x3);
theta+=270.0;
}
else
{
theta=atan(-x3/-z3);
theta+=180.0;
}
}
h=(int)theta;
while(h<0) h+=360;
while(h>359) h-=360;
return(h);
}
}

this returns the relative heading (-179 to 180 degrees) from x1,z1 to x2,z2. h1 is the current heading of the target at x1,z1 in degrees.

int relheading(int x1,int z1,int h1,int x2,int z2)
{
int a;
while(a<-179) a+=360;
while(a>180) a-=360;
return(a);
}

so you take your player's y rotation in rads, and convert it to degrees, then pass that and the player's x,z to relheading().

what you get  back is how many degrees the target is left (negative) or right (positive) of the player's current heading.  so if the player is heading east (pos x, 90 degrees), and the target is northeast of the player (a heading of 45 degrees from the player's location) , the relative heading is -45 degrees (45 degrees to the left of the player's current direction).

##### Share on other sites

@belfegor : I think I'm getting close, but I see there is something wrong, the character is seeing from his side and when I rotate him he is still seeing.

Let me know what's going wrong:
float maxDistSq = maxDistance * maxDistance;
float dist = D3DXVec3LengthSq(&(target->Position() - character->GetPosition()));
if(dist > maxDistSq)
return false;

D3DXVECTOR3 modelDir;
D3DXMATRIX ZUnitVec;
D3DXMATRIX matRot;
D3DXMatrixTranslation(&ZUnitVec, 0.0f, 0.0f, 1.0f); // matrix holding position of vector pointing to positive z
D3DXMATRIX m = ZUnitVec * matRot;
modelDir = D3DXVECTOR3(m._41,m._42,m._43); // Get vector from the matrix
D3DXVec3Normalize(&modelDir, &modelDir);

D3DXVECTOR3 view = modelDir - model->Position();
D3DXVec3Normalize(&view, &view);

D3DXVECTOR3 toChar2 = target->Position() - character->Position();
D3DXVec3Normalize(&toChar2, &toChar2);

float cosAngle = D3DXVec3Dot(&toChar2, &view);
float cosMaxAngle = 0.5f;
if(cosAngle < cosMaxAngle)
return false;
return true;

##### Share on other sites

I gave you the working code, all you have to do is to fill it with correct params, but you mix (screw) it up with your code and expect it to work.

Your variable naming does not make sense.

1. Why do you name it modelDir (direction) when you extract position from matrix?

2. Why do you normalize position vector?

modelDir = D3DXVECTOR3(m._41,m._42,m._43); // Get vector from the matrix
D3DXVec3Normalize(&modelDir, &modelDir);


3. Why do you use ZUintVec matrix and matRot for?

4. Why do you reverse the order of matrix multiplication?

D3DXMATRIX m = ZUnitVec * matRot; // m = translation * rotation should be m = rotation * translation if you want to rotate about mesh origin


5. What represent target and what is character?

Edited by belfegor

##### Share on other sites

I think there is something confusing here.

target = The model which I want to check if the character see or not, target->Position = the model position which the character might be looking at.

modelDir = I assume that's the direction the character is looking it? This calculation should be based on the character ROTATION, since if the character rotated, he won't be looking at the same direction and therefore might not be looking at the target model.

character = This is the character itself that want to see if he is seeing "target", so character->Position is the position of the character in 3D world.

##### Share on other sites

That is a bit clearer, then this should work (assuming that you have created your model facing positive Z, positive Y is up and positive X is right):

float maxDistSq = maxDistance * maxDistance;
float dist = D3DXVec3LengthSq(&(target->GetPosition() - character->GetPosition()));
if(dist > maxDistSq)
return false;

D3DXMATRIX temp;
D3DXVECTOR3 view( temp.m[2][0], temp.m[2][1], temp.m[2][2] );
D3DXVec3Normalize(&view, &view);

D3DXVECTOR3 toTarget = target->GetPosition() - character->GetPosition();
D3DXVec3Normalize(&toTarget, &toTarget);

float cosAngle = D3DXVec3Dot(&toChar2, &view);
float cosMaxAngle = 0.5f;
if(cosAngle < cosMaxAngle)
return false;
return true;

Edited by belfegor

##### Share on other sites

Okay that works, except that the character see from his back instead of his front, I have negative Z for moving forward, hmmm should I make it postivie Z or keep it as it is?

Anyway, I tried:

D3DXVECTOR3 view( -temp.m[2][0], -temp.m[2][1], -temp.m[2][2] );

And now it's working as expected with negative Z for Forward.

Since I have cosMaxAngle = 0.5f is the same as 90 degree, I want to give the number in degrees instead, so I want to be:

float cosMaxAngle = GetMaxCosAngle(90); // Should return 0.5f

##### Share on other sites

I am really really sorry, looks like i gave you cos for 60 not 90 degree. Need to learn more about unit circle.

This should be your function to get cos from degree angle for view cone:

float GetMaxCosAngle(float angleDegree)
{
return std::cos( D3DXToRadian(angleDegree) * 0.5f );
}


##### Share on other sites

Great

One last thing, the character should never see the model if it's too high, for example: the model is above him.

Can you give me some idea on how I should do restriction to this?

##### Share on other sites

character should never see the model if it's too high...

On which axis is height? Does that ring some bells?

##### Share on other sites

X = Right/Left

Y = Up/Down

Z = Forward/Backward

##### Share on other sites

Then check the distance along Y axis between character and model.

##### Share on other sites

The problem is the character could move his head to look up, in that case the character should see the model above him.

So, it will be based on where the character head is pointing at.

Example: The character heard something above him, then he would look up.

Edited by Medo3337

##### Share on other sites

Then grab view orientation where his head is facing to build "view cone".

Edited by belfegor

##### Share on other sites

Maybe it's easier to calculate the distance based on Y axis and do calculation based on the player head rotation angle?

##### Share on other sites

dealing with targets above/below is the same as dealing with targets left/right, but instead of working in the x-z plane, you work in the vertical plane that lies in the direction the player is pointing.

again, it can be done by calculating relative angle, or by checking dot products.

if you use dot products, remember that the range -1 to 1 is not linear, its sinusoidal. not an issue if you're just testing boundary conditions.

##### Share on other sites

How do I get the "view" to deal with targets above/below?

I need to change the following line:

D3DXVECTOR3 view( temp.m[2][0], temp.m[2][1], temp.m[2][2] );

##### Share on other sites

How do I get the "view" to deal with targets above/below?

You could store two matrices for your character, one used to move him around the world and other "dummy" for local orientation of his head (if it is a humanoid) you probably also need to constrain your rotations.

struct Character
{
matrix world;
};
...
// extract "view"
// head offset pos from character model center
// so it doesn't look from its stomach (or feet) depending where do you place it in modeling editor
// if your character is 1.8 tall and you center it on origin this will move his "head" in somewhat normal position
matrix temp = world * dummyHead;
vec3 view( temp.m[2][0], temp.m[2][1], temp.m[2][2] );
normalize( &view );
...

Edited by belfegor

##### Share on other sites

The head is associated with "preset animation", so how do I determine the current head transformation?

Do I have to get the head bone transformation?

##### Share on other sites

Do I have to get the head bone transformation?

Who is forcing you?

Since you already have skinned character you could use that.

• 10
• 11
• 9
• 16
• 19