Introduction We have learned the absolute direction, relative direction, and whole direction system in robocode. I believe everyone is right
This is a deep experience. However, the problem arises again, and the goal of knowing the direction alone cannot be completely achieved. How can we detect enemy distance? How to precisely lock the target? What should we do with moving targets?
Why? Here we will use Java. Lang
The math class in the basic class library and some basic trigonometric functions are used to uncover these mysteries. Skyala. Li gives a more detailed explanation of the forgotten triangular ry knowledge at the end of this article.

Basic concepts of coordinates First, let's take a look at a text translation in the robocode API.

All coordinates are expressed as (x, y ).

All coordinates are represented by X and Y.

All coordinates are positive.

All coordinates are positive.

The origin (0, 0) is at the bottom left of the screen.

Coordinate origin (0, 0) in the lower left corner of the screen

Positive X is right. X is positive on the right.

Positive y is up. Y is positive above

Figure 1 shows the coordinate system in robocode. For details about the figure, see the previous article "Analysis of the direction of the basic principles of robocode ".

Figure 1

Static and Dynamic robot Test Method

Well, we know the whole coordinate system of robocode, and all the problems are easy to solve. Let's start with some interesting experiments. We still use the "Dynamic and Static robots" method for testing. This is a good way to test the robot's direction and coordinate parameters. See the following description:

Design two robots named geny and genytrack. Geny is a static robot. Its main task is to print its current coordinates to verify genytrack
Check whether its location is correct. Genytrack, as its name implies, is the target robot to be tracked. It is responsible for locking geny coordinates and printing the detected geny distance.
The X, Y coordinates and distance of the operator. Here, the math. Round method in the Java. Lang class library is used to round the data of the double type to facilitate comparison. Final table
To verify the correctness of our method.

Of course there are still a lot of interesting test methods to wait for your verification. For example, when measuring the speed, we can use the method of "tortoise and rabbit race" to test the acceleration. The method of "centrifugal gravity" can be used to test the mutual influence between gun tubes and radar tank vehicles. I believe that you will know how to use the test method by name.

Before we start, we suggest you download the source code (Resource) to see the genytrack performance. Of course, you can also refer to the supplementary instructions in the article to illustrate the code of the robocode coordinate system.

Pay attention to these two robots. We use the advancedrobot class. This is an illustration of the advanced robot. For instructions on the robocode API, refer to the "Rock 'Em, Sock 'Em robocode: Round 2" in sing Li ".

Distance Detection

To obtain the target coordinates, we must first know the distance between us and the target. The distance detection here is very simple, as long as the genytrack robot scannedrobotevent event is used
In the getdistance () method, we can get the difference between geny and you. We only need to pay attention to this point. Because robots are wide and high, robocode can be used separately.
API
The getwidth () and getheigth () methods in. The distance between the two robots is the center of both sides ., L is the distance between them, and a is wrong.
.

Figure 2

Coordinate Detection

Knowing the distance between the other side and the entire coordinate system. Let's lock our target geny. Let's take a look at Figure 3:

Figure 3

List 1:

Geny genytrack
X: 303 genyx: 303
Y: 128 geny: 128

List 1 is the data obtained using the "Dynamic and Static robots" test method. You will be surprised. Well, we have successfully detected the coordinates of our poor geny. After the surprise, you will not understand: We
How can we achieve this? Why does math, a non-robocode class library, appear to be used in the Code for solving the positive cosine and radians? Good, this is robocode: everywhere
We are amazed, and we are learning new knowledge everywhere. If you are unfamiliar with the triangular geometric solution of mathematics in the middle school age, you will learn this in the basis of the trigonometric function at the end of this article. It sets
Hook up the memories of your middle school age.

Now let's analyze what genytrack has done:

In
In the scannderobotevent event of genytrack, we first obtain the absolute bearing angle of geny, that is, the relative screen angle. And from
The distance between geny and genytrack is distance. With geny,
With the geny distance, we can find the exact coordinates of geny Based on the triangle learning basics (see the trigonometric function basics.

In addition, the sin cosine function Cos in the Java class library is based on radians (see the basis of trigonometric functions. So we use the math. toradians method to convert geny's absolute angle to radians. See list 2

List 2:

Double bearing = (getheading () + E. getbearing () % 360;
Double distance = E. getdistance ();

Base on trigonometric function: edge length = sina
* Oblique Edge length, side length = cosa * Oblique Edge length, but remember that sin and Cos in the triangle Coordinate System in robocode are different from those in the triangle Coordinate System in Our mathematics, that is, the preceding
Sina and cosa should be switched, and the length of the Peer side = cosa * is the length of the oblique side. Figure 4 shows the relationship between the angle and distance between geny and genytrack and the triangular sit used by robocode.
Standard System.

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.