Skip to content

Commit c0394b8

Browse files
committed
ADSF
1 parent 89f3fee commit c0394b8

File tree

4 files changed

+123
-54
lines changed

4 files changed

+123
-54
lines changed

src/dan.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,35 @@ struct DAN {
44

55
impl DAN {
66
fn dan_to_plane(point: &Vec3, point_on_plane: &Vec3, plane_normal: &Vec3) -> (f64,Vec3) {
7-
((*point - *point_on_plane).inner_product(&plane_normal) , *plane_normal)
7+
let pn = Vec3 {
8+
x: plane_normal.x,
9+
y: plane_normal.h,
10+
h: plane_normal.y,
11+
};
12+
let pop = Vec3 {
13+
x: point_on_plane.x,
14+
y: point_on_plane.h,
15+
h: point_on_plane.y,
16+
};
17+
((*point - pop).inner_product(&pn) ,pn)
818
}
919

1020
fn dan_to_sphere_inner(point: &Vec3, sphere_center: &Vec3, sphere_radius: &f64) -> (f64,Vec3) {
11-
(sphere_radius - (*point - *sphere_center).len(),(*sphere_center - *point).normalize())
21+
let sc = Vec3 {
22+
x: sphere_center.x,
23+
y: sphere_center.h,
24+
h: sphere_center.y,
25+
};
26+
(sphere_radius - (*point - sc).len(),(sc - *point).normalize())
1227
}
1328

1429
fn dan_to_sphere_outer(point: &Vec3, sphere_center: &Vec3, sphere_radius: &f64) -> (f64,Vec3) {
15-
((*point - *sphere_center).len() - sphere_radius,(*point - *sphere_center).normalize())
30+
let sc = Vec3 {
31+
x: sphere_center.x,
32+
y: sphere_center.h,
33+
h: sphere_center.y,
34+
};
35+
((*point - sc).len() - sphere_radius,(*point - sc).normalize())
1636

1737
}
1838

src/my_strategy.rs

Lines changed: 70 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ pub struct MyStrategy{
2424
rules: Rules,
2525
game: Game,
2626
action: Action,
27-
posPID : PID
2827
}
2928

3029
impl Default for MyStrategy {
@@ -35,7 +34,6 @@ impl Default for MyStrategy {
3534
rules: Rules{..Default::default()},
3635
game: Game{..Default::default()},
3736
action: Action{..Default::default()},
38-
posPID: PID::new(5.0,0.0,0.0),
3937
}
4038
}
4139
}
@@ -83,37 +81,71 @@ fn get_bisect(seg: &Seg2, vec: &Vec2) -> Seg2{
8381

8482
impl MyStrategy {
8583

84+
fn will_hit_the_ball(&self) -> bool{
85+
let mut me = self.me.clone();
86+
let mut ball = self.game.ball.clone();
87+
let mut action = self.action;
88+
let r = &self.rules;
89+
for _ in 0..100 {
90+
Self::pure_gk(&me, &ball, r, &mut action, true);
91+
let (col, vel) = Simulation::tick(&mut me, &mut ball, &action, &self.rules);
92+
if col && vel.y > 1.0 {
93+
println!("COL: {}, {:?}", col, vel);
94+
return true
95+
}
96+
}
97+
false
98+
}
8699

87-
fn gk(&mut self) {
88-
let y_goal = self.rules.arena.depth/-2.0 + 3.0;
89-
let ball_pos = self.game.ball.position();
100+
fn pure_gk(me: &Robot, ball: &Ball, rules:&Rules, action: &mut Action, s: bool) {
101+
let y_goal = rules.arena.depth/-2.0 + 3.0;
102+
let ball_pos = ball.position();
90103
let goal_line = Seg2{
91-
origin: Vec2{x: self.rules.arena.goal_width/2.0, y:y_goal},
92-
terminal: Vec2{x:self.rules.arena.goal_width/-2.0, y:y_goal}
104+
origin: Vec2{x: rules.arena.goal_width/ 2.0, y:y_goal},
105+
terminal: Vec2{x: rules.arena.goal_width/-2.0, y:y_goal}
93106
};
94-
let ball_seg = Seg2::new(self.game.ball.position(), self.game.ball.velocity()*100.0);
107+
let ball_seg = Seg2::new(ball.position(), ball.velocity()*100.0);
95108
let biset = get_bisect(&goal_line, &ball_pos);
96109
let mut target = biset.terminal();
97-
if self.game.ball.velocity().y < -1.0 { // KICK
98-
target = goal_line.intersection(ball_seg);
110+
if ball.velocity().y < -1.0 { // KICK
111+
// if ball.position().y < -15.0 {
112+
// target = ball.position();
113+
// } else {
114+
target = goal_line.intersection(ball_seg);
115+
// }
99116
if !target.is_valid() {
100117
target = Vec2::new(ball_pos.x, y_goal);
101118
}
102-
} else if self.game.ball.position().y < 0.0 {
103-
target = Vec2{x:self.game.ball.position().x, y:y_goal};
119+
} else if ball.position().y < 0.0 {
120+
target = Vec2{x: ball.position().x, y:y_goal};
104121
}
105-
if target.x < self.rules.arena.goal_width/-2.0 + 1.5 {
106-
target.x =self.rules.arena.goal_width/-2.0 + 1.5;
107-
} else if target.x > self.rules.arena.goal_width/2.0 - 1.5{
108-
target.x = self.rules.arena.goal_width/2.0 - 1.5;
122+
if target.x < rules.arena.goal_width/-2.0 + 1.5 {
123+
target.x =rules.arena.goal_width/-2.0 + 1.5;
124+
} else if target.x > rules.arena.goal_width/2.0 - 1.5{
125+
target.x = rules.arena.goal_width/2.0 - 1.5;
109126
}
110127

111-
self.gtp(&target);
112-
self.action.jump_speed = 0.0;
113-
if ball_pos.dist(self.me.position()) < 3.0 && self.game.ball.height() > 2.5 {
114-
self.action.jump_speed = self.rules.ROBOT_MAX_JUMP_SPEED;
128+
Self::gtp(&target, me, rules, action);
129+
130+
if s {
131+
action.jump_speed = rules.ROBOT_MAX_JUMP_SPEED;
132+
action.target_velocity_y = action.jump_speed;
133+
} else {
134+
action.jump_speed = 0.0;
135+
action.target_velocity_y = action.jump_speed;
115136
}
137+
}
116138

139+
fn gk(&mut self) {
140+
let y_goal = self.rules.arena.depth/-2.0 + 3.0;
141+
if self.game.ball.position().y < self.me.position().y {
142+
self.kick(&Vec2::new(0.0, -y_goal));
143+
} else {
144+
Self::pure_gk(&self.me, &self.game.ball, &self.rules,&mut self.action, false);
145+
if self.will_hit_the_ball() {
146+
self.action.jump_speed = self.rules.ROBOT_MAX_JUMP_SPEED;
147+
}
148+
}
117149
}
118150

119151
fn ballTouchPrediction(&mut self) -> Vec2 {
@@ -184,14 +216,13 @@ impl MyStrategy {
184216
if robotvel.len() > 25.0 {
185217
jump = self.game.ball.height() *4.0;
186218
}
187-
self.set_robot_vel(idealPath*3.1415/180.0 , 100.0 ,jump);
219+
Self::set_robot_vel(idealPath*3.1415/180.0 , 100.0 ,jump, &mut self.action);
188220
} else {
189221
/////
190222
if self.game.ball.height() >= 6.0 {
191223
let touchPrediction = self.ballTouchPrediction();
192224
let mut locationByPredict = touchPrediction + (touchPrediction - *target).normalize() * (0.1 + self.me.radius + self.game.ball.radius + (self.game.ball.height() - self.game.ball.radius) * 0.2) + ballVel * 0.05;
193-
194-
self.gtp(&locationByPredict);
225+
Self::gtp(&locationByPredict, &self.me, &self.rules, &mut self.action);
195226
} else {
196227

197228
////// New prediction
@@ -233,7 +264,7 @@ impl MyStrategy {
233264
}
234265

235266

236-
self.set_robot_vel(idealPath*DEG2RAD ,100.0,jump);
267+
Self::set_robot_vel(idealPath*DEG2RAD ,100.0,jump, &mut self.action);
237268
}
238269
}
239270

@@ -244,36 +275,34 @@ fn pm(&mut self, target: &Vec2) {
244275

245276
}
246277

247-
fn gtp(&mut self, targetMain: & Vec2) {
278+
fn gtp(targetMain: & Vec2, me: &Robot, _rules: &Rules, action: &mut Action) {
248279

249280
let mut target = *targetMain;
250-
if (target).y > self.rules.arena.depth / 2.0 {
251-
(target).y = self.rules.arena.depth / 2.0;
281+
if (target).y > _rules.arena.depth / 2.0 {
282+
(target).y = _rules.arena.depth / 2.0;
252283
}
253-
if target.y < self.rules.arena.depth / -2.0 {
254-
target.y = self.rules.arena.depth / -2.0;
284+
if target.y < _rules.arena.depth / -2.0 {
285+
target.y = _rules.arena.depth / -2.0;
255286
}
256-
if target.x > self.rules.arena.width / 2.0 {
257-
target.x = self.rules.arena.width / 2.0;
287+
if target.x > _rules.arena.width / 2.0 {
288+
target.x = _rules.arena.width / 2.0;
258289
}
259-
if target.x < self.rules.arena.width / -2.0 {
260-
target.x = self.rules.arena.width / -2.0;
290+
if target.x < _rules.arena.width / -2.0 {
291+
target.x = _rules.arena.width / -2.0;
261292
}
262293

263-
let dist = self.me.position().dist(target);
264-
let diff = target - self.me.position();
294+
let dist = me.position().dist(target);
295+
let diff = target - me.position();
265296
let angle = (diff.y).atan2(diff.x);
266-
let a = self.posPID.run(dist);
267-
println!("{}**{}**{}", dist, angle, a);
268-
self.set_robot_vel(angle, a , 0.0);
297+
Self::set_robot_vel(angle, 5.0 * dist , 0.0, action);
269298

270299

271300
}
272301

273-
fn set_robot_vel(&mut self, angle : f64, vel: f64, jump : f64) {
274-
self.action = Action {
302+
fn set_robot_vel(angle : f64, vel: f64, jump : f64, action: &mut Action) {
303+
*action = Action {
275304
target_velocity_x: vel*angle.cos(),
276-
target_velocity_y: 0.0,
305+
target_velocity_y: 15.0,
277306
target_velocity_z: vel*angle.sin(),
278307
jump_speed: jump,
279308
use_nitro: false,

src/simulation.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
#[derive(Copy, Clone, Debug, Default)]
12
struct Simulation {
2-
33
}
44

55
impl Simulation {
@@ -11,7 +11,7 @@ impl Simulation {
1111
*_v
1212
}
1313

14-
fn collide_entities(_a: &mut Entity3, _b: &mut Entity3, _radius_change_speed: f64, _rules: &Rules) {
14+
fn collide_entities(_a: &mut Entity3, _b: &mut Entity3, _radius_change_speed: f64, _rules: &Rules, col:&mut(&mut bool, &mut Vec3)) {
1515
let delta_position = _b.position3() - _a.position3();
1616
let distance = delta_position.len();
1717
let penetration = _a.radius() + _b.radius() - distance;
@@ -31,6 +31,8 @@ impl Simulation {
3131
let impulse = normal * (1.0 + (_rules.MIN_HIT_E + _rules.MAX_HIT_E)/2.0) * delta_vel;
3232
_a.set_velocity(&(a_vel + (impulse * k_a)));
3333
_b.set_velocity(&(b_vel - (impulse * k_b)));
34+
*col.0 = true;
35+
*col.1 = *col.1 * 0.2 + _b.velocity3() * 0.8;
3436
}
3537
}
3638
}
@@ -64,22 +66,23 @@ impl Simulation {
6466
_e.set_height(h - (_rules.GRAVITY * delta_time));
6567
}
6668

67-
fn update(_me : &mut Robot, _ball: &mut Ball, _action: &Action, _rules: &Rules, delta_time: f64) {
69+
fn update(_me : &mut Robot, _ball: &mut Ball, _action: &Action, _rules: &Rules, delta_time: f64, col:&mut(&mut bool, &mut Vec3)) {
6870
if _me.touch {
6971
let mut target_vel = Self::clamp(&_action.target_vel(), _rules.ROBOT_MAX_GROUND_SPEED);
7072
target_vel -= _me.touch_normal() * _me.touch_normal().inner_product(&target_vel);
7173
let target_vel_change = target_vel - _me.velocity3();
7274
if target_vel_change.len() > 0.0 {
7375
let acc = _rules.ROBOT_ACCELERATION * _me.touch_normal_y.unwrap().max(0.0);
7476
let robot_vel = _me.velocity3();
75-
_me.set_velocity(&(robot_vel + Self::clamp(&(_action.target_vel() * acc * delta_time), target_vel_change.len())));
77+
_me.set_velocity(&(robot_vel + Self::clamp(&(target_vel_change.normalize() * acc * delta_time), target_vel_change.len())));
7678
}
7779
// TODO : USE NITRO
78-
Self::move_e(_me, delta_time, _rules);
79-
_me.radius = _rules.ROBOT_MIN_RADIUS + (_rules.ROBOT_MAX_RADIUS - _rules.ROBOT_MIN_RADIUS) * _action.jump_speed / _rules.ROBOT_MAX_JUMP_SPEED;
8080
}
81+
Self::move_e(_me, delta_time, _rules);
82+
_me.radius = _rules.ROBOT_MIN_RADIUS + (_rules.ROBOT_MAX_RADIUS - _rules.ROBOT_MIN_RADIUS) * _action.jump_speed / _rules.ROBOT_MAX_JUMP_SPEED;
83+
8184
Self::move_e(_ball, delta_time, _rules);
82-
Self::collide_entities(_me, _ball, _action.jump_speed, _rules);
85+
Self::collide_entities(_me, _ball, _action.jump_speed, _rules, col);
8386
let collision_normal = Self::collide_with_arena(_me, _action.jump_speed, _rules);
8487
if ! collision_normal.is_valid() {
8588
_me.touch = false;
@@ -90,11 +93,21 @@ impl Simulation {
9093
Self::collide_with_arena(_ball, 0.0, _rules);
9194
}
9295

93-
fn tick(_me : &mut Robot, _ball: &mut Ball, _action: &Action, _rules: &Rules) {
96+
fn tick(_me : &mut Robot, _ball: &mut Ball, _action: &Action, _rules: &Rules) -> (bool, Vec3){
9497
let delta_time = 1.0 / _rules.TICKS_PER_SECOND as f64;
95-
for _ in 0 .. _rules.MICROTICKS_PER_TICK - 1 {
96-
Self::update(_me, _ball, _action, _rules, delta_time / _rules.MICROTICKS_PER_TICK as f64);
98+
let mut c = 0;
99+
let mut v = Vec3::default();
100+
for _ in 0 .. 99 {
101+
let mut collide = false;
102+
let mut col_vel = Vec3::default();
103+
Self::update(_me, _ball, _action, _rules, delta_time / 100.0 as f64, &mut (&mut collide, &mut col_vel));
104+
if collide {
105+
println!("COL: {:?}", _action);
106+
c += 1;
107+
v += col_vel;
108+
}
97109
}
110+
(c > 20, v * (1.0 / f64::from(c)))
98111
}
99112

100113
fn update_ball(_ball: &mut Ball, _rules: &Rules, delta_time: f64) {

src/vec3.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ impl std::ops::Add for Vec3 {
6565
}
6666
}
6767

68+
// Addition for vectors
69+
impl std::ops::AddAssign for Vec3 {
70+
fn add_assign(&mut self, b: Self) {
71+
*self = Self::new(self.x + b.x, self.y + b.y, self.h + b.h);
72+
}
73+
}
74+
6875
// Multiplying vector by a number
6976
impl std::ops::Mul<f64> for Vec3 {
7077
type Output = Self;

0 commit comments

Comments
 (0)