Skip to content

Commit

Permalink
Fix angular velocities in replay augmenter
Browse files Browse the repository at this point in the history
  • Loading branch information
Rolv-Arild committed Jan 8, 2022
1 parent b9d3690 commit a7c8de3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 64 deletions.
4 changes: 2 additions & 2 deletions rlgym_tools/extra_state_setters/augment_setter.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def mirror_front_back(state_wrapper: StateWrapper):
for obj in [state_wrapper.ball] + state_wrapper.cars:
obj.set_pos(*(mul * obj.position))
obj.set_lin_vel(*(mul * obj.linear_velocity))
obj.set_ang_vel(*(mul * obj.angular_velocity))
obj.set_ang_vel(*(-mul * obj.angular_velocity)) # Angular velocities are negated

if isinstance(obj, CarWrapper):
pitch, yaw, roll = obj.rotation
Expand All @@ -104,7 +104,7 @@ def mirror_left_right(state_wrapper: StateWrapper):
for obj in [state_wrapper.ball] + state_wrapper.cars:
obj.set_pos(*(mul * obj.position))
obj.set_lin_vel(*(mul * obj.linear_velocity))
obj.set_ang_vel(*(mul * obj.angular_velocity))
obj.set_ang_vel(*(-mul * obj.angular_velocity)) # Angular velocities are negated

if isinstance(obj, CarWrapper):
pitch, yaw, roll = obj.rotation
Expand Down
121 changes: 60 additions & 61 deletions rlgym_tools/extra_state_setters/goalie_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

YAW_MAX = np.pi


class GoaliePracticeState(StateSetter):

def __init__(self, aerial_only=False, allow_enemy_interference=False, first_defender_in_goal=False, reset_to_max_boost=True):
def __init__(self, aerial_only=False, allow_enemy_interference=False, first_defender_in_goal=False,
reset_to_max_boost=True):
"""
GoaliePracticeState constructor.
Expand All @@ -28,8 +30,8 @@ def __init__(self, aerial_only=False, allow_enemy_interference=False, first_defe
:param reset_to_max_boost: Boolean indicating whether the cars will start each episode with 100 boost or keep from last episode
"""
super().__init__()
self.team_turn = 0 #swap every reset who's getting shot at
self.team_turn = 0 # swap every reset who's getting shot at

