@@ -11,37 +11,41 @@ function tick_follow_leader(stig){
11
11
log("Trying to get leader: ", leader)
12
12
leader_obj = neighbors.get(leader) # Global to be accessible in filter
13
13
if (istable(leader_obj)){
14
-
15
- # Getting neighbors in LOS
16
- var obstructing_neighbors = neighbors.filter(function(rid, data){
17
- if(rid != leader and math.abs(data.azimuth) < MAX_LOS_ANGLE){
18
- return true
19
- }
20
- return false
21
-
22
- })
23
-
24
- # Cummulating the avoidance vector
25
- var avoidance_vec = obstructing_neighbors.reduce(function(rid, data, accum){
26
- var vec_length = -1/math.pow(data.distance, 2)
27
- var vec = math.vec2.newp(vec_length, data.azimuth)
28
- return math.vec2.add(vec, accum);
29
- },
30
- math.vec2.new(0.0, 0.0))
31
-
32
- # Goal to the leader and add the avoidance vector
33
- var goal = math.vec2.newp(leader_obj.distance - 0.5, leader_obj.azimuth)
34
-
35
- if(math.vec2.length(goal) > 1){
14
+ if(leader_obj.distance > 1.25){
15
+ # Getting neighbors in LOS
16
+ var obstructing_neighbors = neighbors.filter(function(rid, data){
17
+ if(rid != leader and data.distance < 1){
18
+ return true
19
+ }
20
+ return false
21
+
22
+ })
23
+
24
+ # Cummulating the avoidance vector
25
+ var avoidance_vec = obstructing_neighbors.reduce(function(rid, data, accum){
26
+ var vec_length = -1/math.pow(data.distance, 1.5)
27
+ var vec = math.vec2.newp(vec_length, data.azimuth)
28
+ return math.vec2.add(vec, accum);
29
+ },
30
+ math.vec2.new(0.0, 0.0))
31
+
32
+ # Goal to the leader and add the avoidance vector
33
+ var goal = math.vec2.newp(leader_obj.distance, leader_obj.azimuth)
34
+
35
+ if(math.vec2.length(goal) > 1){
36
+ goal = math.vec2.norm(goal)
37
+ }
38
+
39
+ goal = math.vec2.add(goal, avoidance_vec)
36
40
goal = math.vec2.norm(goal)
37
- }
38
-
39
- goal = math.vec2.add(goal, avoidance_vec)
40
-
41
- log("Avoidance vector: X ", avoidance_vec.x, " Y: ", avoidance_vec.y)
42
- log("Moving towards leader via: ", leader, " at x:", goal.x, " y:", goal.y)
43
- call_host_function(id, "moveBy", {.0 = goal.x, .1 = goal.y})
44
41
42
+ log("Avoidance vector: X ", avoidance_vec.x, " Y: ", avoidance_vec.y)
43
+ log("Moving towards leader via: ", leader, " at x:", goal.x, " y:", goal.y)
44
+ call_host_function(id, "moveBy", {.0 = goal.x, .1 = goal.y})
45
+ }
46
+ else {
47
+ call_host_function(id, "moveBy", {.0 = 0.0, .1 = 0.0})
48
+ }
45
49
}
46
50
else{
47
51
log("Neighbor not found!")
0 commit comments