1#include "FPSNavigationComponent.h"
3#include "ViewContext.h"
4#include "Input/InputManager.h"
5#include "Services/Time.h"
6#include "Components/Core/TransformComponent.h"
8#include "Foundation/Reflection/Method.h"
10#include <glm/gtx/quaternion.hpp>
18void Cogs::Core::FPSNavigationComponent::registerType()
21 Field(
Name(
"enable"), &FPSNavigationComponent::enable),
22 Field(
Name(
"enablePitch"), &FPSNavigationComponent::enablePitch),
23 Field(
Name(
"maxLinearSpeed"), &FPSNavigationComponent::maxLinearSpeed),
24 Field(
Name(
"maxAngularSpeed"), &FPSNavigationComponent::maxAngularSpeed),
25 Field(
Name(
"axisForward"), &FPSNavigationComponent::axisForward),
26 Field(
Name(
"axisRight"), &FPSNavigationComponent::axisRight),
27 Field(
Name(
"axisUp"), &FPSNavigationComponent::axisUp),
28 Field(
Name(
"axisDown"), &FPSNavigationComponent::axisDown),
29 Field(
Name(
"axisRotateUp"), &FPSNavigationComponent::axisRotateUp),
30 Field(
Name(
"axisRotateRight"), &FPSNavigationComponent::axisRotateRight),
31 Field(
Name(
"actionRun"), &FPSNavigationComponent::actionRun),
32 Field(
Name(
"actionEnableNavigation"), &FPSNavigationComponent::actionEnableNavigation),
33 Field(
Name(
"pitch"), &FPSNavigationComponent::pitch),
34 Field(
Name(
"yaw"), &FPSNavigationComponent::yaw),
37 Method(
Name(
"initialize"), &FPSNavigationComponent::initialize),
38 Method(
Name(
"update"), &FPSNavigationComponent::update),
41 DynamicComponent::registerDerivedType<FPSNavigationComponent>().setMethods(methods).setFields(fields);
46 this->context = context;
49glm::vec3 Cogs::Core::FPSNavigationComponent::getOffset()
51 auto & input = context->getDefaultView()->inputManager;
52 auto speed = glm::vec3(
53 input->getAxisValue(axisRight),
54 input->getAxisValue(axisForward),
55 input->getAxisValue(axisUp) - input->getAxisValue(axisDown)
58 speed *= maxLinearSpeed;
59 if (input->getActionState(actionRun)) speed *= 3;
60 return speed * (float)context->time->getAnimationTimeDelta();
63glm::vec2 Cogs::Core::FPSNavigationComponent::getPitchYawOffset()
65 auto & input = context->getDefaultView()->inputManager;
67 glm::vec2 pitchYaw(input->getAxisValue(axisRotateUp), -input->getAxisValue(axisRotateRight));
69 pitchYaw *= maxAngularSpeed * context->time->getAnimationTimeDelta();
74void Cogs::Core::FPSNavigationComponent::update()
78 auto & input = context->getDefaultView()->inputManager;
79 auto posOffset = getOffset();
80 auto rotOffset = getPitchYawOffset();
81 if (actionEnableNavigation !=
"") {
82 if (!input->getActionState(actionEnableNavigation))
return;
86 auto pitchOffset = rotOffset.x;
87 auto yawOffset = rotOffset.y;
93 constexpr float maxPitch = std::numbers::pi_v<float> / 2.0f;
94 if (pitch < -maxPitch) pitch = -maxPitch;
95 if (pitch > maxPitch) pitch = maxPitch;
101 auto yawRot = glm::quat(glm::vec3(0, 0, yaw));
102 auto pitchRot = glm::quat(glm::vec3(pitch, 0, 0));
105 if (enablePitch) pitchRot *= glm::quat(glm::vec3(glm::half_pi<float>(), 0, 0));
107 auto * navigationTransform = getComponent<TransformComponent>();
108 navigationTransform->transform.trs.rotation = yawRot * pitchRot;
111 posOffset = glm::rotate(yawRot, posOffset);
114 navigationTransform->transform.trs.position += posOffset;
116 navigationTransform->setChanged();
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Field definition describing a single data member of a data structure.
Simple method definition.
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
Contains reflection support.
Represents an unique name.