A slightly more complicated AIM algorithm-line aiming algorithm

Source: Internet
Author: User
Tags cos gety pow sin radar

Package Jiaqi;import Java.awt.geom.point2d;import Robocode. Advancedrobot;import Robocode. Rules;import Robocode. Scannedrobotevent;public class Mydemorobot extends advancedrobot{/** * Scan to enemy time */private double time1 = 0;private Enemy E  Nemy = new Enemy ();p rivate Boolean discover = false;//whether the enemy is found property private double heading = 0.0;private Double radarheading = 0.0;private double bpower = 3;private double time = 0; Bullet flight time private double distance = 3000;//The distance the enemy is about to arrive @overridepublic void Onscannedrobot (Scannedrobotevent e) { Discover = true;//found enemy time1 = GetTime ();//Initialize enemy Enemy.setbearing (E.getbearingradians ()); Enemy.setspeed ( E.getvelocity ()); Enemy.setdistance (E.getdistance ()); Enemy.setheading (E.getheadingradians ()); time = distance/ Rules.getbulletspeed (bpower);} /** * Dress up */private void dressing () {}/** * vehicle cannon and radar separate */private void Severance () {Setadjustgunforrobotturn (true); Setadjustrad Arforgunturn (TRUE);} /** * simplest move algorithm */private void Simplemove () {Double increment = 0;if (enemy.getbearing () &GT 0) {increment = MATH.PI/2-enemy.getbearing (); Setturnleftradians (increment);} else {increment = MATH.PI/2 + enemy.get Bearing (); Setturnrightradians (increment);} Setahead (1000);}  /** * Safe distance */private Double Safdis = 100;/** * Move */private void Movement () {if (getdistanceremaining () < 1) {double NX  = 0;double NY = 0;//Calculates a random safe x, y coordinate nx = Math.random () * (Getbattlefieldwidth ()-2 * safdis) + Safdis;ny = Math.random () * (Getbattlefieldheight ()-2 * Safdis) + safdis;double Headarg = 90-math.atan2 (Ny-gety (), Nx-getx ());//Calculate the rotation of our body Angle Headarg = GETPA (headarg);d ouble dis = point2d.distance (GetX (), GetY (), NX, NY);//calculate the distance that our robot moves if (Headarg-getheadingr Adians () > Math.pi/2) {Setturnrightradians (Headarg-getheadingradians () + Math.PI); Setahead (-dis);} else {SetTurnRi Ghtradians (Headarg-getheadingradians ()); Setahead (dis);}} /** * Radar lock enemy */private void Doscan () {//Determine if scan to enemy if (discover) {//Calculate the angle of the radar rotation heading = This.getheadingradians (); Radarheadi ng = This.getradarheadingraDians ();d ouble temp = radarheading-heading-enemy.getbearing ();//Handle Exception angle TEMP = correctangle (temp); temp *= 1.2;//set the robot's Radar flyback Setturnradarleftradians (temp);//start execution of the robot run settings}}/** * Select Firepower */private double FirePower () {return bpower;} Private double Correctangle (double scan) {Scan%= 2 * math.pi;//prevents the presence of a corner greater than 360 degrees if (Scan > Math.PI) {scan =-(2 * Math.PI -scan);//Calculate the angle to take negative in order for the radar to rotate in reverse direction}if (Scan <-math.pi) {scan = 2 * Math.PI + scan;} return scan;} /** * Direct aiming */private double immidate () {Double increment = heading + enemy.getbearing ()-Getgunheadingradians (); increment %= 2 * math.pi;increment = Correctangle (increment); return increment;} /** * Cannon */private Void Gun () {//double increment = immidate ();d ouble increment = line (); Setturngunrightradians (increment );} /** * Method of obtaining a positive angle * * @return */private double getpa (double angle) {angle%= 2 * math.pi;if (Angle < 0) {angle + 2 * Math . PI;} return angle;} /** * Linear Aiming * * @return */private double Line () {//Calculate the enemy's upcoming coordinates//1. Calculate enemy's current coordinates double EA = GETPA (gEtheadingradians () + enemy.getbearing ());//The enemy's direction angle double ex = GetX () + enemy.getdistance () * Math.sin (EA);//enemy current X coordinate D Ouble ey = GetY () + enemy.getdistance () * Math.Cos (EA);//enemy current y-coordinate//2.  Calculate the enemy's impending motion distance double s = 0;//save enemy Motion distance if (enemy.getspeed () >= rules.max_velocity-0.1) {s = enemy.getspeed () * TIME;} else if (Enemy.getspeed () > 0.0) {double as = (Math.pow (rules.max_velocity, 2)-Math.pow (Enemy.getspeed (), 2))/2 * Rules. acceleration;//Acceleration movement distance double vs = (Time-(Rules.max_velocity-enemy.getspeed ())/rules.acceleration) * Rules.max_ velocity;//Uniform motion Distance s = as + vs;} else {s = 0.0;} 3. Calculate enemy impending coordinates double NEXTX = ex + S * Math.sin (enemy.getheading ());d ouble nexty = ey + S * Math.Cos (enemy.getheading ());d Ista nce = Point2d.distance (GetX (), GetY (), NEXTX, nexty);d ouble t = math.atan2 (Nexty-gety (), Nextx-getx ());//calculate the angle at which the enemy is about to reach the position Degree return Correctangle ((MATH.PI/2-t-getgunheadingradians ())% (2 * math.pi));} public void Run () {///Step one: To give the robot a nice outfit, the body, the cannon and the radar and the scanned arc are going to have a unique color. Dressing ();///The second step: to let the robot's cannon and radar separate, do not affect each other. This is what advanced robots do. Severance ();//allow the radar to turn first to trigger the radar Scan Event//Setturnradarleft ();//execute (); while (true) {if (!discover) { Setturnradarleftradians (Math.PI * 2.1); execute ();} else {///Third step: To determine how the robot moves the algorithm movement ();//Fourth step: To determine how the robot chooses the firepower of the algorithm double fire = firePower ();//Fifth step: To determine how the robot locks other robot algorithms doscan ( );///Sixth step: To determine how the robot calculates the angle and adjusts the muzzle to target the enemy's algorithm gun ();/if (getgunturnremaining () <= 0) {//Fire Setfire; execute ();//}// Determine if the enemy is missing Losetarget (); Execute ();}} /** * Determine if the enemy is lost * */private void Losetarget () {if ((GetTime ()-time1) >= 4) Discover = false;}} ======================================package Jiaqi;public class Enemy {private double bearing;//the angle between himself and the enemy private Double distance;//the distance from the enemy private double heading;private double velocity;private double speed;public Double getspeed () { return speed;} public void Setspeed (double speed) {this.speed = speed;} Public double getbearing () {return bearing;} public void setbearing (double bearing) {this.bearing = bearing;} Public double getdistance () {return distance;} public void setdistance (double distance) {this.distance = distance;} Public double getheading () {return heading;} public void setheading (double heading) {this.heading = heading;} Public double getvelocity () {return velocity;} public void setvelocity (double velocity) {this.velocity = velocity;}}

  

A slightly more complicated AIM algorithm-line aiming algorithm

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.