Skip to main content
2 of 2
deleted 254 characters in body
whitwhoa
  • 201
  • 1
  • 14

What's causing this stutter when framerate fluctuates?

I'm working on a class responsible for arm sway in an fps engine, and am having an issue with a stutter that appears to be related to fluctuating framerate. However I have also noticed it when locking the framerate, but to a much lesser almost unnoticeable degree (so maybe NOT related to framerate variance). As far as I can tell I have everything setup the way it should be to account for framerate fluctuations, but apparently I've missed something?? Curious if I've made a glaring oversight that might jump out at someone. I have uploaded a video where you can see exactly what it is I'm talking about.

Any ideas? update() is where the magic happens.

#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)),
    maxSwayDistance(0.1f),
    swaySpeed(0.0f),
    mouseBufferSize(44),
    firstMouseInput(true),
    currentMousePosition(glm::vec2(0.0f, 0.0f)),
    lastMousePosition(glm::vec2(0.0f, 0.0f)),
    mouseDelta(glm::vec2(0.0f, 0.0f)),
{};

glm::vec2 ArmSwayController::getAverageMouse()
{
    glm::vec2 out;
    
    for (auto& x : this->mouseXBuffer)
    {
        out.x += x;
    }
    out.x = out.x / this->mouseXBuffer.size();

    for (auto& y : this->mouseYBuffer)
    {
        out.y += y;
    }
    out.y = out.y / this->mouseYBuffer.size();

    return out;
}

void ArmSwayController::bufferMouse(glm::vec2 in)
{
    if (this->mouseXBuffer.size() == this->mouseBufferSize)
        this->mouseXBuffer.pop_front();
    if (this->mouseYBuffer.size() == this->mouseBufferSize)
        this->mouseYBuffer.pop_front();

    this->mouseXBuffer.push_back(in.x);
    this->mouseYBuffer.push_back(in.y);
}

void ArmSwayController::bufferMouseXSpeed(float in)
{
    if (this->mouseXSpeedBuffer.size() == this->mouseBufferSize)
        this->mouseXSpeedBuffer.pop_front();

    this->mouseXSpeedBuffer.push_back(in);
}

void ArmSwayController::bufferMouseYSpeed(float in)
{
    if (this->mouseYSpeedBuffer.size() == this->mouseBufferSize)
        this->mouseYSpeedBuffer.pop_front();

    this->mouseYSpeedBuffer.push_back(in);
}

void ArmSwayController::updateMouseDelta()
{
    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::update(float dt) // called once per tick
{
    this->updateMouseDelta();

    this->bufferMouse(this->mouseDelta);
    this->swaySpeed = 0.00000625f / dt;


    glm::vec2 vecToAdd = vel::helpers::functions::invertVec2(this->getAverageMouse());

    // X
    this->bufferMouseXSpeed(vecToAdd.x);

    float pullBackAmountX = this->mouseXSpeedBuffer.size() > 1 ? this->mouseXSpeedBuffer[this->mouseXSpeedBuffer.size() - 2] : this->mouseXSpeedBuffer[0];
    pullBackAmountX = pullBackAmountX < 0 ? fabs(pullBackAmountX) : (-1.0f * pullBackAmountX);

    this->swayXYPos.x += ((vecToAdd.x + pullBackAmountX) * this->swaySpeed);


    // Y
    this->bufferMouseYSpeed(vecToAdd.y);

    float pullBackAmountY = this->mouseYSpeedBuffer.size() > 1 ? this->mouseYSpeedBuffer[this->mouseYSpeedBuffer.size() - 2] : this->mouseYSpeedBuffer[0];
    pullBackAmountY = pullBackAmountY < 0 ? fabs(pullBackAmountY) : (-1.0f * pullBackAmountY);

    this->swayXYPos.y += ((vecToAdd.y + pullBackAmountY) * this->swaySpeed);


    return this->swayXYPos;
}
whitwhoa
  • 201
  • 1
  • 14