• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Medo Mex

Check Distance Within Certain Angle

20 posts in this topic

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
}

 

0

Share this post


Link to post
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;
}
0

Share this post


Link to post
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)
            {
            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).

0

Share this post


Link to post
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
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;

 

0

Share this post


Link to post
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
1

Share this post


Link to post
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.

0

Share this post


Link to post
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;
    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
1

Share this post


Link to post
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
0

Share this post


Link to post
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 );
}
1

Share this post


Link to post
Share on other sites

Great wink.png

 

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? mellow.png

0

Share this post


Link to post
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
0

Share this post


Link to post
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?

0

Share this post


Link to post
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.

1

Share this post


Link to post
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] );
0

Share this post


Link to post
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;
    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
0

Share this post


Link to post
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?

0

Share this post


Link to post
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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0