tags:

views:

82

answers:

3

I am working on a simple 2D soccer game, while passing the ball between my players I would like to check if any of the enemy players can intercept the ball, what I would like to do is calculate a list of coordinates between my players a corridor so to speak and then check if any enemy players are in this region,

--------------------------
S                        S
--------------------------

It is easy to calculate the area between Ss when they lie like this, but how do I calculate the region when they are not aligned,

       /S /
      /  /
     /  /
    /  /
   /  /
  /  /
 / S/

EDIT: When I mean area, I want the list of coordinates in that region so that I can check those coordinates against players coordinates, not the magnitude of the area.

A: 

If it only needs to be approximate, and you need to calculate it quickly, then estimate the "diameter" of your object (even if it's square), and multiply by the distance between their centers (minus 1 radius at each end for a total of 1 diameter). This'll be blazing fast and pretty close.

Update: misread the question, I thought you literally wanted "the area".

You can still approximate it as above, but determine the side points as the points on the imaginary circles, 90 degrees off the line that connects the two. Again, it's approximate, but it keeps you from having to calculate "closest side of the object" and "closest points to a line".

Update 2:

Actually, even better: sort the points of S1 in order of how far they are from the center of S2. Pick the 2nd and 3rd closest. Sort the points of S2 in order of how far they are from the center of S1. Pick the 2nd and 3rd closest. Those are the 4 vertices. The "1st" closest is the one directly pointing at the other object, so the 2nd and 3rd are the "side" ones, in terms of facing the other object.

eruciform
updated........
eruciform
+1  A: 

Why don't you rotate your coordinate system, so that both players will be aligned? Assuming you have a fixed number of players (22 for examples) you multiply each of their (x,y) coordinate in the rotation+translation matrix that makes S1 (or S2, doesn't really matters) aligned with its companion.

And you can calculate the rotation matrix from this simple formula: http://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_given_an_axis_and_an_angle Further explanation and a good example: http://www.quantunet.com/flash8/knowledgebase/actionscript/advanced/matrix/matrix_rotation.html

Protostome
+1  A: 

(image)

Show enemy is between red lines: Calculate the distance of the enemy to the line formed by the two players (dotted line). If it is <= w/2 (w is the width of your "region"), then either the enemy is within the region, or behind one of the players.

Show enemy is between orange lines: To check that he is not behind one of the players, just check that he is between the two lines which pass through one of the players and are normal (perpendicular) to the first line (the dotted line passing through both players).

This will tell you if the enemy is within the yellow region:

BlueRaja - Danny Pflughoeft