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;
}
}