当前位置:网站首页>Robocode tutorial 7 - Radar locking
Robocode tutorial 7 - Radar locking
2022-04-23 18:08:00 【dawnsun001】
In this tutorial , We're going to write such a code , It will keep the enemy from escaping our eyes . Radar locking is the basis of an efficient combat robot , because robot Only execute onScannedRobot Method , We can get information about the enemy , and onScannedRobot The method will only be called after our radar scans the enemy , Of course , The procedure called is automatic .
Radar is a ray centered on a robot , In order to be more intuitive , We'd better turn on the radar graphic display , The method is :
Get into robocode, open Options-> Preferences->viewoptions, Check Visible Scan Arcs
Remember ours Enemy Class? ? It plays a vital role in our robot . So if you're not familiar with it class Enemy You'd better go and have a look first .
stay Enemy There is one in the class direction Variable , Its value is :
direction= bearingRadian+ me.getHeadingRadians();
As this picture shows , I understand direction Variable , Radar locking is simple .
The radar scan is actually a line , When the scan line reaches the target , Trigger onScannedRobot event , Update current data , Include direction. When the program is executed to onScannedRobot Code in , The angle of the radar scan line getRadarHeadingRadians() Has been and direction Deviate from . In order to lock the target , We can scan the radar in the opposite direction . Because the radar rotates very fast , and getRadarHeadingRadians() and direction The offset of is not large , Robots are of a certain size . So the scanning line sweeps back and forth on the target , Radar locking is realized .
public voidonScannedRobot(ScannedRobotEvent e) {
enemy.update(e,this);
doubleOffset = rectify( enemy.direction-getRadarHeadingRadians() );
setTurnRadarRightRadians( Offset * 1.5);
}
This is ours onScannedRobot Method ,enemy.updata(e,this); Is to call our enemy Methods in objects , Update enemy information , Of course , Forget a little , before this , We need to generate a enemy object , The specific method is :
Enemy enemy = new Enemy();
Let's explain here rectify Method , Its function is to correct the angle , because direction Minus the orientation of our radar , It may be greater than 180 Degrees or less -180 degree , For example, when it is greater than 180 When the degree of , The angle of rotation we need is not that big , Just turn the direction by an angle . This rectify It's easy , When applied a lot later . Its code is :
public double rectify ( doubleangle )
{
if ( angle < -Math.PI )
angle += 2*Math.PI;
if ( angle > Math.PI )
angle -= 2*Math.PI;
return angle;
}
In the code ,enemy.direction -getRadarHeadingRadians() Is the offset that the radar needs to rotate . Suppose the radar scans clockwise before , that enemy.direction slightly smaller than getRadarHeadingRadians(), Negative . the rectify() After the method is corrected, it is the value to be rotated . And then use setTurnRadarRightRadians Rotating radar , The degree of rotation is offset 1.5 times , because RadarOffset Negative , So scan in the opposite direction , Ensure that whatever the goal
How to move , The scan line is always on the target . above 1.5 You can change to 2,3 Equal number . When you are in Options Open in Visible Scan Arcs After the options , You can see the Green fan , The multiple is 1.5 When , Like a line , And the multiple is 2,3 You can see that it looks like a fan .
In addition, let's talk about two other pieces of code :
setAdjustGunForRobotTurn( true );
setAdjustRadarForGunTurn( true );
Their function is to make the radar 、 artillery 、 Body movement is independent , Specific reference API manual .
At this point, our radar scanning code is completed , Run and try our “ Observer ” Well !!
attach : Observer complete code :
package cm;
import java.awt.*;
import robocode.*;
public class ObserverRobo extends AdvancedRobot
{
Enemy enemy= newEnemy();
public static double PI = Math.PI;
public void run()
{
setAdjustGunForRobotTurn(true);
setAdjustRadarForGunTurn( true );
this.setColors(Color.red, Color.blue, Color.yellow, Color.black, Color.green);
while(true){
if(enemy.name == null){
setTurnRadarRightRadians(2*PI);
execute();
}
else{
execute();
}
}
}
public void onScannedRobot(ScannedRobotEvent e)
{
enemy.update(e,this);
double Offset = rectify( enemy.direction -getRadarHeadingRadians() );
setTurnRadarRightRadians( Offset *1.5);
}
// Angle correction method , important
public double rectify ( doubleangle )
{
if ( angle < -Math.PI )
angle += 2*Math.PI;
if ( angle > Math.PI )
angle -= 2*Math.PI;
return angle;
}
public class Enemy {
public double x,y;
public String name = null;
public double headingRadian = 0.0D;
public double bearingRadian = 0.0D;
public double distance = 1000D;
public double direction = 0.0D;
public double velocity = 0.0D;
public double prevHeadingRadian = 0.0D;
public double energy = 100.0D;
public void update(ScannedRobotEvent e,AdvancedRobotme){
name = e.getName();
headingRadian =e.getHeadingRadians();
bearingRadian = e.getBearingRadians();
this.energy = e.getEnergy();
this.velocity = e.getVelocity();
this.distance = e.getDistance();
direction = bearingRadian +me.getHeadingRadians();
x = me.getX() + Math.sin( direction ) * distance;
y= me.getY() + Math.cos( direction ) * distance;
}
}
}
版权声明
本文为[dawnsun001]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230544289419.html
边栏推荐
- Implementation of object detection case based on SSD
- Vulnérabilité d'exécution de la commande de fond du panneau de commande JD - freefuck
- String function in MySQL
- Rust: how to match a string?
- Dock installation redis
- Implement a simple function to calculate the sum of all integers between M ~ n (m < n)
- Process management command
- Deep learning classic network analysis and target detection (I): r-cnn
- 解决报错max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
- C network related operations
猜你喜欢
SSD硬盘SATA接口和M.2接口区别(详细)总结
Robocode tutorial 3 - Robo machine analysis
Qt读写XML文件(含源码+注释)
7-21 wrong questions involve knowledge points.
Classification of cifar100 data set based on convolutional neural network
Nodejs安装
Robocode Tutorial 4 - robocode's game physics
Docker 安装 Redis
Clion installation tutorial
From source code to executable file
随机推荐
PowerDesigner various font settings; Preview font setting; SQL font settings
Go's gin framework learning
Nodejs安装
Random number generation of C #
C [file operation] read TXT text by line
Operators in C language
MySQL auto start settings start with systemctl start mysqld
Implement a simple function to calculate the sum of all integers between M ~ n (m < n)
C# 的数据流加密与解密
Transfer learning of five categories of pictures based on VGg
Notes on common basic usage of eigen Library
[UDS unified diagnostic service] IV. typical diagnostic service (4) - online programming function unit (0x34-0x38)
Read excel, int digital time to time
线上怎么确定期货账户安全的?
Identification verification code
C#的随机数生成
由tcl脚本生成板子对应的vivado工程
Box pointer of rust
Qtablewidget usage explanation
2022江西储能技术展会,中国电池展,动力电池展,燃料电池展