Skip to content

Commit

Permalink
Fix scale for Car Racing (#2831)
Browse files Browse the repository at this point in the history
* Shift relative position of coordinates in render

* Format file
  • Loading branch information
andrewtanJS authored May 23, 2022
1 parent a37b956 commit 8f9b62f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
17 changes: 3 additions & 14 deletions gym/envs/box2d/car_dynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@
WHEEL_WHITE = (77, 77, 77)
MUD_COLOR = (102, 102, 0)

SCALE = 6.0 # Track scale
PLAYFIELD = 2000 / SCALE # Game over boundary


class Car:
def __init__(self, world, init_angle, init_x, init_y):
Expand Down Expand Up @@ -267,10 +264,7 @@ def draw(self, surface, zoom, translation, angle, draw_particles=True):

if draw_particles:
for p in self.particles:
poly = [
(coords[0] + PLAYFIELD, coords[1] + PLAYFIELD) for coords in p.poly
]
poly = [pygame.math.Vector2(c).rotate_rad(angle) for c in poly]
poly = [pygame.math.Vector2(c).rotate_rad(angle) for c in p.poly]
poly = [
(
coords[0] * zoom + translation[0],
Expand All @@ -286,9 +280,7 @@ def draw(self, surface, zoom, translation, angle, draw_particles=True):
for f in obj.fixtures:
trans = f.body.transform
path = [trans * v for v in f.shape.vertices]
path = [
(coords[0] + PLAYFIELD, coords[1] + PLAYFIELD) for coords in path
]
path = [(coords[0], coords[1]) for coords in path]
path = [pygame.math.Vector2(c).rotate_rad(angle) for c in path]
path = [
(
Expand Down Expand Up @@ -323,10 +315,7 @@ def draw(self, surface, zoom, translation, angle, draw_particles=True):
]
white_poly = [trans * v for v in white_poly]

white_poly = [
(coords[0] + PLAYFIELD, coords[1] + PLAYFIELD)
for coords in white_poly
]
white_poly = [(coords[0], coords[1]) for coords in white_poly]
white_poly = [
pygame.math.Vector2(c).rotate_rad(angle) for c in white_poly
]
Expand Down
18 changes: 9 additions & 9 deletions gym/envs/box2d/car_racing.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,8 @@ def render(self, mode: str = "human"):
angle = -self.car.hull.angle
# Animating first second zoom.
zoom = 0.1 * SCALE * max(1 - self.t, 0) + ZOOM * SCALE * min(self.t, 1)
scroll_x = -(self.car.hull.position[0] + PLAYFIELD) * zoom
scroll_y = -(self.car.hull.position[1] + PLAYFIELD) * zoom
scroll_x = -(self.car.hull.position[0]) * zoom
scroll_y = -(self.car.hull.position[1]) * zoom
trans = pygame.math.Vector2((scroll_x, scroll_y)).rotate_rad(angle)
trans = (WINDOW_W / 2 + trans[0], WINDOW_H / 4 + trans[1])

Expand Down Expand Up @@ -549,10 +549,10 @@ def render(self, mode: str = "human"):
def _render_road(self, zoom, translation, angle):
bounds = PLAYFIELD
field = [
(2 * bounds, 2 * bounds),
(2 * bounds, 0),
(0, 0),
(0, 2 * bounds),
(bounds, bounds),
(bounds, -bounds),
(-bounds, -bounds),
(-bounds, bounds),
]

# draw background
Expand All @@ -562,8 +562,8 @@ def _render_road(self, zoom, translation, angle):

# draw grass patches
grass = []
for x in range(0, 40, 2):
for y in range(0, 40, 2):
for x in range(-20, 20, 2):
for y in range(-20, 20, 2):
grass.append(
[
(GRASS_DIM * x + GRASS_DIM, GRASS_DIM * y + 0),
Expand All @@ -580,7 +580,7 @@ def _render_road(self, zoom, translation, angle):
# draw road
for poly, color in self.road_poly:
# converting to pixel coordinates
poly = [(p[0] + PLAYFIELD, p[1] + PLAYFIELD) for p in poly]
poly = [(p[0], p[1]) for p in poly]
color = [int(c) for c in color]
self._draw_colored_polygon(self.surf, poly, color, zoom, translation, angle)

Expand Down

0 comments on commit 8f9b62f

Please sign in to comment.