-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCube.pde
115 lines (106 loc) · 2.44 KB
/
Cube.pde
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
class Cube {
int x;
int y;
int prex;
int prey;
int oidx;
int oidy;
float targetx =-1;
float targety =-1;
boolean isLost;
int id;
int deg;
long lastUpdate;
int count=0;
Cube(int i, boolean lost) {
id = i;
isLost=lost;
lastUpdate = System.currentTimeMillis();
}
void resetCount() {
count =0;
}
boolean isAlive(long now) {
return(now < lastUpdate+200);
}
int[] aimslow(float tx, float ty) {
int left = 0;
int right = 0;
float angleToTarget = atan2(ty-y, tx-x);
float thisAngle = deg*PI/180;
float diffAngle = thisAngle-angleToTarget;
if (diffAngle > PI) diffAngle -= TWO_PI;
if (diffAngle < -PI) diffAngle += TWO_PI;
//if in front, go forward and
if (abs(diffAngle) < HALF_PI) {
//in front
float frac = cos(diffAngle);
//println(frac);
if (diffAngle > 0) {
//up-left
left = floor(100*pow(frac, 1));
right = 100;
} else {
left = 100;
right = floor(100*pow(frac, 1));
}
} else {
//face back
if (diffAngle > 0) {
left = -30;
right = 30;
} else {
left = 30;
right = -30;
}
}
int[] res = new int[2];
res[0]= left;
res[1] = right;
return res;
}
//This function defines how the cubes aims at something
//the perceived behavior will strongly depend on this
int[] aim(float tx, float ty) {
int left = 0;
int right = 0;
float angleToTarget = atan2(ty-y, tx-x);
float thisAngle = deg*PI/180;
float diffAngle = thisAngle-angleToTarget;
if (diffAngle > PI) diffAngle -= TWO_PI;
if (diffAngle < -PI) diffAngle += TWO_PI;
//if in front, go forward and
if (abs(diffAngle) < HALF_PI) {
//in front
float frac = cos(diffAngle);
if (diffAngle > 0) {
//up-left
left = floor(100*pow(frac, 2));
right = 100;
} else {
left = 100;
right = floor(100*pow(frac, 2));
}
} else {
//face back
float frac = -cos(diffAngle);
if (diffAngle > 0) {
left = -floor(100*frac);
right = -100;
} else {
left = -100;
right = -floor(100*frac);
}
}
int[] res = new int[2];
res[0] = left;
res[1] = right;
return res;
}
float distance(Cube o) {
return distance(o.x, o.y);
}
float distance(float ox, float oy) {
return sqrt ( (x-ox)*(x-ox) + (y-oy)*(y-oy));
}
}