Created
December 4, 2020 23:12
-
-
Save whitwhoa/02195a28c842f3e594b6df3ab409e116 to your computer and use it in GitHub Desktop.
arm sway controller class, responsible for arm sway on mouse movement and pullback to center
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "glm/gtx/string_cast.hpp" | |
#include "vel/App.h" | |
#include "vel/helpers/functions.h" | |
#include "ArmSwayController.h" | |
ArmSwayController::ArmSwayController() : | |
input(vel::App::get().getInputState()), | |
swayXYPos(glm::vec2(0.0f, 0.0f)), | |
swaySpeed(0.0f), | |
firstMouseInput(true), | |
currentMousePosition(glm::vec2(0.0f, 0.0f)), | |
lastMousePosition(glm::vec2(0.0f, 0.0f)), | |
mouseDelta(glm::vec2(0.0f, 0.0f)), | |
prevVecToAdd(glm::vec2(0.0f, 0.0f)), | |
m_coeff(10.0f), | |
m_mouse_abs(glm::vec2(0.0f, 0.0f)), | |
m_out(glm::vec2(0.0f, 0.0f)), | |
m_prev_out(glm::vec2(0.0f, 0.0f)) | |
{}; | |
void ArmSwayController::updateMouseDelta(float dt) | |
{ | |
this->currentMousePosition.x = this->input.mouseXPos; | |
this->currentMousePosition.y = this->input.mouseYPos; | |
if (this->firstMouseInput) | |
{ | |
this->lastMousePosition = this->currentMousePosition; | |
this->firstMouseInput = false; | |
} | |
this->mouseDelta.x = (this->currentMousePosition.x - this->lastMousePosition.x); | |
this->mouseDelta.y = (this->lastMousePosition.y - this->currentMousePosition.y); | |
this->lastMousePosition = this->currentMousePosition; | |
} | |
glm::vec2 ArmSwayController::getSmoothedMouseDelta(float dt) | |
{ | |
this->updateMouseDelta(dt); | |
this->m_mouse_abs += this->mouseDelta; | |
this->m_out -= std::expm1(-this->m_coeff * dt) * (this->m_mouse_abs - this->m_out); | |
auto smd = this->m_out - this->m_prev_out; | |
this->m_prev_out = this->m_out; | |
return smd; | |
} | |
glm::vec2 ArmSwayController::update(float dt) | |
{ | |
this->swaySpeed = 0.004f; | |
glm::vec2 vecToAdd = vel::helpers::functions::invertVec2(this->getSmoothedMouseDelta(dt)); | |
auto pullBackAmount = vel::helpers::functions::invertVec2(this->prevVecToAdd); | |
this->swayXYPos += (vecToAdd + pullBackAmount) * this->swaySpeed; | |
//this->swayXYPos += vecToAdd * this->swaySpeed; // commenting out the above and uncommenting this verifies smooth movement without pullback | |
this->prevVecToAdd = vecToAdd; | |
return this->swayXYPos; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment