Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

125 rendering is flipped ud #133

Open
wants to merge 4 commits into
base: v1.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/video_recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def main():
}

env = gym.make(
"f1tenth_gym:f1tenth-v0,
"f1tenth_gym:f1tenth-v0",
config={
"map": "Spielberg",
"num_agents": 1,
Expand Down
3 changes: 3 additions & 0 deletions f1tenth_gym/envs/rendering/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np
import pygame

from .utils import to_pygame
from ..collision_models import get_vertices
from . import RenderSpec

Expand Down Expand Up @@ -217,6 +218,8 @@ def render(self, display: pygame.Surface):
self.resolution * self.ppu
)

vertices = to_pygame(vertices, height=display.get_height())

self.rect = pygame.draw.polygon(display, self.color, vertices)

pygame.draw.lines(display, (0, 0, 0), True, vertices, self.car_tickness)
Expand Down
8 changes: 7 additions & 1 deletion f1tenth_gym/envs/rendering/rendering_pygame.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Car,
TextObject,
)
from .utils import to_pygame
from ..track import Track
from .renderer import EnvRenderer, RenderSpec

Expand Down Expand Up @@ -104,8 +105,9 @@ def __init__(
window_shape=(width, height), position="top_center"
)

# load map image
# load map image (flip it vertically to match the pygame coordinate system)
original_img = track.occupancy_map
original_img = np.flipud(original_img)

self.map_renderers = {
"map": Map(map_img=original_img, zoom_level=0.4),
Expand Down Expand Up @@ -212,6 +214,7 @@ def render(self) -> Optional[np.ndarray]:
ego_x, ego_y = self.cars[self.agent_to_follow].pose[:2]
cx = (ego_x - origin[0]) / resolution
cy = (ego_y - origin[1]) / resolution
cx, cy = to_pygame(np.array([cx, cy]), height=self.map_canvas.get_height())
else:
cx = self.map_canvas.get_width() / 2
cy = self.map_canvas.get_height() / 2
Expand Down Expand Up @@ -330,6 +333,7 @@ def render_points(
ppu = self.ppus[self.active_map_renderer]
resolution = self.map_resolution * ppu
points = ((points - origin[:2]) / resolution).astype(int)
points = to_pygame(points, height=self.map_canvas.get_height())
size = math.ceil(size / ppu)

for point in points:
Expand Down Expand Up @@ -357,6 +361,7 @@ def render_lines(
ppu = self.ppus[self.active_map_renderer]
resolution = self.map_resolution * ppu
points = ((points - origin[:2]) / resolution).astype(int)
points = to_pygame(points, height=self.map_canvas.get_height())
size = math.ceil(size / ppu)

pygame.draw.lines(
Expand Down Expand Up @@ -385,6 +390,7 @@ def render_closed_lines(
ppu = self.ppus[self.active_map_renderer]
resolution = self.map_resolution * ppu
points = ((points - origin[:2]) / resolution).astype(int)
points = to_pygame(points, height=self.map_canvas.get_height())
size = math.ceil(size / ppu)

pygame.draw.lines(
Expand Down
8 changes: 8 additions & 0 deletions f1tenth_gym/envs/rendering/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import numpy as np


def to_pygame(coords: np.ndarray, height: int) -> np.ndarray:
"""Convert coordinates into pygame coordinates (lower-left => top left)."""
if coords.ndim == 1:
return np.array([coords[0], height - coords[1]])
return np.array([coords[:, 0], height - coords[:, 1]]).T
Loading