6

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
  • Is this a purely 2D problem or is your drawing a 2D representation of a 3D problem? – DJClayworth Mar 06 '09 at 20:59
  • No, it's purely a 2D platform. – Andreas Grech Mar 06 '09 at 21:42
  • Aw dude. It depends relative to what you're taking your orientation. For the above code to work the orientation has to be relative to the horizontal vector (1,0) otherwise you'll get results like you're getting above. Its just a matter of adding or subtractng PI/2 to your current orientation. – Il-Bhima Mar 11 '09 at 08:41
  • I believe Bhima is correct. It looks like you're measuring the heading from the positive Y axis. It needs to be measured from the positive X axis. Add π/2 and it should work. – Waylon Flinn Mar 11 '09 at 13:37
  • One other thing you'll have to watch out for: Atan returns values in the range (-pi, pi) your orientation is likely in the range (0, 2pi). – Waylon Flinn Mar 11 '09 at 13:47
  • So from the above code, the orientation should be: orientation + (pi/2) = 4.9043751981040664 ? – Andreas Grech Mar 11 '09 at 13:58
  • (excuse my ignorance in math) Waylon, you mentioned that I should watch out for the (-pi,pi)...how do I actually do this ? – Andreas Grech Mar 11 '09 at 14:04
  • It looks like you're actually measuring the angle in the direction opposite the usual. You therefore need to Subtract pi/2 instead of Add it. This works only because computer screens typically use down for positive Y and you seem to be using that convention. – Waylon Flinn Mar 11 '09 at 14:23
  • To correct for (-pi, pi) just add 2*pi if the result of Atan is negative. – Waylon Flinn Mar 11 '09 at 14:30
  • Yes, I think it is working now (I will do some tests and will confirm later on) Thanks a lot. I appreciate the help and input from everyone. I wish I can Accept Waylon Flinn's answer but the Bounty system auto-accepted another answer and I can't revert it. – Andreas Grech Mar 11 '09 at 14:42
  • One more thing: when you subtract pi/2 you'll have to apply the same correction you do for Atan; add 2*pi if it's negative. – Waylon Flinn Mar 12 '09 at 01:29
  • Apologies if you've worked this out, but not only is the angle returned by atan2(cy,cx) in the counterclockwise direction but the zero angle is 'east'. If Bot1 were actually facing 191 degrees by this measure it WOULD see Bot2. – DJClayworth Mar 12 '09 at 21:17
  • 1
    It sure would be nice to hear some success stories of the bounty system because I've seen nothing but failure so far. – Jon 'links in bio' Ericson Mar 14 '09 at 00:15
  • 1
    Ahh the old good robocode. I've always wanted to create a WallsHunter robot that predicts where the damn wall robot will be. But never got the chance. – OscarRyz Mar 14 '09 at 00:19
  • I'm surprised people continue to tag this question WPF. Both the question and the answer are almost completely general. If I had to give it an implementation specific label it would be C#. – Waylon Flinn Mar 16 '09 at 15:31
  • Please fix or remove the imageshack links – Cœur Jul 29 '17 at 10:44
  • @Cœur It's been eight years since I opened the question. It's a bit difficult to fix them at this point. – Andreas Grech Aug 01 '17 at 01:58
  • I'm aware it's old. but then you should remove the dead links to imageshack I believe. – Cœur Aug 01 '17 at 02:01

9 Answers9

3

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.

  1. 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.

  2. 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" of Pi / 2 as given by atan2(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.

    • Analysis: In the now-deleted imageshack image, your "heading" of 191 degrees corresponded to a south-south-west direction. This actually an trigonometric "orientation" of -101 degrees, or -1.76. The first issue in the updated code is therefore conflating "heading" and "orientation". you can get the latter from the former by orientation_degrees = 90 - heading_degrees or orientation_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.
  3. 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.

    • Analysis: in your example, the orientation is 3.3, the right edge of view is orientation 2.95, the left edge of view is 3.65. The calculated azimith is 3.0141873380511295, which happens to be correct (it does lie between). However, this would fail for azimuth values like -3, which should be calculated as "hit". See Calculating if an angle is between two angles for solutions.
Jimmy
  • 89,068
  • 17
  • 119
  • 137
  • You still have to figure out the angle from the heading vector to know if it still lies within the FOV, and until this step is laid out, this answer is only half complete. – Boon Mar 11 '09 at 06:06
  • This answer got Auto-Accepted because of the Bounty Time-Limit – Andreas Grech Mar 11 '09 at 09:04
1

Calculate the relative angle and distance of each robot relative to the current one. If the angle is within some threshold of the current heading and within the max view range, then it can see it.

The only tricky thing will be handling the boundary case where the angle goes from 2pi radians to 0.

geofftnz
  • 9,954
  • 2
  • 42
  • 50
1

Something like this within your bot's class (C# code):

