Line | Count | Source |
1 | | #include "client/camera.h" |
2 | | |
3 | | #include <algorithm> |
4 | | #include <cmath> |
5 | | |
6 | | namespace nanommo { |
7 | | |
8 | | namespace { |
9 | | |
10 | | // Up to the player's chest, so that they're in the middle of the screen. |
11 | | constexpr float kChest = 1.2f; |
12 | | |
13 | | constexpr float kAboveGround = 1.0f; |
14 | | |
15 | | // Past straight down, the camera would go over the top of the player and look |
16 | | // back the other way. Near level, it looks along the ground, and any rise in |
17 | | // between hides the player. |
18 | | constexpr float kLowest = 5.0f * DEG2RAD; |
19 | | constexpr float kHighest = 80.0f * DEG2RAD; |
20 | | |
21 | | } // namespace |
22 | | |
23 | 15 | Camera3D GameCamera() { |
24 | 15 | return {.position = {0.0f, 11.0f, 10.0f}, |
25 | 15 | .target = {0.0f, 0.0f, 0.0f}, |
26 | 15 | .up = {0.0f, 1.0f, 0.0f}, |
27 | 15 | .fovy = 50.0f, |
28 | 15 | .projection = CAMERA_PERSPECTIVE}; |
29 | 15 | } |
30 | | |
31 | | void FollowCamera::Update(Vector3 player, float turn, float tilt, float zoom, |
32 | 0 | const Terrain& terrain) { |
33 | 0 | yaw_ += turn; |
34 | 0 | pitch_ = std::clamp(pitch_ + tilt, kLowest, kHighest); |
35 | 0 | distance_ = std::clamp(distance_ * (1.0f - zoom), kNearest, kFarthest); |
36 | |
|
37 | 0 | const Vector3 target{player.x, player.y + kChest, player.z}; |
38 | 0 | Vector3 position{target.x + distance_ * std::cos(pitch_) * std::sin(yaw_), |
39 | 0 | target.y + distance_ * std::sin(pitch_), |
40 | 0 | target.z + distance_ * std::cos(pitch_) * std::cos(yaw_)}; |
41 | | // Up a hill behind the player, it would see the inside of the hill. |
42 | 0 | position.y = std::max(position.y, |
43 | 0 | terrain.Height(position.x, position.z) + kAboveGround); |
44 | 0 | camera_.position = position; |
45 | 0 | camera_.target = target; |
46 | 0 | } |
47 | | |
48 | 0 | Vector3 FollowCamera::Relative(Vector3 direction) const { |
49 | 0 | const float cos = std::cos(yaw_); |
50 | 0 | const float sin = std::sin(yaw_); |
51 | 0 | return {direction.x * cos + direction.z * sin, 0.0f, |
52 | 0 | direction.z * cos - direction.x * sin}; |
53 | 0 | } |
54 | | |
55 | | } // namespace nanommo |