If you are facing northeast, your direction vector would be in the direction of (+1, +1), normalised this gives (1/sqrt(2), 1/sqrt(2)).
The vector from you to an enemy is posEnemy - posYou so if you are at (10, 0) and an enemy is at (5, 5) then the vector towards them is (5-10, 5-0) = (-5, 5), normalised (-1/sqrt(2), 1/sqrt(2)).
Dot product between those vectors is -1/2 + 1/2 = 0.
This is the cosine of the angle between you, and cos(90degrees) = 0, so they are perpendicular to your facing.
If the dot product is 1, they are directly ahead of you (cos(0) = 1). If it is 0, they are perpendicular to your facing.
All you need to check is that the dot product is greater than the cosine of the desired half-angle of your cone.
Hope that helps.
EDIT: Maths fail, oops ;) Fixed now
Show differencesHistory of post edits
#3Paradigm Shifter
Posted 03 January 2013 - 09:43 AM
If you are facing northeast, your direction vector would be in the direction of (+1, +1), normalised this gives (1/sqrt(2), 1/sqrt(2)).
The vector from you to an enemy is posEnemy - posYou so if you are at (10, 0) and an enemy is at (5, 5) then the vector towards them is (5-10, 5-0) = (-5, 5), normalised (-1/sqrt(2), 1/sqrt(2)).
Dot product between those vectors is -1/2 + 1/2 = 0.
This is the cosine of the angle between you, and cos(90degrees) = 0, so they are perpendicular to your facing.
If the dot product is 1, they are directly ahead of you (cos(0) = 1). If it is 0, they are perpendicular to your facing.
All you need to check is that the dot product is greater than the cosine of the desired half-angle of your cone.
Hope that helps.
EDIT: Maths fail, oops ;) Fixed now
The vector from you to an enemy is posEnemy - posYou so if you are at (10, 0) and an enemy is at (5, 5) then the vector towards them is (5-10, 5-0) = (-5, 5), normalised (-1/sqrt(2), 1/sqrt(2)).
Dot product between those vectors is -1/2 + 1/2 = 0.
This is the cosine of the angle between you, and cos(90degrees) = 0, so they are perpendicular to your facing.
If the dot product is 1, they are directly ahead of you (cos(0) = 1). If it is 0, they are perpendicular to your facing.
All you need to check is that the dot product is greater than the cosine of the desired half-angle of your cone.
Hope that helps.
EDIT: Maths fail, oops ;) Fixed now
#2Paradigm Shifter
Posted 03 January 2013 - 09:41 AM
If you are facing northeast, your direction vector would be in the direction of (+1, +1), normalised this gives (1/sqrt(2), 1/sqrt(2)).
The vector from you to an enemy is posEnemy - posYou so if you are at (10, 0) and an enemy is at (5, 5) then the vector towards them is (5-10, 0-5) = (-5, -5), normalised (-1/sqrt(2), -1/sqrt(2)).
Dot product between those vectors is -1/2 + (-1/2) = -1.
This is the cosine of the angle between you, and cos(180degrees) = -1, so they are directly behind you.
If the dot product is 1, they are directly ahead of you (cos(0) = 1). If it is 0, they are perpendicular to your facing.
All you need to check is that the dot product is greater than the cosine of the desired half-angle of your cone.
Hope that helps.
The vector from you to an enemy is posEnemy - posYou so if you are at (10, 0) and an enemy is at (5, 5) then the vector towards them is (5-10, 0-5) = (-5, -5), normalised (-1/sqrt(2), -1/sqrt(2)).
Dot product between those vectors is -1/2 + (-1/2) = -1.
This is the cosine of the angle between you, and cos(180degrees) = -1, so they are directly behind you.
If the dot product is 1, they are directly ahead of you (cos(0) = 1). If it is 0, they are perpendicular to your facing.
All you need to check is that the dot product is greater than the cosine of the desired half-angle of your cone.
Hope that helps.
#1Paradigm Shifter
Posted 03 January 2013 - 09:40 AM
If you are facing northeast, your direction vector would be in the direction of (+1, +1), normalised this gives (1/sqrt(2), 1/sqrt(2)).<br /><br />The vector from you to an enemy is posEnemy - posYou so if you are at (10, 0) and an enemy is at (5, 5) then the vector towards them is (5-10, 0-5) = (-5, -5), normalised (-1/sqrt(2), -1/sqrt(2)).<br /><br />Dot product between those vectors is -1/2 + (-1/2) = -1.<br /><br />This is the cosine of the angle between you, and cos(180degrees) = -1, so they are directly behind you.<br /><br />If the dot product is 1, they are directly ahead of you (cos(0) = 1). If it is 0, they are perpendicular to your facing.<br /><br />All you need to check is that the dot product is greater than the cosine of the desired half-angle of your cone.<br /><br />Hope that helps.