SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
Sleak::FirstPersonController Class Reference

#include <FirstPersonController.hpp>

Inheritance diagram for Sleak::FirstPersonController:

Public Member Functions

 FirstPersonController (GameObject *object)
 ~FirstPersonController () override
float GetJumpZVelocity () const
float GetMaxFlySpeed () const
float GetMaxWalkSpeed () const
float GetPitch () const
float GetSprintSpeedMultiplier () const
float GetYaw () const
bool Initialize () override
 Grabs the sibling RigidbodyComponent and derives initial yaw/pitch from the camera's facing.
bool IsFlying () const
void OnKeyPressed (const Events::Input::KeyPressedEvent &e)
 Updates translation/sprint input state from a key-down event.
void OnKeyReleased (const Events::Input::KeyReleasedEvent &e)
 Clears translation/sprint input state from a key-up event.
void SetAirControl (float airControl)
void SetBrakingDeceleration (float decel)
void SetEnabled (bool enabled) override
 Toggles relative mouse mode and, when re-enabling, resets velocity and re-syncs yaw/pitch.
void SetFlying (bool flying)
 Switches between grounded movement and free-flight, resetting vertical velocity.
void SetFlySprintMultiplier (float mult)
void SetGroundFriction (float friction)
void SetJumpZVelocity (float vel)
void SetMaxAcceleration (float accel)
void SetMaxFlySpeed (float speed)
void SetMaxWalkSpeed (float speed)
void SetPitch (float pitch)
void SetSprintSpeedMultiplier (float mult)
void SetVerticalFlyInput (float v)
void SetYaw (float yaw)
void ToggleCursor (bool enable) override
 Shows or hides the OS cursor and, typically, toggles relative mouse mode.
void Update (float deltaTime) override
Public Member Functions inherited from Sleak::CameraController
 CameraController (GameObject *object)
 ~CameraController () override=default
CameraGetCamera ()
 Casts the owning GameObject to Camera; valid only when attached to one.
virtual float GetSensitivity () const
virtual bool IsEnabled () const
virtual void SetSensitivity (float sensitivity)
Public Member Functions inherited from Sleak::Component
 Component (GameObject *object)
virtual ~Component ()=default
virtual void FixedUpdate (float fixedDeltaTime)
GameObjectGetOwner ()
virtual void LateUpdate (float deltaTime)
virtual void OnDestroy ()
 Called when the component is detached or the owner is destroyed.
virtual void OnDisable ()
virtual void OnEnable ()
Public Member Functions inherited from Sleak::Object
 Object (const std::string &name="Object")
virtual ~Object ()=default
const std::string & GetName () const
uint64_t GetUniqueID () const
void SetName (const std::string &value)

Additional Inherited Members

Protected Attributes inherited from Sleak::CameraController
Cameracamera = nullptr
bool isEnabled = true
Math::Vector2D LastMousePosition
Math::Vector2D MousePosition
Math::Vector3D rotationInput
float sensitivity = 0.0f
Math::Vector3D translationInput
Protected Attributes inherited from Sleak::Component
bool bIsInitialized
GameObjectowner

Detailed Description

Grounded first-person character controller with mouse look, jumping, sprinting, and an optional flight mode.

Attach it to a Camera to get a player you can walk around with. Movement follows Unreal's CharacterMovementComponent model: acceleration toward a target speed, braking deceleration and ground friction when input stops, and deliberately low air control so a jump commits to its arc. W, A, S, D translate, the mouse looks, and pitch is clamped to plus or minus 89 degrees.

It needs a sibling RigidbodyComponent, which Initialize() looks up: the rigidbody owns vertical motion and gravity, the controller owns horizontal velocity. Add a ColliderComponent too, or the character falls through the world.

SetEnabled(false) releases the mouse and stops all input handling, which is what you want when a menu or console opens. Re-enabling resets velocity and re-syncs yaw and pitch to the camera's current facing, so the view does not snap. SetFlying(true) switches to noclip-style free flight, driven by SetVerticalFlyInput() for up and down.