/// <summary>
/// Check to see if another bot is visible from this bot's point of view.
/// </summary>
/// <param name="other">The other bot to look for.</param>
/// <returns>True iff <paramref name="other"/> is visible for this bot with the current turret angle.</returns>
private bool Sees(Bot other)
{
    // Get the actual angle of the tangent between the bots.
    var actualAngle = Math.Atan2(this.X - other.X, this.Y - other.Y) * 180/Math.PI + 360;

    // Compare that angle to a the turret angle +/- the field of vision.
    var minVisibleAngle = (actualAngle - (FOV_ANGLE / 2) + 360);
    var maxVisibleAngle = (actualAngle + (FOV_ANGLE / 2) + 360); 
    if (this.TurretAngle >= minVisibleAngle && this.TurretAngle <= maxVisibleAngle)
    {
        return true;
    }
    return false;
}

Notes:

  • The +360's are there to force any negative angles to their corresponding positive values and to shift the boundary case of angle 0 to somewhere easier to range test.
  • This might be doable using only radian angles but I think they're dirty and hard to read :/
  • See the Math.Atan2 documentation for more details.
  • I highly recommend looking into the XNA Framework, as it's created with game design in mind. However, it doesn't use WPF.

This assumes that:

  • there are no obstacles to obstruct the view
  • Bot class has X and Y properties
  • The X and Y properties are at the center of the bot.
  • Bot class a TurretAngle property which denotes the turret's positive angle relative to the x-axis, counterclockwise.
  • Bot class has a static const angle called FOV_ANGLE denoting the turret's field of vision.

Disclaimer: This is not tested or even checked to compile, adapt it as necessary.

Ben S
  • 68,394
  • 30
  • 171
  • 212
  • The minVisibleAngle (and max) are always calculating to approx. more than 600, yet the TurretAngle is >= 0 and < 360, so the condition that the TurretAngle is greater than the minVisibleAngle can never return true. What am I doing wrong ? – Andreas Grech Mar 06 '09 at 22:48
  • Btw, as regards FOV_ANGLE I'm hardcoding a 40 atm. Is that too much, too little, or is it good ? – Andreas Grech Mar 06 '09 at 22:49
  • Field of vision in typical first person shooters are 45-90 degrees. The human field of vision is approx 200 degrees to give you a frame of reference. – Ben S Mar 06 '09 at 22:56
  • A 35mm camera has a 39.6 degree angle of view (http://en.wikipedia.org/wiki/Angle_of_view) so I think 40 is perfect. – Ben S Mar 07 '09 at 15:07
  • Add 360 to the turret's angle (in the Sees function, not overall) to compare everything in the right range. I added 360 to avoid having to test the boundary case of angles near 0 – Ben S Mar 07 '09 at 15:09
1

A couple of suggestions after implementing something similar (a long time ago!):

The following assumes that you are looping through all bots on the battlefield (not a particularly nice practice, but quick and easy to get something working!)

1) Its a lot easier to check if a bot is in range then if it can currently be seen within the FOV e.g.

int range = Math.sqrt( Math.abs(my.Location.X - bots.Location.X)^2 + 
            Math.abs(my.Location.Y - bots.Location.Y)^2 );

if (range < maxRange)
{
    // check for FOV
}

This ensures that it can potentially short-cuircuit a lot of FOV checking and speed up the process of running the simulation. As a caveat, you could have some randomness here to make it more interesting, such that after a certain distance the chance to see is linearly proportional to the range of the bot.

2) This article seems to have the FOV calculation stuff on it.

3) As an AI graduate ... nave you tried Neural Networks, you could train them to recognise whether or not a robot is in range and a valid target. This would negate any horribly complex and convoluted maths! You could have a multi layer perceptron [1], [2] feed in the bots co-ordinates and the targets cordinates and recieve a nice fire/no-fire decision at the end. WARNING: I feel obliged to tell you that this methodology is not the easiest to achieve and can be horribly frustrating when it goes wrong. Due to the (simle) non-deterministic nature of this form of algorithm, debugging can be a pain. Plus you will need some form of learning either Back Propogation (with training cases) or a Genetic Algorithm (another complex process to perfect)! Given the choice I would use Number 3, but its no for everyone!

TK.
  • 46,577
  • 46
  • 119
  • 147
1

This will tell you if the center of canvas2 can be hit by canvas1. If you want to account for the width of canvas2 it gets a little more complicated. In a nutshell, you would have to do two checks, one for each of the relevant corners of canvas2, instead of one check on the center.

/// assumming canvas1 is firing on canvas2

// positions of canvas1 and canvas2, respectively
// (you're probably tracking these in your Tank objects)
int x1, y1, x2, y2;

// orientation of canvas1 (angle)
// (you're probably tracking this in your Tank objects, too)
double orientation;
// angle available for firing
// (ditto, Tank object)
double angleOfSight;

// vector from canvas1 to canvas2
int cx, cy;
// angle of vector between canvas1 and canvas2
double azimuth;
// can canvas1 hit the center of canvas2?
bool canHit;

// find the vector from canvas1 to canvas2
cx = x2 - x1;
cy = y2 - y1;

// calculate the angle of the vector
azimuth = Math.Atan2(cy, cx);
// correct for Atan range (-pi, pi)
if(azimuth < 0) azimuth += 2*Math.PI;

