I have not marked this question Answered yet.
The current accepted answer got accepted automatically because of the Bounty Time-Limit
With reference to this programming game I am currently building.
As you can see from the above link, I am currently building a game in where user-programmable robots fight autonomously in an arena.
Now, I need a way to detect if a robot has detected another robot in a particular angle (depending on where the turret may be facing):
alt text http://img21.imageshack.us/img21/7839/robotdetectionrg5.jpg
As you can see from the above image, I have drawn a kind of point-of-view of a tank in which I now need to emulate in my game, as to check each point in it to see if another robot is in view.
The bots are just canvases that are constantly translating on the Battle Arena (another canvas).
I know the heading the turret (the way it will be currently facing), and with that, I need to find if there are any bots in its path(and the path should be defined in kind of ‘viewpoint’ manner, depicted in the image above in the form of the red ‘triangle’. I hope the image makes things more clear to what I am trying to convey.
I hope that someone can guide me to what math is involved in achieving this problem.
[UPDATE]
I have tried the calculations that you have told me, but it’s not working properly, since as you can see from the image, bot1 shouldn’t be able to see Bot2 . Here is an example :
alt text http://img12.imageshack.us/img12/7416/examplebattle2.png
In the above scenario, Bot 1 is checking if he can see Bot 2. Here are the details (according to Waylon Flinn’s answer):
angleOfSight = 0.69813170079773179 //in radians (40 degrees) orientation = 3.3 //Bot1's current heading (191 degrees) x1 = 518 //Bot1's Center X y1 = 277 //Bot1's Center Y x2 = 276 //Bot2's Center X y2 = 308 //Bot2's Center Y cx = x2 - x1 = 276 - 518 = -242 cy = y2 - y1 = 308 - 277 = 31 azimuth = Math.Atan2(cy, cx) = 3.0141873380511295 canHit = (azimuth < orientation + angleOfSight/2) && (azimuth > orientation - angleOfSight/2) = (3.0141873380511295 < 3.3 + 0.349065850398865895) && (3.0141873380511295 > 3.3 - 0.349065850398865895) = true
According to the above calculations, Bot1 can see Bot2, but as you can see from the image, that is not possible, since they are facing different directions.
What am I doing wrong in the above calculations?
The angle between the robots is arctan(x-distance, y-distance) (most platforms provide this 2-argument arctan that does the angle adjustment for you. You then just have to check whether this angle is less than some number away from the current heading.
Edit 2020: Here’s a much more complete analysis based on the updated example code in the question and a now-deleted imageshack image.
Atan2: The key function you need to find an angle between two points is
atan2. This takes a Y-coordinate and X-coordinate of a vector and returns the angle between that vector and the positive X axis. The value will always be wrapped to lie between -Pi and Pi.Heading vs Orientation:
atan2, and in general all your math functions, work in the ‘mathematical standard coordinate system’, which means an angle of ‘0’ corresponds to directly east, and angles increase counterclockwise. Thus, an ‘mathematical angle’ ofPi / 2as given byatan2(1, 0)means an orientation of ’90 degrees counterclockwise from due east’, which matches the point (x=0, y=1). ‘Heading’ is a navigational idea that expresses orientation is a clockwise angle from due north.orientation_degrees = 90 - heading_degreesororientation_radians = Math.PI / 2 - heading_radians, or alternatively you could specify input orientations in the mathematical coordinate system rather than the nautical heading coordinate system.Checking that an angle lies between two others: Checking that an vector lies between two other vectors is not as simple as checking that the numeric angle value is between, because of the way the angles wrap at Pi/-Pi.