-
Notifications
You must be signed in to change notification settings - Fork 0
/
goalk_target.m
65 lines (51 loc) · 1.67 KB
/
goalk_target.m
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
function [goalk_pos] = goalk_target (ball, goal, enemy)
persistent ball_prev_pos ball_pprev_pos mass_x mass_y
max_hist_len = 3;
if isempty(ball_prev_pos)
ball_prev_pos = [0, 0];
end
if isempty(ball_pprev_pos)
ball_pprev_pos = [0, 0];
end
if goal(1) > 0
goalk_pos(1) = goal(1) - 300;
goalk_block = goal(1) - 400;
else
goalk_pos(1) = goal(1) + 300;
goalk_block = goal(1) + 400;
end
if norm(ball_prev_pos - goal) > norm(ball.z - goal) + 5
if numel(mass_x) == 0 || mass_x(numel(mass_x)) ~= ball.x
mass_x = [mass_x, ball.x];
mass_y = [mass_y, ball.y];
end
else
mass_x = [];
mass_y = [];
end
if enemy ~= 0 && norm(vec_enemy_ball) < 175 %&& (vec_enemy_ball(1) * goal(1)) > 0
vec_enemy_ball = enemy.z-ball.z;
goalk_pos(2) = enemy.y + abs(enemy.x - goal(1)) * sin(enemy.ang) / abs(cos(enemy.ang)); %%%%%%%%%
disp('by enemy"s turning');
elseif numel(mass_x) >= max_hist_len
pol = polyfit(mass_x, mass_y, 1);
goalk_pos(2) = pol(1) * goalk_block + pol(2);
disp('polyfit');
else
goalk_pos(2) = ball.y / abs(ball.x - goal(1)) * 300;
disp('normal_goalk_mode');
end
if goalk_pos(2) > 500
goalk_pos(2) = 500;
elseif goalk_pos(2) < -500
goalk_pos(2) = -500;
end
if(numel(mass_x) > max_hist_len)
mass_x = mass_x(2:max_hist_len);
mass_y = mass_y(2:max_hist_len);
end
if ball_pprev_pos ~= ball.z
ball_prev_pos = ball_pprev_pos;
ball_pprev_pos = ball.z;
end
end