SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
FreeLookCameraController.hpp
Go to the documentation of this file.
1#ifndef _FREELOOKCOMPONENT_HPP_
2#define _FREELOOKCOMPONENT_HPP_
3
6#include <Core/OSDef.hpp>
7#include <string>
8
9namespace Sleak {
10 /// Editor/spectator-style camera controller: mouse-look plus accelerated
11 /// free translation, independent of gravity or collision.
12 /// @ingroup scene
13 class ENGINE_API FreeLookCameraController : public CameraController {
14 public:
17
18 /// Derives initial yaw/pitch from the camera's current facing direction.
19 bool Initialize() override;
20 void Update(float deltaTime) override;
21 /// Toggles relative mouse mode and, when re-enabling, resets velocity and re-syncs yaw/pitch.
22 void SetEnabled(bool enabled) override;
23
24 void ToggleCursor(bool enable) override;
25
26 virtual void SetSpeed(float speed) {this->speed = speed;}
27 virtual float GetSpeed() const {return speed;}
28 virtual void SetAcceleration(float acceleration) {this->acceleration = acceleration;}
29 virtual float GetAcceleration() const {return acceleration;}
30 virtual void SetDamping(float damping) {this->damping = damping;}
31 virtual float GetDamping() const {return damping;}
32 virtual void SetMaxSpeed(float maxSpeed) {this->maxSpeed = maxSpeed;}
33 virtual float GetMaxSpeed() const {return maxSpeed;}
34 virtual Math::Vector3D GetVelocity() const { return velocity;}
35
37
38 void SetInvertY(bool invert) { isInvertY = invert; }
39 bool GetInvertY() const { return isInvertY; }
40
41 void SetPitchRange(Math::Vector2D range) { PitchRange = range; }
42 Math::Vector2D GetPitchRange() const { return PitchRange; }
43 void SetYawRange(Math::Vector2D range) { YawRange = range; }
44 Math::Vector2D GetYawRange() const { return YawRange; }
45 void SetRollRange(Math::Vector2D range) { RollRange = range; }
46 Math::Vector2D GetRollRange() const { return RollRange; }
47
48 void SetPitch(float pitch) { this->pitch = pitch; }
49 float GetPitch() const { return pitch; }
50 void SetYaw(float yaw) { this->yaw = yaw; }
51 float GetYaw() const { return yaw; }
52 void SetRoll(float roll) { this->roll = roll; }
53 float GetRoll() const { return roll; }
54
55
56 /// Updates translation input state from a key-down event; doubles speed while LCTRL is held.
57 virtual void OnKeyPressed(const Sleak::Events::Input::KeyPressedEvent& e);
58 /// Clears translation input state from a key-up event.
59 virtual void OnKeyReleased(const Sleak::Events::Input::KeyReleasedEvent& e);
60
61 private:
62 /// Reads and smooths mouse delta into yaw/pitch, clamped to PitchRange.
63 void UpdateInput(float deltaTime) override;
64 /// Accelerates toward the input-driven target velocity, applies damping, and moves the camera.
65 void UpdateCamera(float deltaTime) override;
66
67 /// Exponentially decays velocity toward zero when there's no translation input.
68 void ApplyDamping(float DeltaTime);
69 /// Accelerates velocity toward the input-driven target velocity.
70 void ApplyAcceleration(float DeltaTime);
71 /// Clamps velocity magnitude to maxSpeed.
72 void ClampVelocity();
73
74 Math::Vector3D velocity;
75 float speed = 1.0f;
76 float sensitivity = 0.1f;
77 float acceleration;
78 float damping;
79 float maxSpeed;
80
81 bool isInvertY = false;
82 float pitch = 0.0f;
83 float yaw = 0.0f;
84 float roll = 0.0f;
85 Math::Vector2D PitchRange;
86 Math::Vector2D YawRange;
87 Math::Vector2D RollRange;
88
89 bool m_firstFrame = true;
90
91 std::string m_keyPressedHandlerId;
92 std::string m_keyReleasedHandlerId;
93 };
94}
95
96#endif
CameraController(GameObject *object)
bool Initialize() override
Derives initial yaw/pitch from the camera's current facing direction.
virtual void SetMaxSpeed(float maxSpeed)
void ToggleCursor(bool enable) override
Shows or hides the OS cursor and, typically, toggles relative mouse mode.
void SetEnabled(bool enabled) override
Toggles relative mouse mode and, when re-enabling, resets velocity and re-syncs yaw/pitch.
virtual void SetAcceleration(float acceleration)
virtual Math::Vector3D GetVelocity() const
Root namespace for everything the engine exposes.
Definition Camera.hpp:10