The Secret of Robocode master

Source: Internet
Author: User
Tags relative

By supplementing the robot in the tracking your opponents ' movement, we can add the factor wall avoidance method to the existing or annoying mobile algorithm. This method attempts to find the most probable direction in terms of the desired direction and the safety direction determined by the distance between the robot and the wall.

Add auxiliary method to do common math calculation

First, we need to add some auxiliary methods for the robot to use in common mathematical algorithms.

The Calculatebearingtoxyradians () method uses the Atan2 () method in Java.lang.Math to compute the absolute position from Sourcex,sourcey to targetx,targety and then Values are converted to relative positions relative to the sourceheading.

We also need the Normalizeabsoluteangleradians () method and the Normalizerelativeangleradians () method.

Listing 1. Mathematical Assistant Method

private static final Double DOUBLE_PI = (Math.PI * 2);
private static final Double Half_pi = (MATH.PI/2); br> public double Calculatebearingtoxyradians (double Sourcex, double Sourcey,
Double sourceheading, double targetx  , double targety) {
return Normalizerelativeangleradians (
math.atan2 (Targetx-sourcex), Targety- Sourcey))-
Sourceheading);
}
Public double Normalizeabsoluteangleradians (double angle) {
if (angle < 0) {
Return (Doub LE_PI + (angle% double_pi));   
} else {
return (angle% double_pi);

}
public static double Normalizerelativeangleradians (double angle) {
Double trimmedangle = (angle% Doub LE_PI);   
if (Trimmedangle > Math.PI) {
Return-(Math.PI-(trimmedangle% Math.PI));
   else if (Trimmedangle <-math.pi) {
Return (Math.PI + (trimmedangle% Math.PI));
   else {
return trimmedangle;
}
}

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.