   Line of Sight Using Ogre
  I'm not 100% sure if this is where I should post this, but it seems right to me.Basically I'm trying to determine whether an entity is in the line of sight of another entity in ogre. I'm using functions built into ogre to try and determine this, but I'm having some issues. It seems that sometimes it gets the expected result, and at other times it ignores entities right infront of it, but will find entities directly behind it.This is the function:-/* method for checking that entity is FOV, SEntity mean Source Entityand TEntity mean TargetEntity, return true if TEntity in within the FOV of SEntity*/bool Character::IsEntityInFOV(Entity *SEntity, Entity *TEntity, Vector3 SEntityDirection){Vector3 SourcePosition = SEntity->getParentSceneNode()->_getDerivedPosition();Vector3 TargetPosition = TEntity->getParentSceneNode()->_getDerivedPosition();Vector3 DirectionVector = SourcePosition - TargetPosition;SEntityDirection.normalise();DirectionVector.normalise();return(SEntityDirection.directionEquals(DirectionVector, Ogre::Degree(120))) ;}This function should determine the direction vector between the two entities, and determine whether this vector is in a 120 degree threshold from the direction that the first entity is facing. Is my thinking behind this right? And if not, where is the problem?   
  It should probably be:Vector3 DirectionVector = TargetPosition - SourcePosition;Rather than:Vector3 DirectionVector = SourcePosition - TargetPosition;Remember, it's the vector from the source position to the target position that you're interested in, not the other way around.If that doesn't fix it, perhaps you could post the directionEquals() function.   ;  That didnt change anything.heres the directionEquals() function,inline bool directionEquals(const Vector3& rhs, const Radian& tolerance) const{Real dot = dotProduct(rhs);Radian angle = Math::ACos(dot);return Math::Abs(angle.valueRadians()) <= tolerance.valueRadians();};Quote:That didnt change anything.Can you post your revised IsEntityInFOV() function? Meanwhile, a couple of quick points:1. This isn't actually a 'line of sight' test, but rather a field-of-view test (as the function name IsEntityInFOV correctly indicates).2. You're using a field of view of 240 degrees, which is fairly wide. Are you sure you didn't intend for the FOV to be 120 degrees?   ;  1st jyk is right2nd 120 degrees is a lot. Your function is telling 120° clockwise AND anticlockwise (that means a total FOV of 240°). I believe you want to use 60°, which will mean60° clockw + 60° anticlockw = 120° FOVRight? (pst, I am told children eyes have a FOV of 110° and adults' 160°)3rd, check you got SEntityDirection right (the third parameter)CheersDark SylincEdit: Argh, jyk beated me with his 2nd post   ;  Revised Function:-bool Character::IsEntityInFOV(Entity *SEntity, Entity *TEntity, Vector3 SEntityDirection){Vector3 SourcePosition = SEntity->getParentSceneNode()->_getDerivedPosition();Vector3 TargetPosition = TEntity->getParentSceneNode()->_getDerivedPosition();Vector3 DirectionVector = TargetPosition - SourcePosition;SEntityDirection.normalise();DirectionVector.normalise();return(SEntityDirection.directionEquals(DirectionVector, Ogre::Degree(60))) ;}Still not working right, its still the same problem (return a true result even when the object is behind the entity). The SEntityDirection is definately right, I'm using it earlier in the code to make the player face the correct direction.   ;  Add some debug output, or step through the code using the debugger. Set up situations where you know the outcome (e.g. move directly in front of or behind the enemy), then check the various values involved in the computation to see where things are going wrong. Do the two vectors (direction and vector to target) appear to be correct? What's the value of the dot product? What's the value of the computed angle? Is the function actually returning the wrong result, or is some other part of your code interpreting the results incorrectly?   ;  Ok I think I've found the problem, but I'm not a 100% sure if I'm right or not, so any input will be great.I think the problem is when I get the angle between the two normalised direction vectors its returning the wrong angle.Its hard to explain this without an example so here's one-Entity1 is trying to determine whether Entity2 is in its line of sight-Entity1 has direction vector (1,0,0) and is at position (-150, 0, -900)-Entity2 is at Postion (-750, 0, -600)-The normalised direction vector between Entity1 and Entity2 is (-0.89, 0, 0.44) :YOU MIGHT WANT TO CHECK THIS-using angle = acos((1,0,0).(-0.89, 0, 0.44)) and then converting to degrees gets the angle 26.58 degrees.It doesnt take a genius to work out that this is wrong, and I figure this is the opposite angle and the real angle between the 2 vectors is 153.42 degrees (180 - 26.58). How can I get this angle rather than tryng to catch exceptions with a bunch of if statements?;  Your analysis seems correct, so the question then becomes why this:Quote:-using angle = acos((1,0,0).(-0.89, 0, 0.44)) and then converting to degrees gets the angle 26.58 degrees.Is returning the wrong angle.At this point, I can really only confirm your observations. The result of the dot product should be -.89; acos(-.89) is ~2.67 radians, or ~152.87 degrees.You're using the Ogre math library, correct? It seems unlikely that this would be due to a bug in the math library code (if only because Ogre is fairly widely used). If you haven't solved this yet, here's what I'd recommend doing next. Break the problem down into even smaller steps and make sure all of the intermediate values are correct. Check the dot product return value and the angle in radians (before conversion) to see if they match the expected values. If necessary, step into the dot product code itself (if possible), and see how it's being computed.Clearly the problem is related to the sign of the dot product, but in what you've posted here, I'm not seeing where the error is being introduced.   ;  Ok I did what you said and broke the problem down into smaller steps...and now it works. I have no idea why but I'm not complaining. Thanks for all your helps.   