6

I'm working on a school project creating a tank team in the programming game Robocode. (It's a tank battle game with self-programmed bots, in Java).

I'm programming a scout bot that scans, finds enemies and reports them to the commander bot. I only have the distance and angle to the scanned enemy bot. How can I find its X,Y-position using only these known variables?

Here's a diagram:

angle of one bot from another

Anko
  • 13,393
  • 10
  • 54
  • 82
Sven van Zoelen
  • 303
  • 1
  • 3
  • 10

3 Answers3

6

Simple?

NewPos = new Vector2(825 * sin(85 degrees), 825 * cos(85 degrees));

Most libraries use radians not degrees, so:

radians = degrees * PI / 180

If you want the coordinates in the world reference frame, you will need to get the direction of the Tank, subt add the centre Tanks origin to the result:

NewDir = TankDir + 85;
NewPos = new Vector2(825 * sin(NewDir), 825 * cos(NewDir));
NewWorldPos = NewPos + TankWorldPos
hiddensunset4
  • 2,087
  • 2
  • 17
  • 27
  • I think this will compute bad NewPos vector. It takes angle as it is related with global coordinate system. But it's related to local tank coordinate system given by tank direction vector and vector perpendicular to it. You should first recompute angle - substract part of angle, which is under X axis. – zacharmarz Nov 09 '11 at 09:49
  • @zacharmaz I already mentioned that, you first compute it in the source Tank's coordinate space, then to transfer it you just add the Tanks position in the world space (see end of my answer). – hiddensunset4 Nov 09 '11 at 10:03
  • No. You understood me wrong. By this, you shift yout NewPos somewhere (you take tanks position), but you do not transfer it to global coordinate system, because you don't take tanks rotation into account. So your options are: compute transformation matrix, which recomputes vector from tanks coordinate system to global coordinate system. Or it should be enough just to recompute angle in the beginning of your computation. – zacharmarz Nov 09 '11 at 10:49
  • @zacharmarz Fixed, forgot to write that in originally cheers. It was a simply as just offsetting the direction to begin with. No need to use matrices to transform the coordinate spaces; unless you start moving coordinate systems around arbitrarily. – hiddensunset4 Nov 09 '11 at 11:40
3

I think this will be enough if you don't use scale.

float Angle = RotationTankA - RelativeAngle;

PosTankB = PosTankA + new Vector2(cos(Angle), sin(Angle)) * Distance;
Blau
  • 3,386
  • 16
  • 19
1

I have just made up an example in C#/XNA and got it to work perfectly.

Here is the current function

public Vector2 GetPosition(Vector2 CurPos, float angle, float distance)
        {
            //Get SOH
            float op = (float)Math.Sin(angle) * distance;
            //Get CAH
            float ad = (float)Math.Cos(angle) * distance;
            //Add to old Vector
            return (CurPos + new Vector2(ad, op));
        }

Note that angle 0 means that the CurPos Tank Sprite is pointing right, In the example I made, I simple used the Left and Right arrow keys to + or - the current angle, and when drawing would simple use...

        batch.Draw(BaseSprite, Position, null, Color.White, BaseRotation, new Vector2(BaseSprite.Width / 2, BaseSprite.Height / 2), 1, SpriteEffects.None, 0);

Obviously this is in C#, but i'm sure it wouldn't be too hard to convert to Java, seeing as both languages are quite similar anyway.

ZeunO8
  • 699
  • 7
  • 20