|
| 1 | +-- The initial zoom level |
| 2 | +go.property("zoom", 3) |
| 3 | +-- The speed of the zoom |
| 4 | +go.property("zoom_speed", 0.1) |
| 5 | +-- The speed of the rotation |
| 6 | +go.property("rotation_speed", 0.5) |
| 7 | +-- The offset of the camera from the origin |
| 8 | +go.property("offset", vmath.vector3(0, 0, 0)) |
| 9 | + |
| 10 | +function init(self) |
| 11 | + -- Acquire input focus to receive input events |
| 12 | + msg.post(".", "acquire_input_focus") |
| 13 | + |
| 14 | + -- Initialize start values |
| 15 | + self.yaw = go.get(".", "euler.y") |
| 16 | + self.pitch = go.get(".", "euler.x") |
| 17 | + self.zoom_offset = 0 |
| 18 | + self.current_yaw = self.yaw |
| 19 | + self.current_pitch = self.pitch |
| 20 | + self.current_zoom = self.zoom_offset |
| 21 | +end |
| 22 | + |
| 23 | +function update(self, dt) |
| 24 | + -- Animate camera rotation and zoom |
| 25 | + self.current_yaw = vmath.lerp(0.15, self.current_yaw, self.yaw) |
| 26 | + self.current_pitch = vmath.lerp(0.15, self.current_pitch, self.pitch) |
| 27 | + self.current_zoom = vmath.lerp(0.15, self.current_zoom, self.zoom_offset) |
| 28 | + |
| 29 | + -- Calculate rotation and position |
| 30 | + local camera_yaw = vmath.quat_rotation_y(math.rad(self.current_yaw)) |
| 31 | + local camera_pitch = vmath.quat_rotation_x(math.rad(self.current_pitch)) |
| 32 | + local camera_rotation = camera_yaw * camera_pitch |
| 33 | + local camera_position = self.offset + vmath.rotate(camera_rotation, vmath.vector3(0, 0, self.zoom + self.current_zoom)) |
| 34 | + |
| 35 | + -- Set camera position and rotation |
| 36 | + go.set_position(camera_position) |
| 37 | + go.set_rotation(camera_rotation) |
| 38 | +end |
| 39 | + |
| 40 | +function on_input(self, action_id, action) |
| 41 | + if action_id == hash("touch") and not action.pressed then |
| 42 | + self.yaw = self.yaw - action.dx * self.rotation_speed |
| 43 | + self.pitch = self.pitch + action.dy * self.rotation_speed |
| 44 | + elseif action_id == hash("wheel_up") then |
| 45 | + self.zoom_offset = self.zoom_offset - self.zoom * self.zoom_speed |
| 46 | + elseif action_id == hash("wheel_down") then |
| 47 | + self.zoom_offset = self.zoom_offset + self.zoom * self.zoom_speed |
| 48 | + end |
| 49 | +end |
0 commit comments