[Add] Camera modes, for first person/third person.

This commit is contained in:
Ritchie Cunningham 2025-09-16 18:59:57 +01:00
parent ad2a540554
commit eb38a34c93
5 changed files with 31 additions and 8 deletions

View File

@ -10,4 +10,8 @@ inline Vec3 operator+(const Vec3& a, const Vec3& b) {
return {a.x + b.x, a.y + b.y, a.z + b.z}; return {a.x + b.x, a.y + b.y, a.z + b.z};
} }
inline Vec3 operator*(const Vec3& v, float scalar) {
return { v.x * scalar, v.y * scalar, v.z * scalar };
}
} /* namespace BettolaMath. */ } /* namespace BettolaMath. */

View File

@ -8,6 +8,7 @@
#include <cstdlib> #include <cstdlib>
#include <cstdio> #include <cstdio>
#include "game/player.h" #include "game/player.h"
#include "graphics/camera.h"
#include "bettola.h" #include "bettola.h"
@ -18,7 +19,8 @@ const int SCREEN_HEIGHT = 600;
Bettola::Bettola(void) : Bettola::Bettola(void) :
_is_running(false), _is_running(false),
_window(nullptr), _window(nullptr),
_gl_context(nullptr) {} _gl_context(nullptr),
_camera_mode(Camera::CameraMode::FIRST_PERSON) {}
Bettola::~Bettola(void) { Bettola::~Bettola(void) {
if(_gl_context) { if(_gl_context) {
@ -87,6 +89,10 @@ void Bettola::process_events(void) {
case SDLK_S: _input.down = true; break; case SDLK_S: _input.down = true; break;
case SDLK_A: _input.left = true; break; case SDLK_A: _input.left = true; break;
case SDLK_D: _input.right = true; break; case SDLK_D: _input.right = true; break;
case SDLK_V:
_camera_mode = (_camera_mode == Camera::CameraMode::FIRST_PERSON) ?
Camera::CameraMode::THIRD_PERSON :Camera::CameraMode::FIRST_PERSON;
break;
} }
} else if(event.type == SDL_EVENT_KEY_UP) { } else if(event.type == SDL_EVENT_KEY_UP) {
switch(event.key.key) { switch(event.key.key) {
@ -115,7 +121,7 @@ void Bettola::update(double dt) {
/* Update camera to follow the local player. */ /* Update camera to follow the local player. */
const BettolaMath::Vec3 player_pos = player.get_position(); const BettolaMath::Vec3 player_pos = player.get_position();
_camera.update(player_pos); _camera.update(player_pos, _camera_mode);
} }
void Bettola::render(void) { void Bettola::render(void) {

View File

@ -32,5 +32,6 @@ private:
Renderer _renderer; Renderer _renderer;
GameClient _game_client; GameClient _game_client;
Camera _camera; Camera _camera;
Camera::CameraMode _camera_mode;
PlayerBase::InputState _input; PlayerBase::InputState _input;
}; };

View File

@ -14,15 +14,20 @@ Camera::Camera(void) :
_pitch(0.0f), _pitch(0.0f),
_distance(10.0f), _distance(10.0f),
_mouse_sensitivity(0.1f), _mouse_sensitivity(0.1f),
_zoom_sensitivity(0.5f) { _zoom_sensitivity(0.5f),
_first_person_offset({0.0f, 0.5f, 0.0f}) {
_update_camera_vectors(); _update_camera_vectors();
} }
void Camera::update(const BettolaMath::Vec3& target_pos) { void Camera::update(const BettolaMath::Vec3& target_pos, CameraMode mode) {
if(mode == CameraMode::THIRD_PERSON) {
_position.x = target_pos.x - _front.x * _distance; _position.x = target_pos.x - _front.x * _distance;
_position.y = target_pos.y - _front.y * _distance; _position.y = target_pos.y - _front.y * _distance;
_position.z = target_pos.z - _front.z * _distance; _position.z = target_pos.z - _front.z * _distance;
} else {
_position = target_pos + _first_person_offset + (_front * 0.6f);
}
} }
void Camera::process_mouse_movement(float x_offset, float y_offset) { void Camera::process_mouse_movement(float x_offset, float y_offset) {

View File

@ -5,9 +5,15 @@
class Camera { class Camera {
public: public:
enum class CameraMode {
FIRST_PERSON,
THIRD_PERSON
};
Camera(void); Camera(void);
void update(const BettolaMath::Vec3& target_pos); void set_mode(CameraMode mode);
void update(const BettolaMath::Vec3& target_pos, CameraMode mode);
void process_mouse_movement(float x_offset, float y_offset); void process_mouse_movement(float x_offset, float y_offset);
void process_mouse_scroll(float y_offset); void process_mouse_scroll(float y_offset);
@ -21,6 +27,7 @@ private:
float _yaw; float _yaw;
float _pitch; float _pitch;
float _distance; float _distance;
BettolaMath::Vec3 _first_person_offset;
BettolaMath::Vec3 _position; BettolaMath::Vec3 _position;
BettolaMath::Vec3 _front; BettolaMath::Vec3 _front;