-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_robot.java
58 lines (47 loc) · 1.68 KB
/
test_robot.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package Spurs;
import robocode.AdvancedRobot;
import robocode.ScannedRobotEvent;
import robocode.util.Utils;
public class test_robot extends AdvancedRobot {
/*--------variables--------*/
double enemyX;
double enemyY;
/*--------variables--------*/
public void run(){
while (true){
turnGunLeft(360);
//goTo(getBattleFieldWidth() - enemyX, enemyY);
//goTo(enemyX, getBattleFieldHeight() - enemyY);
//goTo(getBattleFieldWidth() - enemyX, getBattleFieldHeight() - enemyY);
}
}
public void onScannedRobot(ScannedRobotEvent e) {
stop();
setTurnRightRadians(Math.cos(e.getBearingRadians()));
setAhead(4 * e.getVelocity());
// TODO
}
private void goTo(double x, double y) {
/* Transform our coordinates into a vector */
x -= getX();
y -= getY();
/* Calculate the angle to the target position */
double angleToTarget = Math.atan2(x, y);
/* Calculate the turn required get there */
double targetAngle = Utils.normalRelativeAngle(angleToTarget - getHeadingRadians());
/*
* The Java Hypot method is a quick way of getting the length
* of a vector. Which in this case is also the distance between
* our robot and the target location.
*/
double distance = Math.hypot(x, y);
/* This is a simple method of performing set front as back */
double turnAngle = Math.atan(Math.tan(targetAngle));
setTurnRightRadians(turnAngle);
if(targetAngle == turnAngle) {
setAhead(distance);
} else {
setBack(distance);
}
}
}