Skip to content

Commit 7b92424

Browse files
committed
Refactoring the goal detector a bit.
1 parent 7688c7c commit 7b92424

3 files changed

Lines changed: 35 additions & 15 deletions

File tree

src/bot.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
from rlbot.utils.structures.game_data_struct import GameTickPacket
66
from rlbot.utils.structures.quick_chats import QuickChats
77

8+
from util.goal_detector import find_future_goal
89
from util.orientation import Orientation
10+
from util.sequence import Sequence, ControlStep
911
from util.spikes import SpikeWatcher
1012
from util.vec import Vec3
11-
from util.sequence import Sequence, ControlStep
1213

1314

1415
class MyBot(BaseAgent):
@@ -25,13 +26,13 @@ def get_output(self, packet: GameTickPacket) -> SimpleControllerState:
2526
return self.active_sequence.tick(packet)
2627

2728
self.spike_watcher.read_packet(packet)
28-
2929
ball_prediction = self.get_ball_prediction_struct()
30+
31+
# Example of predicting a goal event
3032
predicted_goal = find_future_goal(ball_prediction)
3133
goal_text = "No Goal Threats"
32-
3334
if predicted_goal:
34-
goal_text = f"Goal in {'%.4s' % str(predicted_goal[1]-packet.game_info.seconds_elapsed)}s"
35+
goal_text = f"Goal in {predicted_goal.time - packet.game_info.seconds_elapsed:.2f}s"
3536

3637
ball_location = Vec3(packet.game_ball.physics.location)
3738
my_car = packet.game_cars[self.index]
@@ -101,13 +102,3 @@ def draw_debug(renderer, text_lines: List[str]):
101102
renderer.draw_string_2d(50, y, 1, 1, line, renderer.yellow())
102103
y += 20
103104
renderer.end_rendering()
104-
105-
106-
def find_future_goal(ball_predictions):
107-
for i in range(0, ball_predictions.num_slices):
108-
# field length(5120) + ball radius(93) = 5213 however that results in false positives
109-
if abs(ball_predictions.slices[i].physics.location.y) >= 5235:
110-
# returns the position the ball crosses the goal as well as the time it's predicted to occur
111-
return [Vec3(ball_predictions.slices[i].physics.location), ball_predictions.slices[i].game_seconds]
112-
113-
return None

src/util/goal_detector.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from dataclasses import dataclass
2+
3+
from util.vec import Vec3
4+
5+
# field length(5120) + ball radius(93) = 5213 however that results in false positives
6+
GOAL_THRESHOLD = 5235
7+
8+
# We will jump this number of frames when looking for a moment where the ball is inside the goal.
9+
# Big number for efficiency, but not so big that the ball could go in and then back out during that
10+
# time span. Unit is the number of frames in the ball prediction, and the prediction is at 60 frames per second.
11+
COARSE_SEARCH_INCREMENT = 20
12+
13+
14+
@dataclass
15+
class FutureGoal:
16+
location: Vec3
17+
velocity: Vec3
18+
time: float
19+
20+
21+
def find_future_goal(ball_predictions):
22+
for coarse_index in range(0, ball_predictions.num_slices, COARSE_SEARCH_INCREMENT):
23+
if abs(ball_predictions.slices[coarse_index].physics.location.y) >= GOAL_THRESHOLD:
24+
for j in range(max(0, coarse_index - COARSE_SEARCH_INCREMENT), coarse_index):
25+
slice = ball_predictions.slices[j]
26+
if abs(slice.physics.location.y) >= GOAL_THRESHOLD:
27+
# returns the position the ball crosses the goal as well as the time it's predicted to occur
28+
return FutureGoal(Vec3(slice.physics.location), Vec3(slice.physics.velocity), slice.game_seconds)
29+
return None

src/util/spikes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def read_packet(self, packet: GameTickPacket):
1717
car = packet.game_cars[i]
1818
car_location = Vec3(car.physics.location)
1919
distance = car_location.dist(ball_location)
20-
if distance < 190:
20+
if distance < 195:
2121
if distance < closest_distance:
2222
closest_candidate = car
2323
closest_distance = distance

0 commit comments

Comments
 (0)