Cogs.Core
FPSNavigationComponent.cpp
1#include "FPSNavigationComponent.h"
2
3#include "ViewContext.h"
4#include "Input/InputManager.h"
5#include "Services/Time.h"
6#include "Components/Core/TransformComponent.h"
7
8#include "Foundation/Reflection/Method.h"
9
10#include <glm/gtx/quaternion.hpp>
11
12#include <algorithm>
13
14using namespace Cogs::Core;
15using namespace Cogs::Reflection;
16
17void Cogs::Core::FPSNavigationComponent::registerType()
18{
19 Field fields[] = {
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),
34 };
35 Method methods[] = {
36 Method(Name("initialize"), &FPSNavigationComponent::initialize),
37 Method(Name("update"), &FPSNavigationComponent::update),
38 };
39
40 DynamicComponent::registerDerivedType<FPSNavigationComponent>().setMethods(methods).setFields(fields);
41}
42
43void Cogs::Core::FPSNavigationComponent::initialize(Cogs::Core::Context * context)
44{
45 this->context = context;
46}
47
48glm::vec3 Cogs::Core::FPSNavigationComponent::getOffset()
49{
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)
55 );
56
57 speed *= maxLinearSpeed;
58 if (input->getActionState(actionRun)) speed *= 3;
59 return speed * (float)context->time->getAnimationTimeDelta();
60}
61
62glm::vec2 Cogs::Core::FPSNavigationComponent::getPitchYawOffset()
63{
64 auto & input = context->getDefaultView()->inputManager;
65
66 glm::vec2 pitchYaw(input->getAxisValue(axisRotateUp), -input->getAxisValue(axisRotateRight));
67
68 pitchYaw *= maxAngularSpeed * context->time->getAnimationTimeDelta();
69
70 return pitchYaw;
71}
72
73void Cogs::Core::FPSNavigationComponent::update()
74{
75 if (!enable) return;
76
77 auto & input = context->getDefaultView()->inputManager;
78 auto posOffset = getOffset();
79 auto rotOffset = getPitchYawOffset();
80 if (actionEnableNavigation != "") {
81 if (!input->getActionState(actionEnableNavigation)) return;
82 }
83
84
85 auto pitchOffset = rotOffset.x;
86 auto yawOffset = rotOffset.y;
87
88 pitch += pitchOffset;
89 yaw += yawOffset;
90
91 if (enablePitch) {
92 constexpr float maxPitch = glm::half_pi<float>();
93 if (pitch < -maxPitch) pitch = -maxPitch;
94 if (pitch > maxPitch) pitch = maxPitch;
95 }
96 else {
97 pitch = 0;
98 }
99
100 auto yawRot = glm::quat(glm::vec3(0, 0, yaw));
101 auto pitchRot = glm::quat(glm::vec3(pitch, 0, 0));
102
103 // When pitch control is enabled, we add a 90 degree rotation to point the camera forward instead of down
104 if (enablePitch) pitchRot *= glm::quat(glm::vec3(glm::half_pi<float>(), 0, 0));
105
106 auto * navigationTransform = getComponent<TransformComponent>();
107 navigationTransform->rotation = yawRot * pitchRot;
108
109 //find move direction:
110 posOffset = glm::rotate(yawRot, posOffset);
111
112 //apply translation:
113 navigationTransform->position += posOffset;
114
115 navigationTransform->setChanged();
116}
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Field definition describing a single data member of a data structure.
Definition: Field.h:70
Simple method definition.
Definition: Method.h:72
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
Contains reflection support.
Definition: Component.h:11
Represents an unique name.
Definition: Name.h:70