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>
17void Cogs::Core::FPSNavigationComponent::registerType()
20 Field(
Name(
"enable"), &FPSNavigationComponent::enable),
21 Field(
Name(
"enablePitch"), &FPSNavigationComponent::enablePitch),
22 Field(
Name(
"maxLinearSpeed"), &FPSNavigationComponent::maxLinearSpeed),
23 Field(
Name(
"maxAngularSpeed"), &FPSNavigationComponent::maxAngularSpeed),
24 Field(
Name(
"axisForward"), &FPSNavigationComponent::axisForward),
25 Field(
Name(
"axisRight"), &FPSNavigationComponent::axisRight),
26 Field(
Name(
"axisUp"), &FPSNavigationComponent::axisUp),
27 Field(
Name(
"axisDown"), &FPSNavigationComponent::axisDown),
28 Field(
Name(
"axisRotateUp"), &FPSNavigationComponent::axisRotateUp),
29 Field(
Name(
"axisRotateRight"), &FPSNavigationComponent::axisRotateRight),
30 Field(
Name(
"actionRun"), &FPSNavigationComponent::actionRun),
31 Field(
Name(
"actionEnableNavigation"), &FPSNavigationComponent::actionEnableNavigation),
32 Field(
Name(
"pitch"), &FPSNavigationComponent::pitch),
33 Field(
Name(
"yaw"), &FPSNavigationComponent::yaw),
36 Method(
Name(
"initialize"), &FPSNavigationComponent::initialize),
37 Method(
Name(
"update"), &FPSNavigationComponent::update),
40 DynamicComponent::registerDerivedType<FPSNavigationComponent>().setMethods(methods).setFields(fields);
45 this->context = context;
48glm::vec3 Cogs::Core::FPSNavigationComponent::getOffset()
50 auto & input = context->getDefaultView()->inputManager;
51 auto speed = glm::vec3(
52 input->getAxisValue(axisRight),
53 input->getAxisValue(axisForward),
54 input->getAxisValue(axisUp) - input->getAxisValue(axisDown)
57 speed *= maxLinearSpeed;
58 if (input->getActionState(actionRun)) speed *= 3;
59 return speed * (float)context->time->getAnimationTimeDelta();
62glm::vec2 Cogs::Core::FPSNavigationComponent::getPitchYawOffset()
64 auto & input = context->getDefaultView()->inputManager;
66 glm::vec2 pitchYaw(input->getAxisValue(axisRotateUp), -input->getAxisValue(axisRotateRight));
68 pitchYaw *= maxAngularSpeed * context->time->getAnimationTimeDelta();
73void Cogs::Core::FPSNavigationComponent::update()
77 auto & input = context->getDefaultView()->inputManager;
78 auto posOffset = getOffset();
79 auto rotOffset = getPitchYawOffset();
80 if (actionEnableNavigation !=
"") {
81 if (!input->getActionState(actionEnableNavigation))
return;
85 auto pitchOffset = rotOffset.x;
86 auto yawOffset = rotOffset.y;
92 constexpr float maxPitch = glm::half_pi<float>();
93 if (pitch < -maxPitch) pitch = -maxPitch;
94 if (pitch > maxPitch) pitch = maxPitch;
100 auto yawRot = glm::quat(glm::vec3(0, 0, yaw));
101 auto pitchRot = glm::quat(glm::vec3(pitch, 0, 0));
104 if (enablePitch) pitchRot *= glm::quat(glm::vec3(glm::half_pi<float>(), 0, 0));
106 auto * navigationTransform = getComponent<TransformComponent>();
107 navigationTransform->rotation = yawRot * pitchRot;
110 posOffset = glm::rotate(yawRot, posOffset);
113 navigationTransform->position += posOffset;
115 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.