0
# Camera System
1
2
2D camera system with view transformations, viewport management, projection controls, and smooth camera movement for dynamic view control and effects.
3
4
## Capabilities
5
6
### Camera Classes
7
8
```python { .api }
9
class Camera2D:
10
"""
11
2D camera for view transformations and viewport management.
12
"""
13
def __init__(self, viewport: tuple[int, int, int, int] = None,
14
projection: tuple[float, float, float, float] = None):
15
"""
16
Create 2D camera.
17
18
Args:
19
viewport: (x, y, width, height) viewport rectangle
20
projection: (left, right, bottom, top) projection bounds
21
"""
22
23
# Position and transformation
24
position: arcade.math.Vec2
25
zoom: float
26
angle: float
27
28
# Viewport properties
29
viewport: tuple[int, int, int, int]
30
projection: tuple[float, float, float, float]
31
32
def move_to(self, position: tuple[float, float], speed: float = 1.0) -> None:
33
"""Move camera to position with optional speed."""
34
35
def move(self, dx: float, dy: float) -> None:
36
"""Move camera by offset."""
37
38
def use(self) -> None:
39
"""Apply camera transformation to rendering."""
40
41
def resize_viewport(self, width: int, height: int) -> None:
42
"""Resize camera viewport."""
43
44
def shake(self, intensity: float, duration: float) -> None:
45
"""Apply camera shake effect."""
46
47
class OrthographicProjector:
48
"""Orthographic projection camera for 2D rendering."""
49
def __init__(self, window: arcade.Window = None):
50
"""Create orthographic projector."""
51
52
class PerspectiveProjector:
53
"""Perspective projection camera for 3D-like effects."""
54
def __init__(self, window: arcade.Window = None, fov: float = 60.0):
55
"""Create perspective projector."""
56
57
fov: float # Field of view in degrees
58
```
59
60
### Camera Functions
61
62
```python { .api }
63
def generate_view_matrix(position: tuple[float, float], angle: float = 0) -> list:
64
"""
65
Generate view transformation matrix.
66
67
Args:
68
position: Camera position (x, y)
69
angle: Rotation angle in degrees
70
71
Returns:
72
4x4 transformation matrix
73
"""
74
75
def generate_orthographic_matrix(left: float, right: float, bottom: float, top: float,
76
near: float = -1000, far: float = 1000) -> list:
77
"""
78
Generate orthographic projection matrix.
79
80
Args:
81
left: Left clipping plane
82
right: Right clipping plane
83
bottom: Bottom clipping plane
84
top: Top clipping plane
85
near: Near clipping plane
86
far: Far clipping plane
87
88
Returns:
89
4x4 projection matrix
90
"""
91
92
def generate_perspective_matrix(fov: float, aspect: float, near: float, far: float) -> list:
93
"""
94
Generate perspective projection matrix.
95
96
Args:
97
fov: Field of view in degrees
98
aspect: Aspect ratio (width/height)
99
near: Near clipping plane
100
far: Far clipping plane
101
102
Returns:
103
4x4 projection matrix
104
"""
105
```
106
107
## Usage Examples
108
109
### Basic Camera Following
110
111
```python
112
import arcade
113
114
class CameraGame(arcade.Window):
115
def __init__(self):
116
super().__init__(1024, 768, "Camera Example")
117
118
self.player_list = None
119
self.wall_list = None
120
self.camera = None
121
122
def setup(self):
123
# Create camera
124
self.camera = arcade.Camera2D()
125
126
# Create sprites
127
self.player_list = arcade.SpriteList()
128
self.wall_list = arcade.SpriteList()
129
130
# Player
131
self.player = arcade.Sprite(":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png", 0.4)
132
self.player.center_x = 500
133
self.player.center_y = 300
134
self.player_list.append(self.player)
135
136
# Create walls for larger world
137
for x in range(-1000, 2000, 64):
138
wall = arcade.Sprite(":resources:images/tiles/grassMid.png")
139
wall.center_x = x
140
wall.center_y = 0
141
self.wall_list.append(wall)
142
143
def on_draw(self):
144
self.clear()
145
146
# Use camera
147
self.camera.use()
148
149
# Draw world
150
self.wall_list.draw()
151
self.player_list.draw()
152
153
def on_update(self, delta_time):
154
self.player_list.update()
155
156
# Follow player with camera
157
self.camera.move_to((self.player.center_x, self.player.center_y), 0.1)
158
159
def on_key_press(self, key, modifiers):
160
if key == arcade.key.LEFT:
161
self.player.change_x = -5
162
elif key == arcade.key.RIGHT:
163
self.player.change_x = 5
164
165
def on_key_release(self, key, modifiers):
166
if key in (arcade.key.LEFT, arcade.key.RIGHT):
167
self.player.change_x = 0
168
169
def main():
170
game = CameraGame()
171
game.setup()
172
arcade.run()
173
174
if __name__ == "__main__":
175
main()
176
```