Skip to content

Commit bb820f6

Browse files
committed
final fix instersection and GK
1 parent 869abc0 commit bb820f6

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

src/my_strategy.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl Strategy for MyStrategy {
4646
// Choose My Role 1. GK, 2. DEF, 3. OFF 4. SUP
4747
let my_role = self.coach.find_role(me, _game, _rules);
4848
// Execute My Role
49-
let oppGoal = Vec2::new(0.0, -self.rules.arena.depth/2.0);
49+
let oppGoal = Vec2::new(0.0, self.rules.arena.depth/2.0 + 20.0);
5050

5151
match my_role {
5252
Role::NONE => println!("No Role is Selected"),
@@ -69,7 +69,9 @@ fn get_bisect(seg: &Seg2, vec: &Vec2) -> Seg2{
6969
let v2 = seg.origin().rotateVector(-ang.deg());
7070
let s = Seg2::new(*vec, v2);
7171
let v3 = s.intersection(*seg);
72-
println!("AAA {:?} {:?}",ang, v3);
72+
if ! v3.is_valid() {
73+
return Seg2::new(*vec, (seg.origin() + seg.terminal()) * 0.5)
74+
}
7375
Seg2::new(*vec, v3)
7476
}
7577

@@ -78,15 +80,14 @@ impl MyStrategy {
7880

7981

8082
fn gk(&mut self) {
81-
let ball_pos = self.game.ball.position();
83+
let ball_pos = self.game.ball.position() + self.game.ball.velocity() * 0.1;
8284
let goal_line = Seg2{
8385
origin: Vec2{x: self.rules.arena.goal_width/2.0, y:-self.rules.arena.depth/2.0},
8486
terminal: Vec2{x:-self.rules.arena.goal_width/2.0, y:-self.rules.arena.depth/2.0}
8587
};
8688
let ball_seg = Seg2::new(self.game.ball.position(), self.game.ball.velocity()*100.0);
8789
let biset = get_bisect(&goal_line, &ball_pos);
8890
let mut target = biset.terminal();
89-
println!("DFAS");
9091
if self.game.ball.velocity().y < -1.0 { // KICK
9192
target = goal_line.intersection(ball_seg);
9293
if !target.is_valid() {
@@ -100,16 +101,11 @@ impl MyStrategy {
100101
} else if target.x > self.rules.arena.goal_width/2.0 - 1.5{
101102
target.x = self.rules.arena.goal_width/2.0 - 1.5;
102103
}
103-
self.gtp(&target);
104-
// let my = self.me.position();
105-
// if self.game.ball.position().dist(Vec2::new(0.0, -self.rules.arena.depth/2.0)) < 20.0 && self.game.ball.velocity().len() < 2.0 {
106-
// self.kick(&((ball_pos - my) * 10.0));
107-
// }
108104

105+
self.gtp(&target);
106+
self.action.jump_speed = 0.0;
109107
if ball_pos.dist(self.me.position()) < 3.0 && self.game.ball.height() > 2.5 {
110108
self.action.jump_speed = self.rules.ROBOT_MAX_JUMP_SPEED;
111-
} else {
112-
self.action.jump_speed = 0.0;
113109
}
114110

115111
}

src/seg2.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,31 @@ impl Seg2 {
4848
Line2::new2(self.origin, self.terminal)
4949
}
5050

51+
fn contains(&self, p: &Vec2) -> bool{
52+
(p.x - self.origin.x) * (p.x - self.terminal.x) <= EPSILON
53+
&& (p.y - self.origin.y) * (p.y - self.terminal.y) <= EPSILON
54+
}
55+
56+
fn nearest_point(&self, p: &Vec2) -> Vec2{
57+
let v = self.terminal - self.origin;
58+
let len_square = v.len() * v.len();
59+
if len_square - 0.0 < EPSILON {
60+
return self.origin;
61+
}
62+
let inner_product = v.inner_product( &(*p - self.origin) );
63+
if inner_product <= 0.0 {
64+
return self.origin;
65+
} else if inner_product >= len_square {
66+
return self.terminal;
67+
}
68+
self.origin + v * (inner_product / len_square)
69+
}
70+
5171
fn intersection(&self, other : Seg2) -> Vec2 {
52-
self.line().intersection(other.line())
72+
let sol = self.line().intersection(other.line());
73+
if ! sol.is_valid() || ! self.contains(&sol) || ! other.contains(&sol) {
74+
return VEC2INVALID;
75+
}
76+
sol
5377
}
5478
}

src/vec2.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ impl Vec2 {
6363
v
6464
}
6565

66+
fn inner_product(&self, p: &Vec2) -> f64 {
67+
self.x * p.x + self.y * p.y
68+
}
6669
}
6770

6871
// Subtraction operation for vectors

0 commit comments

Comments
 (0)