Skip to content

Commit 0884c33

Browse files
committed
Fix scan bug
1 parent 7290a8e commit 0884c33

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

bin/draw.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ let draw_sprite (s : t) (r : Robot.t) =
2222
| ALIVE ->
2323
(* draw the scan radar *)
2424
let res = r.scan_res in
25-
let dir = r.scan_degrees +. 90. in
25+
let dir = get_screen_degrees r.scan_degrees +. 90. in
2626
if r.scan_cycles > 0 then (
2727
draw_circle_sector (V.create x y) scan_height (dir +. res)
2828
(dir -. res) 1 (fade s.color 0.25);

lib/robot.ml

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
open Math
22
open CCFloat
33

4+
let pr = Printf.printf
5+
let debug = false
6+
47
let _robot_size = 50.
58
let _robot_mass = 50.
69
let _max_x = 1000.
@@ -52,19 +55,26 @@ let scan degree resolution =
5255
!cur_robot.scan_cycles <- _scan_duration;
5356
!cur_robot.scan_degrees <- degree;
5457
!cur_robot.scan_res <- res;
58+
if debug then
59+
pr "###### %s is scanning at %f res %f\n" !cur_robot.name degree resolution;
5560
Array.iter
5661
(fun r ->
57-
if Stdlib.(!cur_robot.id <> r.id && r.status <> DEAD) then
58-
let v1, v2 = (rayvec_of_vector !cur_robot.p, rayvec_of_vector r.p) in
59-
let d = V.angle v1 v2 * _rad2deg |> round |> normalize_degrees in
62+
if Stdlib.(!cur_robot.id <> r.id && r.status <> DEAD) then (
63+
let x, y = (!cur_robot.p.x - r.p.x, !cur_robot.p.y - r.p.y) in
64+
let d =
65+
(Float.atan2 y x * _rad2deg) + 180. |> round |> normalize_degrees
66+
in
6067
let d1, d2 =
6168
(normalize_degrees (d - res), normalize_degrees (d + res))
6269
in
70+
if debug then
71+
pr "testing %s at angle %f (%f < %f < %f)\n" r.name d d1 degree d2;
6372

64-
if is_between d1 degree d2 then
65-
let distance = V.distance v1 v2 in
73+
if is_between d1 degree d2 then (
74+
if debug then pr "detected %s\n" r.name;
75+
let distance = Math.distance !cur_robot.p r.p in
6676
if (0. < distance && distance < !close_dist) || !close_dist = 0. then
67-
close_dist := distance)
77+
close_dist := distance)))
6878
!all_robots;
6979
!close_dist |> round |> to_int
7080

0 commit comments

Comments
 (0)