nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
camera.cc
Go to the documentation of this file.
1#include "client/camera.h"
2
3#include <algorithm>
4#include <cmath>
5
6namespace nanommo {
7
8namespace {
9
10// Up to the player's chest, so that they're in the middle of the screen.
11constexpr float kChest = 1.2f;
12
13constexpr 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.
18constexpr float kLowest = 5.0f * DEG2RAD;
19constexpr float kHighest = 80.0f * DEG2RAD;
20
21} // namespace
22
23Camera3D GameCamera() {
24 return {.position = {0.0f, 11.0f, 10.0f},
25 .target = {0.0f, 0.0f, 0.0f},
26 .up = {0.0f, 1.0f, 0.0f},
27 .fovy = 50.0f,
28 .projection = CAMERA_PERSPECTIVE};
29}
30
31void FollowCamera::Update(Vector3 player, float turn, float tilt, float zoom,
32 const Terrain& terrain) {
33 yaw_ += turn;
34 pitch_ = std::clamp(pitch_ + tilt, kLowest, kHighest);
35 distance_ = std::clamp(distance_ * (1.0f - zoom), kNearest, kFarthest);
36
37 const Vector3 target{player.x, player.y + kChest, player.z};
38 Vector3 position{target.x + distance_ * std::cos(pitch_) * std::sin(yaw_),
39 target.y + distance_ * std::sin(pitch_),
40 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 position.y = std::max(position.y,
43 terrain.Height(position.x, position.z) + kAboveGround);
44 camera_.position = position;
45 camera_.target = target;
46}
47
48Vector3 FollowCamera::Relative(Vector3 direction) const {
49 const float cos = std::cos(yaw_);
50 const float sin = std::sin(yaw_);
51 return {direction.x * cos + direction.z * sin, 0.0f,
52 direction.z * cos - direction.x * sin};
53}
54
55} // namespace nanommo
void Update(Vector3 player, float turn, float tilt, float zoom, const Terrain &terrain)
Definition camera.cc:31
static constexpr float kNearest
Definition camera.h:16
Vector3 Relative(Vector3 direction) const
Definition camera.cc:48
static constexpr float kFarthest
Definition camera.h:17
float Height(float x, float z) const
On the triangles between the grid's corners, as the client draws them.
Definition terrain.cc:102
Definition chat.cc:9
Camera3D GameCamera()
Looking down at the middle of the board, from above and behind.
Definition camera.cc:23