// determine if canvas1 can hit canvas2
// can eliminate the and (&&) with Math.Abs but this seems more instructive
canHit = (azimuth < orientation + angleOfSight) &&
    (azimuth > orientation - angleOfSight);
Waylon Flinn
  • 19,969
  • 15
  • 70
  • 72
1

It can be quite easily achieved with the use of a concept in vector math called dot product.

http://en.wikipedia.org/wiki/Dot_product

It may look intimidating, but it's not that bad. This is the most correct way to deal with your FOV issue, and the beauty is that the same math works whether you are dealing with 2D or 3D (that's when you know the solution is correct).

(NOTE: If anything is not clear, just ask in the comment section and I will fill in the missing links.)

Steps:

1) You need two vectors, one is the heading vector of the main tank. Another vector you need is derived from the position of the tank in question and the main tank.

For our discussion, let's assume the heading vector for main tank is (ax, ay) and vector between main tank's position and target tank is (bx, by). For example, if main tank is at location (20, 30) and target tank is at (45, 62), then vector b = (45 - 20, 62 - 30) = (25, 32).

Again, for purpose of discussion, let's assume main tank's heading vector is (3,4).

The main goal here is to find the angle between these two vectors, and dot product can help you get that.

2) Dot product is defined as

a * b = |a||b| cos(angle)

read as a (dot product) b since a and b are not numbers, they are vectors.

3) or expressed another way (after some algebraic manipulation):

angle = acos((a * b) / |a||b|)

angle is the angle between the two vectors a and b, so this info alone can tell you whether one tank can see another or not.

|a| is the magnitude of the vector a, which according to the Pythagoras Theorem, is just sqrt(ax * ax + ay * ay), same goes for |b|.

Now the question comes, how do you find out a * b (a dot product b) in order to find the angle.

4) Here comes the rescue. Turns out that dot product can also be expressed as below:

a * b = ax * bx + ay * by

So angle = acos((ax * bx + ay * by) / |a||b|)

If the angle is less than half of your FOV, then the tank in question is in view. Otherwise it's not.

So using the example numbers above:

Based on our example numbers:

a = (3, 4) b = (25, 32)

|a| = sqrt(3 * 3 + 4 * 4)

|b| = sqrt(25 * 25 + 32 * 32)

angle = acos((20 * 25 + 30 * 32) /|a||b|

(Be sure to convert the resulting angle to degree or radian as appropriate before comparing it to your FOV)

Boon
  • 40,656
  • 60
  • 209
  • 315
  • An adaptation of this approach (if you're smart you can eliminate the cosine inverse) is probably the one I'd employ in practice, if the code was going to be in a tight loop. Otherwise, I think the level of mathematical sophistication makes it prohibitively expensive (maintainability). – Waylon Flinn Mar 11 '09 at 06:04
  • It's not expensive as it seems. Once you lay everything down in code, it's less than ten lines of code. It's the explanation that makes it look complicated and intimidating. Inverse cosine can be optimized with other tricks but it's almost never necessary. – Boon Mar 11 '09 at 06:12
  • I agree, this code is likely faster. The problem is that it's hard for a mathematical novice to understand. My thought on inverse cosine is that you can remove it by taking the cosine of the FOV/2 once, outside the loop. You can also ensure that 'a' is a unit vector and save a multiplication. – Waylon Flinn Mar 11 '09 at 14:03
  • Good call, do cosine on FOV/2 once and avoid having to do inverse cosine each time. – Boon Mar 11 '09 at 17:36
0

Looking at both of your questions I'm thinking you can solve this problem using the math provided, you then have to solve many other issues around collision detection, firing bullets etc. These are non trivial to solve, especially if your bots aren't square. I'd recommend looking at physics engines - farseer on codeplex is a good WPF example, but this makes it into a project way bigger than a high school dev task.

Best advice I got for high marks, do something simple really well, don't part deliver something brilliant.

MrTelly
  • 14,657
  • 1
  • 48
  • 81
  • This is good advice. Collision detection using circles is easy and accurate enough for a first cut. – geofftnz Feb 18 '09 at 01:24
0

Does your turret really have that wide of a firing pattern? The path a bullet takes would be a straight line and it would not get bigger as it travels. You should have a simple vector in the direction of the the turret representing the turrets kill zone. Each tank would have a bounding circle representing their vulnerable area. Then you can proceed the way they do with ray tracing. A simple ray / circle intersection. Look at section 3 of the document Intersection of Linear and Circular Components in 2D.

John Ellinwood
  • 14,291
  • 7
  • 38
  • 48
  • That wide triangle you see in the image is the viewpoint of the tank. As in, if there is a bot in that viewpoint, the bot that is looking should be notified. But i don't know how to check if there is a bot in that particular range – Andreas Grech Mar 05 '09 at 17:00
  • its called a view frustum. standard graphics algorithm for determining what elements to draw in a 3d scene. you want to know if an object is in a view frustum from a particular eye perspective. i'll look for references. – John Ellinwood Mar 11 '09 at 00:47
0

Your updated problem seems to come from different "zero" directions of orientation and azimuth: an orientation of 0 seems to mean "straight up", but an azimuth of 0 "straight right".

Svante
  • 50,694
  • 11
  • 78
  • 122