MCPcopy Create free account
hub / github.com/Illation/ETEngine / Process

Method Process

Projects/Demo/source/Runtime/FreeCamera.cpp:52–123  ·  view source on GitHub ↗

------------------------------ FreeCameraSystem::Process Update a cameras position according to input

Source from the content-addressed store, hash-verified

50// Update a cameras position according to input
51//
52void FreeCameraSystem::Process(fw::ComponentRange<FreeCameraSystemView>& range)
53{
54 // common variables
55 core::InputManager* const input = core::InputManager::GetInstance();
56 float const dt = core::ContextManager::GetInstance()->GetActiveContext()->time->DeltaTime();
57
58 for (FreeCameraSystemView& view : range)
59 {
60 // movement direction
61 bool hasMoveInput = false;
62 {
63 vec3 move;
64
65 move.x -= (input->GetKeyState(E_KbdKey::A) == E_KeyState::Down) ? 1.0f : 0.0f;
66 move.x += (input->GetKeyState(E_KbdKey::D) == E_KeyState::Down) ? 1.0f : 0.0f;
67
68 move.y -= (input->GetKeyState(E_KbdKey::Q) == E_KeyState::Down) ? 1.0f : 0.0f;
69 move.y += (input->GetKeyState(E_KbdKey::E) == E_KeyState::Down) ? 1.0f : 0.0f;
70
71 move.z += (input->GetKeyState(E_KbdKey::W) == E_KeyState::Down) ? 1.0f : 0.0f;
72 move.z -= (input->GetKeyState(E_KbdKey::S) == E_KeyState::Down) ? 1.0f : 0.0f;
73
74 if (!math::isZero(move))
75 {
76 move = math::normalize(move);
77 hasMoveInput = true;
78 }
79
80 //Acceleration
81 {
82 vec3 const delta = move - view.camera->movement;
83 float const acc = s_Accelleration * dt;
84 view.camera->movement = view.camera->movement + (delta * math::Clamp01(acc));
85 }
86 }
87
88 //handle scrolling to change camera speed
89 if (hasMoveInput || input->GetMouseButton(E_MouseButton::Left) == E_KeyState::Down)
90 {
91 float const scroll = input->GetMouseWheelDelta().y;
92 if (scroll > 0.0f)
93 {
94 view.camera->speedMultiplier *= 1.1f;
95 }
96 else if (scroll < -0.01f)
97 {
98 view.camera->speedMultiplier *= 0.9f;
99 }
100 }
101
102 float const currSpeed = s_MoveSpeed * view.camera->speedMultiplier;
103
104 // Calculate the view inverse in the same way the camera would do it
105 vec3 const lookAt = view.transform->GetPosition() + view.transform->GetForward();
106 mat3 const camMat = math::CreateFromMat4(math::inverse(math::lookAt(view.transform->GetPosition(), lookAt, view.transform->GetUp())));
107 vec3 currPos = view.transform->GetPosition() + camMat * view.camera->movement * currSpeed * dt;
108 view.transform->SetPosition(currPos);
109

Callers

nothing calls this directly

Calls 8

Clamp01Function · 0.85
ClampFunction · 0.85
DeltaTimeMethod · 0.80
GetActiveContextMethod · 0.80
GetKeyStateMethod · 0.80
GetMouseButtonMethod · 0.80
SetPositionMethod · 0.80
SetRotationMethod · 0.80

Tested by

no test coverage detected