Detection of Line of Sight

is it necessary to use A Line or A Ray is enough for Line of Sight
and how to make a region in which any objects gets Detected!.

Assuming you want to check if the player is seen by an enemy:
First, you check if the player is inside the view distance.
Then, you cast a ray from the enemy to the player and check that there is no obstacle between them.
Your enemy has a certain FOV. Get the angle of the viewDirection of the enemy to the vector from the enemy to the player and check if the angle between them is inside the FOV.

you might want to send multiple rays (one ray to the head, one to left hand, one to right hand, one to belly, one to leg left, one to leg right, one to the weapon). for performance reasons, you could cycle between those ray targets each frame. multiple rays compensate for partially covered enemies.

like @mathiasj said: the FOV (Field of View) is important (guard has no eyes on backside of the head).

also note that the A.I. will have to remember where it has seen the player so that the player can not hide behind wall and unhide frequently. but still the A.I. would have to deal with the possibility that the user moved from where it was and layd a trap or ambush. complex behavior…

‚Ķwhat i do is simple check for point inside a triangle, where triangle is sight of NPC (one corner of triangle is where NPC eyes are), and point been position of player‚Ķyou can easy adopt this for any ‚Äėsight angle‚Äô by adjusting triangle corner angle and you can check depth(height) if needed, and then additionally perform Ray check just in case that detected player is obstructed by some other geometry‚Ķ

1 Like

Thanks bro but how to do this exactly
i mean where is the triangle inside a class??
which class exactly??

…well…for triangle you need basically 3 points (Vector3f), two points you place on right and left side of sight and 3rd point is location where NPC eyes should be…then you just calculate is point inside that triangle, where point is position of player or any other character. Once is detected, just fire Ray to detected point, in order to determine is it obstructed or not. Also, you need to do translation/rotation of mentioned points, based on translation/rotation of character head…i don’t know does JME providing any built in commands for it so i really cant say about that…

EDIT:
Give me a moment…Ill write small thing you can implement then in to your code…

…this should do…

…this is your sight check update…

 public boolean in_sight(Vector3f position_of_my_target,Vector3f v1,Vector3f v2, Vector3f v3)
   {
    //v1=triangle eyes position corner, v2=triangle right corner, v3=triangle left corner
    boolean b1,b2,b3;
    
    b1=(this.cross_product(position_of_my_target,v1,v2)<0.0f);
    b2=(this.cross_product(position_of_my_target,v2,v3)<0.0f);
    b3=(this.cross_product(position_of_my_target,v3,v1)<0.0f);
    
    return ((b1==b2)&&(b2==b3));
   }

…this is a bit modified cross product call…

public float cross_product(Vector3f p1,Vector3f p2,Vector3f p3)
{
    //since its sight angle is distributed on XZ plane , Z coordinate is used instead of Y (its 2D plane check)
    
    return ((p1.getX()-p3.getX())*(p2.getZ()-p3.getZ())-(p2.getX()-p3.getX())*(p1.getZ()-p3.getZ()));
}

…this will do…keep in mind, detection will work infinitely along vertical Y axis…so, if you want to limit it, just perform additional check after this one, where you will check is eye corner of triangle within certain boundaries (eg. is_eye_position.Y>character_position.Y, and is_eye_position.Y<(character_position.Y+sight_vertical_offset)…something like this…also, since character moves, you must update positions/rotations of triangle corners accordingly…I hope this helps…

1 Like

got it man! I wil see that it works

…it should work just fine…tested already :wink:

yo can we directly use a triangle???

P.S if it is more efficient

sry to reply late but where do we have to use this method?

@Ecco, I was thinking of GhostControl attached to the node, with Cone collision shape and detect the collision, what do you think of this approach?

@Ecco i think this check is working on YX plane not as expected!
any solution ?

…take a look at this pic…this is how v1,v2,v3 should be set, for given code example…

…so you can set v1 to be a parent of v2 and v3 (nodes), and then you simply apply translation/rotation on to v1, from object you checking sight from (head of some NPC, turret, etc)…also keep in mind that check will be infinite along Y axis, which means, it will be detected all objects far up or down of your desired sight maybe…so, in that case just do additional check along Y axis (does object you detecting is within Yposition_of_NPC+height_of_NPC, or similar check, depending what you want)…

…you dont have to use this solution as you have already similar function call within JME3…basically, you can get same result by using FastMath.pointInsideTriangle(Vector2f t0, Vector2f t1, Vector2f t2, Vector2f p); , just make sure, that you updating this call with properly updated transformations of corresponding points, and its should work, either one of this functions…i hope this helps…

nice man appreciate it and this v1 object has to be a geometry ? for being a node or is there any other than this!

If you want to detect points within a line of sight ‚Äėcone‚Äô then it‚Äôs even easier: dot product to the rescue.

// Perhaps from a spatial or camera or...
Vector3f pos = ....
Quaternion rotation = ...
Vector3f look = rotation.mult(Vector3f.UNIT_Z);

// Decide how fat the cone should be, this can be a constant.  Note:
// the angle for cosine is relative to the side and not straight ahead
// so I will show a 30 degree example so it's clearer.  90 degrees is
// straight ahead.
float threshold = FastMath.cos((90 - 30) * FastMath.DEG_TO_RAD);

Vector3f intruderPos = ....
Vector3f relative = intruderPos.substract(pos).normalizeLocal();
float dot = relative.dot(look);
if( dot > threshold ) {
    // intruder is in the cone
}
3 Likes

you made me feel stupid, it is so straight forward.
That what make game programming fun, sure I will use it later as an example when I teach my kids dot product.

Heheh‚Ķ the dot product is the ‚Äėheavy handled screw driver‚Äô of 3D graphics (‚Äėheavy-handled‚Äô so that it can even be used as a hammer in a pinch). It‚Äôs one of my very favorite things.

…well…all of them, v1,v2,v3 could be a node…then just use their parent (v1) for aligning and applying transformation/rotation on to, from a NPC head, turret, or anything, and that will do…

thank u bro it worked correctly also i had to use the World Coordinates of the node

1 Like