auto* cam = new Sleak::Camera("PlayerCamera",
60.0f, 0.1f, 1500.0f);
cam->AddComponent<Sleak::FirstPersonController>();
cam->AddComponent<Sleak::ColliderComponent>(
Sleak::Math::Vector3D(0, 0, 0), 0.3f));
cam->AddComponent<Sleak::RigidbodyComponent>(
if (auto* rb = cam->GetComponent<Sleak::RigidbodyComponent>()) {
rb->SetUseGravity(true);
rb->SetGravity(Sleak::Math::Vector3D(0.0f, -32.0f, 0.0f));
}
if (auto* fpc = cam->GetComponent<Sleak::FirstPersonController>()) {
fpc->SetMaxWalkSpeed(4.5f);
fpc->SetSprintSpeedMultiplier(1.8f);
fpc->SetJumpZVelocity(6.0f);
fpc->SetSensitivity(0.1f);
}
AddObject(cam);
cam->Initialize();
SetActiveCamera(cam);
See also
CameraController, FreeLookCameraController, Camera, RigidbodyComponent, ColliderComponent

Definition at line 66 of file FirstPersonController.hpp.

Constructor & Destructor Documentation

◆ FirstPersonController()

Sleak::FirstPersonController::FirstPersonController ( GameObject * object)

Definition at line 13 of file FirstPersonController.cpp.

◆ ~FirstPersonController()

Sleak::FirstPersonController::~FirstPersonController ( )
override

Definition at line 23 of file FirstPersonController.cpp.

Member Function Documentation

◆ GetJumpZVelocity()

float Sleak::FirstPersonController::GetJumpZVelocity ( ) const
inline

Definition at line 84 of file FirstPersonController.hpp.

◆ GetMaxFlySpeed()

float Sleak::FirstPersonController::GetMaxFlySpeed ( ) const
inline

Definition at line 95 of file FirstPersonController.hpp.

◆ GetMaxWalkSpeed()

float Sleak::FirstPersonController::GetMaxWalkSpeed ( ) const
inline

Definition at line 80 of file FirstPersonController.hpp.

◆ GetPitch()

float Sleak::FirstPersonController::GetPitch ( ) const
inline

Definition at line 99 of file FirstPersonController.hpp.

◆ GetSprintSpeedMultiplier()

float Sleak::FirstPersonController::GetSprintSpeedMultiplier ( ) const
inline

Definition at line 82 of file FirstPersonController.hpp.

◆ GetYaw()

float Sleak::FirstPersonController::GetYaw ( ) const
inline

Definition at line 100 of file FirstPersonController.hpp.

◆ Initialize()

bool Sleak::FirstPersonController::Initialize ( )
overridevirtual

Grabs the sibling RigidbodyComponent and derives initial yaw/pitch from the camera's facing.

Reimplemented from Sleak::CameraController.

Definition at line 28 of file FirstPersonController.cpp.

◆ IsFlying()

bool Sleak::FirstPersonController::IsFlying ( ) const
inline

Definition at line 93 of file FirstPersonController.hpp.

◆ OnKeyPressed()

void Sleak::FirstPersonController::OnKeyPressed ( const Events::Input::KeyPressedEvent & e)

Updates translation/sprint input state from a key-down event.

Definition at line 191 of file FirstPersonController.cpp.

◆ OnKeyReleased()

void Sleak::FirstPersonController::OnKeyReleased ( const Events::Input::KeyReleasedEvent & e)

Clears translation/sprint input state from a key-up event.

Definition at line 212 of file FirstPersonController.cpp.

◆ SetAirControl()

void Sleak::FirstPersonController::SetAirControl ( float airControl)
inline

Definition at line 88 of file FirstPersonController.hpp.

◆ SetBrakingDeceleration()

void Sleak::FirstPersonController::SetBrakingDeceleration ( float decel)
inline

Definition at line 86 of file FirstPersonController.hpp.

◆ SetEnabled()

void Sleak::FirstPersonController::SetEnabled ( bool enabled)
overridevirtual

Toggles relative mouse mode and, when re-enabling, resets velocity and re-syncs yaw/pitch.

Reimplemented from Sleak::CameraController.

Definition at line 239 of file FirstPersonController.cpp.

◆ SetFlying()

void Sleak::FirstPersonController::SetFlying ( bool flying)

Switches between grounded movement and free-flight, resetting vertical velocity.

Definition at line 231 of file FirstPersonController.cpp.

◆ SetFlySprintMultiplier()

void Sleak::FirstPersonController::SetFlySprintMultiplier ( float mult)
inline

Definition at line 96 of file FirstPersonController.hpp.

◆ SetGroundFriction()

void Sleak::FirstPersonController::SetGroundFriction ( float friction)
inline

Definition at line 87 of file FirstPersonController.hpp.

◆ SetJumpZVelocity()

void Sleak::FirstPersonController::SetJumpZVelocity ( float vel)
inline

Definition at line 83 of file FirstPersonController.hpp.

◆ SetMaxAcceleration()

void Sleak::FirstPersonController::SetMaxAcceleration ( float accel)
inline

Definition at line 85 of file FirstPersonController.hpp.

◆ SetMaxFlySpeed()

void Sleak::FirstPersonController::SetMaxFlySpeed ( float speed)
inline

Definition at line 94 of file FirstPersonController.hpp.

◆ SetMaxWalkSpeed()

void Sleak::FirstPersonController::SetMaxWalkSpeed ( float speed)
inline

Definition at line 79 of file FirstPersonController.hpp.

◆ SetPitch()

void Sleak::FirstPersonController::SetPitch ( float pitch)
inline

Definition at line 101 of file FirstPersonController.hpp.

◆ SetSprintSpeedMultiplier()

void Sleak::FirstPersonController::SetSprintSpeedMultiplier ( float mult)
inline

Definition at line 81 of file FirstPersonController.hpp.

◆ SetVerticalFlyInput()

void Sleak::FirstPersonController::SetVerticalFlyInput ( float v)
inline

Definition at line 97 of file FirstPersonController.hpp.

◆ SetYaw()

void Sleak::FirstPersonController::SetYaw ( float yaw)
inline

Definition at line 102 of file FirstPersonController.hpp.

◆ ToggleCursor()

void Sleak::FirstPersonController::ToggleCursor ( bool enable)
overridevirtual

Shows or hides the OS cursor and, typically, toggles relative mouse mode.

Implements Sleak::CameraController.

Definition at line 53 of file FirstPersonController.cpp.

◆ Update()

void Sleak::FirstPersonController::Update ( float deltaTime)
overridevirtual

Implements Sleak::CameraController.

Definition at line 46 of file FirstPersonController.cpp.