43 yaw = atan2(forward.
GetX(), forward.
GetZ());
44 pitch = -asin(forward.
GetY());
46 static_cast<float>(PitchRange.GetX() *
D2R),
47 static_cast<float>(PitchRange.GetY() *
D2R));
56 UpdateInput(deltaTime);
57 UpdateCamera(deltaTime);
61 bool cursor_set = enabled ? SDL_ShowCursor() : SDL_HideCursor();
64 SLEAK_ERROR(
"Failed to set cursor visibility {}", SDL_GetError());
68 void FreeLookCameraController::UpdateInput(
float deltaTime) {
73 SDL_GetRelativeMouseState(&x, &y);
91 void FreeLookCameraController::UpdateCamera(
float deltaTime) {
98 Math::Quaternion combinedRotation = yawRotation * pitchRotation;
103 Math::Vector3D right = forward.Cross(up).Normalized();
106 Math::Vector3D targetVelocity(
113 velocity =
Math::Lerp(velocity, targetVelocity, deltaTime * acceleration);
116 Math::Vector3D worldVelocity =
117 (right * velocity.GetX()) +
118 (up * velocity.GetY()) +
119 (forward * velocity.GetZ());
122 if (worldVelocity.Magnitude() > maxSpeed) {
123 worldVelocity = worldVelocity.Normalized() * maxSpeed;
127 velocity = velocity * (1.0f - damping * deltaTime);
130 auto* rb =
owner->GetComponent<RigidbodyComponent>();
131 if (rb && rb->HadCollision()) {
132 Math::Vector3D normal = rb->GetLastCollisionNormal();
133 float dot = worldVelocity.Dot(normal);
135 worldVelocity = worldVelocity - normal * dot;
140 camera->AddPosition(worldVelocity * deltaTime);
143 Math::Vector3D lookTarget =
camera->GetPosition() + forward;
144 camera->SetLookTarget(lookTarget);
171 void FreeLookCameraController::ApplyDamping(
float deltaTime)
174 velocity = velocity * (1.0f - damping * deltaTime);
182 void FreeLookCameraController::ClampVelocity() {
183 if(velocity.Magnitude() > maxSpeed) {
184 velocity = velocity.Normalize() * maxSpeed;
195 if (app) app->GetWindow().SetRelativeMouseMode(enabled);
207 yaw = atan2(forward.
GetX(), forward.
GetZ());
208 pitch = -asin(forward.
GetY());
211 pitch =
Math::Clamp(pitch,
static_cast<float>(PitchRange.GetX() *
D2R),
static_cast<float>(PitchRange.GetY() *
D2R));
static Application * GetInstance()
The one Application for this process, or null before construction.
virtual void SetEnabled(bool enabled)
Math::Vector3D translationInput
Math::Vector2D MousePosition
CameraController(GameObject *object)
virtual bool Initialize()
One-time setup, called after the component is attached and the owner is initialized.
static void UnregisterEvent(EventType type, std::string id)
Removes the single handler with matching id from type's handler list.
static std::string RegisterEventHandler(T *instance, void(T::*memberFunction)(const EventT &))
Registers a member-function handler bound to instance, returning an ID for later unregistration.
bool Initialize() override
Derives initial yaw/pitch from the camera's current facing direction.
void Update(float deltaTime) override
void ToggleCursor(bool enable) override
Shows or hides the OS cursor and, typically, toggles relative mouse mode.
~FreeLookCameraController() override
FreeLookCameraController(GameObject *object)
void SetEnabled(bool enabled) override
Toggles relative mouse mode and, when re-enabling, resets velocity and re-syncs yaw/pitch.
virtual void OnKeyPressed(const Sleak::Events::Input::KeyPressedEvent &e)
Updates translation input state from a key-down event; doubles speed while LCTRL is held.
virtual void OnKeyReleased(const Sleak::Events::Input::KeyReleasedEvent &e)
Clears translation input state from a key-up event.
static Vector3D Forward()
constexpr T Lerp(const T &start, const T &end, float t)
Linear interpolation from start to end; t is clamped to [0,1].
constexpr const T & Clamp(const T &value, T min, T max)
Clamps value into [min, max].
Root namespace for everything the engine exposes.