self.aerial_only = aerial_only
self.allow_enemy_interference = allow_enemy_interference
self.first_defender_in_goal = first_defender_in_goal
Expand All @@ -42,13 +44,14 @@ def reset(self, state_wrapper: StateWrapper):
:param state_wrapper: StateWrapper object to be modified with desired state values.
"""
self._reset_ball(state_wrapper, self.team_turn, self.aerial_only)
self._reset_cars(state_wrapper, self.team_turn, self.first_defender_in_goal, self.allow_enemy_interference, self.reset_to_max_boost)

#which team will recieve the next incoming shot
self._reset_cars(state_wrapper, self.team_turn, self.first_defender_in_goal, self.allow_enemy_interference,
self.reset_to_max_boost)

# which team will recieve the next incoming shot
self.team_turn = (self.team_turn + 1) % 2


def _reset_cars(self, state_wrapper: StateWrapper, team_turn, first_defender_in_goal, allow_enemy_interference, reset_to_max_boost):
def _reset_cars(self, state_wrapper: StateWrapper, team_turn, first_defender_in_goal, allow_enemy_interference,
reset_to_max_boost):
"""
Function to set cars in preparation for an incoming shot
Expand All @@ -61,47 +64,44 @@ def _reset_cars(self, state_wrapper: StateWrapper, team_turn, first_defender_in_
first_set = False
for car in state_wrapper.cars:
# set random position and rotation for all cars based on pre-determined ranges
if car.team_num == team_turn and not first_set:

if car.team_num == team_turn and not first_set:
if first_defender_in_goal:
y_pos = -GOAL_LINE if car.team_num == 0 else GOAL_LINE
car.set_pos(0, y_pos, z=17)
first_set = True
else:
self._place_car_in_box_area(car, team_turn)

else:
if allow_enemy_interference:
self._place_car_in_box_area(car, team_turn)

else:
self._place_car_in_box_area(car, car.team_num)


self._place_car_in_box_area(car, car.team_num)

if reset_to_max_boost:
car.boost = 100

car.set_rot(0, rand.random() * YAW_MAX - YAW_MAX/2, 0)




car.set_rot(0, rand.random() * YAW_MAX - YAW_MAX / 2, 0)

def _place_car_in_box_area(self, car, team_delin):
"""
Function to place a car in an allowed areaI
:param car: car to be modified
:param team_delin: team number delinator to look at when deciding where to place the car
"""
y_pos = (PLACEMENT_BOX_Y - (rand.random() * PLACEMENT_BOX_Y))

y_pos = (PLACEMENT_BOX_Y - (rand.random() * PLACEMENT_BOX_Y))

if team_delin == 0:
y_pos -= PLACEMENT_BOX_Y_OFFSET
else:
y_pos += PLACEMENT_BOX_Y_OFFSET
car.set_pos(rand.random() * PLACEMENT_BOX_X - PLACEMENT_BOX_X/2, y_pos, z=17)

car.set_pos(rand.random() * PLACEMENT_BOX_X - PLACEMENT_BOX_X / 2, y_pos, z=17)

def _reset_ball(self, state_wrapper: StateWrapper, team_turn, aerial_only):
"""
Function to set a new ball towards a goal
Expand All @@ -110,62 +110,61 @@ def _reset_ball(self, state_wrapper: StateWrapper, team_turn, aerial_only):
:param team_turn: team who's getting shot at
:param aerial_only: Boolean indicating whether should shots only be from the air
"""

pos, lin_vel, ang_vel = self._get_shot_parameters(team_turn, aerial_only)
state_wrapper.ball.set_pos(pos[0], pos[1], pos[2])
state_wrapper.ball.set_lin_vel(lin_vel[0], lin_vel[1], lin_vel[2])
state_wrapper.ball.set_ang_vel(ang_vel[0], ang_vel[1], ang_vel[2])

def _get_shot_parameters(self, team_turn, aerial_only):
"""
Function to set a new ball towards a goal
:param team_turn: team who's getting shot at
:param aerial_only: Boolean indicating whether should shots only be from the air
"""

# *** Magic numbers are from manually calibrated shots ***
# *** They are unrelated to numbers in other functions ***
shotpick = random.randrange(4)
INVERT_IF_BLUE = (-1 if team_turn == 0 else 1) #invert shot for blue
#random pick x value of target in goal

shotpick = random.randrange(4)
INVERT_IF_BLUE = (-1 if team_turn == 0 else 1) # invert shot for blue

# random pick x value of target in goal
x_pos = random.uniform(GOAL_X_MIN, GOAL_X_MAX)
#if its not an air shot, we can randomize the shot speed
shot_randomizer = 1 if aerial_only else (random.uniform(.2,1) )

# if its not an air shot, we can randomize the shot speed
shot_randomizer = 1 if aerial_only else (random.uniform(.2, 1))

y_vel = (3000 * INVERT_IF_BLUE) if aerial_only else (3000 * shot_randomizer * INVERT_IF_BLUE)
if shotpick == 0: #long range shot
z_pos = 1500 if aerial_only else random.uniform(100, 1500)
pos = np.array([x_pos, -3300 * INVERT_IF_BLUE, z_pos])
if shotpick == 0: # long range shot

z_pos = 1500 if aerial_only else random.uniform(100, 1500)

pos = np.array([x_pos, -3300 * INVERT_IF_BLUE, z_pos])
lin_vel = np.array([0, y_vel, 600])
elif shotpick == 1: #medium range shot
z_pos = 1550 if aerial_only else random.uniform(100, 1550)
elif shotpick == 1: # medium range shot
z_pos = 1550 if aerial_only else random.uniform(100, 1550)

pos = np.array([x_pos, -500 * INVERT_IF_BLUE, z_pos])
lin_vel = np.array([0, y_vel, 100])
elif shotpick == 2: #angled shot
z_pos = 1500 if aerial_only else random.uniform(100, 1500)
x_pos += 3200 #add offset to start the shot from the side
pos = np.array([x_pos, 0, z_pos])

elif shotpick == 2: # angled shot
z_pos = 1500 if aerial_only else random.uniform(100, 1500)
x_pos += 3200 # add offset to start the shot from the side

pos = np.array([x_pos, 0, z_pos])
lin_vel = np.array([-1900 * shot_randomizer, y_vel, 0])
elif shotpick == 3: # opposite angled shot
z_pos = 1500 if aerial_only else random.uniform(100, 1500)
x_pos -= 3200 #add offset to start the shot from the other side
pos = np.array([x_pos, 0, z_pos])

elif shotpick == 3: # opposite angled shot
z_pos = 1500 if aerial_only else random.uniform(100, 1500)
x_pos -= 3200 # add offset to start the shot from the other side

pos = np.array([x_pos, 0, z_pos])
lin_vel = np.array([1900 * shot_randomizer, y_vel, 0])
else:
print("FAULT")

ang_vel = np.array([0, 0, 0])

return pos, lin_vel, ang_vel

7 changes: 6 additions & 1 deletion rlgym_tools/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
# 3) we can import it into your module module
# https://stackoverflow.com/questions/458550/standard-way-to-embed-version-into-python-package

__version__ = '1.6.0'
__version__ = '1.6.1'

release_notes = {
'1.6.1': {
"""
- Fixed angular velocities in replay augmenter
"""
},
'1.6.0': {
"""
- Added GoaliePracticeState setter (Soren)
Expand Down

0 comments on commit a7c8de3

Please sign in to comment.