mirror of
https://github.com/Farama-Foundation/Gymnasium.git
synced 2025-08-31 10:09:53 +00:00
Fix scale for Car Racing (#2831)
* Shift relative position of coordinates in render * Format file
This commit is contained in:
@@ -45,9 +45,6 @@ WHEEL_COLOR = (0, 0, 0)
|
|||||||
WHEEL_WHITE = (77, 77, 77)
|
WHEEL_WHITE = (77, 77, 77)
|
||||||
MUD_COLOR = (102, 102, 0)
|
MUD_COLOR = (102, 102, 0)
|
||||||
|
|
||||||
SCALE = 6.0 # Track scale
|
|
||||||
PLAYFIELD = 2000 / SCALE # Game over boundary
|
|
||||||
|
|
||||||
|
|
||||||
class Car:
|
class Car:
|
||||||
def __init__(self, world, init_angle, init_x, init_y):
|
def __init__(self, world, init_angle, init_x, init_y):
|
||||||
@@ -267,10 +264,7 @@ class Car:
|
|||||||
|
|
||||||
if draw_particles:
|
if draw_particles:
|
||||||
for p in self.particles:
|
for p in self.particles:
|
||||||
poly = [
|
poly = [pygame.math.Vector2(c).rotate_rad(angle) for c in p.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 = [
|
poly = [
|
||||||
(
|
(
|
||||||
coords[0] * zoom + translation[0],
|
coords[0] * zoom + translation[0],
|
||||||
@@ -286,9 +280,7 @@ class Car:
|
|||||||
for f in obj.fixtures:
|
for f in obj.fixtures:
|
||||||
trans = f.body.transform
|
trans = f.body.transform
|
||||||
path = [trans * v for v in f.shape.vertices]
|
path = [trans * v for v in f.shape.vertices]
|
||||||
path = [
|
path = [(coords[0], coords[1]) for coords in path]
|
||||||
(coords[0] + PLAYFIELD, coords[1] + PLAYFIELD) for coords in path
|
|
||||||
]
|
|
||||||
path = [pygame.math.Vector2(c).rotate_rad(angle) for c in path]
|
path = [pygame.math.Vector2(c).rotate_rad(angle) for c in path]
|
||||||
path = [
|
path = [
|
||||||
(
|
(
|
||||||
@@ -323,10 +315,7 @@ class Car:
|
|||||||
]
|
]
|
||||||
white_poly = [trans * v for v in white_poly]
|
white_poly = [trans * v for v in white_poly]
|
||||||
|
|
||||||
white_poly = [
|
white_poly = [(coords[0], coords[1]) for coords in white_poly]
|
||||||
(coords[0] + PLAYFIELD, coords[1] + PLAYFIELD)
|
|
||||||
for coords in white_poly
|
|
||||||
]
|
|
||||||
white_poly = [
|
white_poly = [
|
||||||
pygame.math.Vector2(c).rotate_rad(angle) for c in white_poly
|
pygame.math.Vector2(c).rotate_rad(angle) for c in white_poly
|
||||||
]
|
]
|
||||||
|
@@ -513,8 +513,8 @@ class CarRacing(gym.Env, EzPickle):
|
|||||||
angle = -self.car.hull.angle
|
angle = -self.car.hull.angle
|
||||||
# Animating first second zoom.
|
# Animating first second zoom.
|
||||||
zoom = 0.1 * SCALE * max(1 - self.t, 0) + ZOOM * SCALE * min(self.t, 1)
|
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_x = -(self.car.hull.position[0]) * zoom
|
||||||
scroll_y = -(self.car.hull.position[1] + PLAYFIELD) * zoom
|
scroll_y = -(self.car.hull.position[1]) * zoom
|
||||||
trans = pygame.math.Vector2((scroll_x, scroll_y)).rotate_rad(angle)
|
trans = pygame.math.Vector2((scroll_x, scroll_y)).rotate_rad(angle)
|
||||||
trans = (WINDOW_W / 2 + trans[0], WINDOW_H / 4 + trans[1])
|
trans = (WINDOW_W / 2 + trans[0], WINDOW_H / 4 + trans[1])
|
||||||
|
|
||||||
@@ -549,10 +549,10 @@ class CarRacing(gym.Env, EzPickle):
|
|||||||
def _render_road(self, zoom, translation, angle):
|
def _render_road(self, zoom, translation, angle):
|
||||||
bounds = PLAYFIELD
|
bounds = PLAYFIELD
|
||||||
field = [
|
field = [
|
||||||
(2 * bounds, 2 * bounds),
|
(bounds, bounds),
|
||||||
(2 * bounds, 0),
|
(bounds, -bounds),
|
||||||
(0, 0),
|
(-bounds, -bounds),
|
||||||
(0, 2 * bounds),
|
(-bounds, bounds),
|
||||||
]
|
]
|
||||||
|
|
||||||
# draw background
|
# draw background
|
||||||
@@ -562,8 +562,8 @@ class CarRacing(gym.Env, EzPickle):
|
|||||||
|
|
||||||
# draw grass patches
|
# draw grass patches
|
||||||
grass = []
|
grass = []
|
||||||
for x in range(0, 40, 2):
|
for x in range(-20, 20, 2):
|
||||||
for y in range(0, 40, 2):
|
for y in range(-20, 20, 2):
|
||||||
grass.append(
|
grass.append(
|
||||||
[
|
[
|
||||||
(GRASS_DIM * x + GRASS_DIM, GRASS_DIM * y + 0),
|
(GRASS_DIM * x + GRASS_DIM, GRASS_DIM * y + 0),
|
||||||
@@ -580,7 +580,7 @@ class CarRacing(gym.Env, EzPickle):
|
|||||||
# draw road
|
# draw road
|
||||||
for poly, color in self.road_poly:
|
for poly, color in self.road_poly:
|
||||||
# converting to pixel coordinates
|
# 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]
|
color = [int(c) for c in color]
|
||||||
self._draw_colored_polygon(self.surf, poly, color, zoom, translation, angle)
|
self._draw_colored_polygon(self.surf, poly, color, zoom, translation, angle)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user