View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

# Check Distance Within Certain Angle

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

20 replies to this topic

### #1Medo3337  Members

Posted 28 June 2013 - 08:53 PM

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
D3DXMatrixRotationYawPitchRoll(&matRot, D3DXToRadian(character->rotationX), D3DXToRadian(character->rotationY), D3DXToRadian(character->rotationZ));
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
}

### #2belfegor  Members

Posted 29 June 2013 - 12:59 AM

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;
}


### #3Norman Barrows  Members

Posted 29 June 2013 - 01:06 AM

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)
{
theta=atan(x3/z3);      // opp/adj. rise/run.
theta=double_rad2deg(theta);
}
else
{
theta=atan(-z3/x3);
theta=double_rad2deg(theta);
theta+=90.0;
}
}
else
{
if (z2>0)
{
theta=atan(z3/-x3);
theta=double_rad2deg(theta);
theta+=270.0;
}
else
{
theta=atan(-x3/-z3);
theta=double_rad2deg(theta);
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;
a=heading(x1,z1,x2,z2)-h1;
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).

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

### #4Medo3337  Members

Posted 29 June 2013 - 07:33 AM

@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
D3DXMatrixRotationYawPitchRoll(&matRot, D3DXToRadian(character->rotationX), D3DXToRadian(character->rotationY), D3DXToRadian(character->rotationZ));
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;

### #5belfegor  Members

Posted 29 June 2013 - 08:44 AM

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, 29 June 2013 - 08:45 AM.

### #6Medo3337  Members

Posted 29 June 2013 - 09:45 AM

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.

### #7belfegor  Members

Posted 29 June 2013 - 10:25 AM

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;
D3DXMatrixRotationYawPitchRoll(&temp, D3DXToRadian(character->rotationX), D3DXToRadian(character->rotationY), D3DXToRadian(character->rotationZ));
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, 29 June 2013 - 10:26 AM.

### #8Medo3337  Members

Posted 29 June 2013 - 10:48 AM

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

### #9belfegor  Members

Posted 29 June 2013 - 11:55 AM

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 );
}


### #10Medo3337  Members

Posted 29 June 2013 - 12:07 PM

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?

### #11belfegor  Members

Posted 29 June 2013 - 12:33 PM

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

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

### #12Medo3337  Members

Posted 29 June 2013 - 12:37 PM

X = Right/Left

Y = Up/Down

Z = Forward/Backward

### #13belfegor  Members

Posted 29 June 2013 - 12:40 PM

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

### #14Medo3337  Members

Posted 29 June 2013 - 12:48 PM

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, 29 June 2013 - 12:54 PM.

### #15belfegor  Members

Posted 29 June 2013 - 12:56 PM

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

Edited by belfegor, 29 June 2013 - 01:00 PM.

### #16Medo3337  Members

Posted 29 June 2013 - 01:41 PM

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

### #17Norman Barrows  Members

Posted 30 June 2013 - 04:43 AM

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.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

### #18Medo3337  Members

Posted 30 June 2013 - 06:05 AM

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] );

### #19belfegor  Members

Posted 30 June 2013 - 06:56 AM

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;
matrix dummyHead;
};
...
// extract "view"
matrix headPos, headRot;
// 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
MatrixTranslation(&headPos, 0.0f, 0.82f, 0.0f);
// head rotation
MatrixYawPitchRoll(&headRot, clampDegree(head_rotationX, -80, 80), clampDegree(head_rotationY, -70, 70), clampDegree(head_rotationZ, -20, 20));
dummyHead = headRot * headPos;
matrix temp = world * dummyHead;
vec3 view( temp.m[2][0], temp.m[2][1], temp.m[2][2] );
normalize( &view );
...


Edited by belfegor, 30 June 2013 - 07:06 AM.

### #20Medo3337  Members

Posted 30 June 2013 - 07:40 AM

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?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.