Skip to content

Commit 5c21400

Browse files
authored
Camera 2D hotfix 2 (#2200)
* Correct `Camera2D.angle` getter and adding unit test * added rotation to the camera gui example * linting pass
1 parent 22c5b05 commit 5c21400

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

arcade/camera/camera_2d.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ def angle(self) -> float:
741741
clock-wise.
742742
"""
743743
# Note that this is flipped as we want 0 degrees to be vert. Normally you have y first and then x.
744-
return degrees(atan2(self._camera_data.position[0], self._camera_data.position[1]))
744+
return degrees(atan2(self._camera_data.up[0], self._camera_data.up[1]))
745745

746746
@angle.setter
747747
def angle(self, value: float) -> None:
@@ -752,7 +752,7 @@ def angle(self, value: float) -> None:
752752
"""
753753
_r = radians(value)
754754
# Note that this is flipped as we want 0 degrees to be vert.
755-
self._camera_data.up = (-sin(_r), cos(_r), 0.0)
755+
self._camera_data.up = (sin(_r), cos(_r), 0.0)
756756

757757
@property
758758
def zoom(self) -> float:

arcade/gui/examples/gui_and_camera.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ def upgrade_spawn_rate(event: UIOnClickEvent):
154154
)
155155
self.timer.with_background(color=arcade.color.TRANSPARENT_BLACK)
156156

157+
self.cam_pos = self.ui.camera.position
158+
157159
def on_draw_before_ui(self):
158160
self.ingame_camera.use() # use the in-game camera to draw in-game objects
159161
self.sprites.draw()
@@ -230,7 +232,10 @@ def on_update(self, delta_time: float) -> Optional[bool]:
230232

231233
# slide in the UI from bottom, until total time reaches 2 seconds
232234
progress = min(1.0, self._total_time / 2)
233-
self.ui.camera.bottom_left = (0, 50 * (1 - progress))
235+
236+
# Because we allow for camera rotation we have work on the center
237+
# and not the edge because it behaves oddly otherwise
238+
self.ui.camera.position = (self.window.center_x, 50 * (1 - progress) + self.window.center_y)
234239

235240
return False
236241

@@ -241,6 +246,9 @@ def on_key_press(self, symbol: int, modifiers: int) -> Optional[bool]:
241246
arcade.close_window()
242247
if symbol == arcade.key.ENTER:
243248
self.window.show_view(MyCoinGame())
249+
if symbol == arcade.key.SPACE:
250+
# Allows user to rotate camera to show off full functionality of the ui camera
251+
self.ui.camera.angle += 5
244252

245253
return False
246254

tests/unit/camera/test_camera2d.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import pytest as pytest
44

5+
from pyglet.math import Vec2
6+
57
from arcade import Window, LRBT
68
from arcade.camera import Camera2D
79
from arcade.camera.data_types import ZeroProjectionDimension, OrthographicProjectionData
@@ -136,3 +138,17 @@ def test_move_camera_and_unproject(window: Window):
136138
screen_coordinate = camera.project(world_coordinate)
137139

138140
assert screen_coordinate == (pytest.approx(0), pytest.approx(0))
141+
142+
def test_rotate_camera_with_angle(window: Window):
143+
camera = Camera2D()
144+
camera.angle = 45
145+
assert camera.angle == pytest.approx(45.0)
146+
assert camera.up == pytest.approx(Vec2(0.5**0.5, 0.5**0.5))
147+
148+
camera.angle = 180
149+
assert camera.angle == pytest.approx(180.0)
150+
assert camera.up == pytest.approx(Vec2(0.0, -1.0))
151+
152+
camera.angle = 270
153+
assert camera.angle == pytest.approx(-90.0)
154+
assert camera.up == pytest.approx(Vec2(-1.0, 0.0))

0 commit comments

Comments
 (0)