6
\$\begingroup\$

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

\$\endgroup\$
4
  • 1
    \$\begingroup\$ This problem has been explained very well in this anser: Get position of point on circumference of circle, given an angle?. You should be able to solve your problem with that. \$\endgroup\$
    – bummzack
    Commented Nov 9, 2011 at 8:01
  • \$\begingroup\$ when you get the position of the tank... don't you already have to have gotten the object? (in other words) you should already have a pointer to the object if you've detected that an object exists at that point- otherwise what's the point of your system? I could be off base here, let me know. \$\endgroup\$ Commented Nov 9, 2011 at 9:13
  • \$\begingroup\$ @ultifinitus The function gets a on scanned Event object instead of a Robot object. So the X and Y methods are not there. \$\endgroup\$ Commented Nov 9, 2011 at 19:29
  • \$\begingroup\$ @bummzack I'm gonna try that, thx! \$\endgroup\$ Commented Nov 9, 2011 at 19:29

3 Answers 3

6
\$\begingroup\$

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
\$\endgroup\$
4
  • \$\begingroup\$ 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. \$\endgroup\$
    – zacharmarz
    Commented Nov 9, 2011 at 9:49
  • \$\begingroup\$ @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). \$\endgroup\$ Commented Nov 9, 2011 at 10:03
  • \$\begingroup\$ 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. \$\endgroup\$
    – zacharmarz
    Commented Nov 9, 2011 at 10:49
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Nov 9, 2011 at 11:40
3
\$\begingroup\$

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;
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .