Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
VrMovementComponent.cpp
Go to the documentation of this file.
4#include <glm/gtc/matrix_transform.hpp>
5#include <plog/Log.h>
6
7namespace EngineCore {
8
10 : LogicComponent(owningScene)
11 , headset_(headset)
12 , inputHandler_(inputHandler)
13{
14 setCanTick(true);
15}
16
18{
20 PLOGD << "VrMovementComponent initialized";
21}
22
23void VrMovementComponent::tick(double deltaTime)
24{
25 if (!headset_ || !inputHandler_) {
26 return;
27 }
28
29 glm::vec3 currentPos = headset_->getPlayerPosition();
30 glm::vec3 movement{0.0f};
31
32 // Get right thumbstick for XZ movement
33 glm::vec2 thumbstick = inputHandler_->getRightThumbstick();
34
35 // Apply deadzone
36 if (glm::length(thumbstick) < deadzone_) {
37 thumbstick = glm::vec2{0.0f};
38 }
39
40 // Get forward direction on XZ plane from headset orientation
41 glm::vec3 forwardXZ = getHeadsetForwardXZ();
42 glm::vec3 rightXZ = glm::normalize(glm::cross(forwardXZ, glm::vec3{0.0f, 1.0f, 0.0f}));
43
44 // thumbstick.y = forward/back, thumbstick.x = strafe left/right
45 movement += forwardXZ * thumbstick.y * moveSpeed_ * static_cast<float>(deltaTime);
46 movement += rightXZ * thumbstick.x * moveSpeed_ * static_cast<float>(deltaTime);
47
48 // Vertical movement from triggers (up) and grips (down)
49 float triggerLeft = inputHandler_->getTriggerValue(Input::Hand::LEFT);
50 float triggerRight = inputHandler_->getTriggerValue(Input::Hand::RIGHT);
51 float gripLeft = inputHandler_->getGripValue(Input::Hand::LEFT);
52 float gripRight = inputHandler_->getGripValue(Input::Hand::RIGHT);
53
54 float upInput = glm::max(triggerLeft, triggerRight);
55 float downInput = glm::max(gripLeft, gripRight);
56
57 movement.y += (upInput - downInput) * verticalSpeed_ * static_cast<float>(deltaTime);
58
59 // Apply movement
60 currentPos += movement;
61 headset_->setPlayerPosition(currentPos);
62}
63
68
70{
71 // Get the view matrix of the left eye (eye 0) - the inverse gives us the camera world transform
72 // We use the raw eye view matrix without player offset to get headset orientation
73 glm::mat4 viewMatrix = headset_->getEyeViewMatrix(0);
74
75 // The view matrix is inverse(camera_world_transform)
76 // To get forward direction: in view space, forward is -Z
77 // In world space, forward = inverse(viewMatrix) * (0,0,-1,0)
78 // But we can extract it directly from the view matrix:
79 // Forward direction in world space is the negative of the third column of the transpose of the upper 3x3
80 // Or equivalently: -viewMatrix[2] (the third row, which is -forward in view space)
81
82 glm::vec3 forward = -glm::vec3(viewMatrix[0][2], viewMatrix[1][2], viewMatrix[2][2]);
83
84 // Project onto XZ plane and normalize
85 forward.y = 0.0f;
86 float length = glm::length(forward);
87 if (length > 0.001f) {
88 forward /= length;
89 } else {
90 // Looking straight up or down, default to -Z
91 forward = glm::vec3{0.0f, 0.0f, -1.0f};
92 }
93
94 return forward;
95}
96
97}
virtual void endPlay()
Called when the component is removed or the game ends.
void setCanTick(bool enable)
Enables or disables ticking for this component.
virtual void beginPlay()
Called when the component is added to the scene or the game starts.
LogicComponent(Scene *owningScene)
A scene is the overarching structure which can spawn actors.
Definition Scene.h:17
Input::XrInputHandler * inputHandler_
void beginPlay() override
Called when the component is added to the scene or the game starts.
void tick(double deltaTime) override
Called every frame if ticking is enabled.
void endPlay() override
Called when the component is removed or the game ends.
VrMovementComponent(Scene *owningScene, Headset *headset, Input::XrInputHandler *inputHandler)
Constructs the VR movement component.
Log category system implementation.