Skip to content

Commit

Permalink
feat(input): add deadzone to axis
Browse files Browse the repository at this point in the history
  • Loading branch information
kuukitenshi committed Sep 27, 2024
1 parent 0244800 commit d69f034
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Handle body rotation on physics integration (#1242, **&fallenatlas**).
- Binary Serializer and Deserializer (#1306, **@RiscadoA**).
- Type Client and Type Server (#1302, **@RiscadoA**).
- Deadzone for input axis (#844, **@kuukitenshi**)

### Fixed

Expand Down
12 changes: 11 additions & 1 deletion engine/include/cubos/engine/input/axis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ namespace cubos::engine
/// @param negative Negative input combinations.
/// @param gamepadAxes Gamepad axis bindings.
InputAxis(std::vector<InputCombination> positive, std::vector<InputCombination> negative,
std::vector<core::io::GamepadAxis> gamepadAxes)
std::vector<core::io::GamepadAxis> gamepadAxes, float deadzone = 0.0F)
: mPositive(std::move(positive))
, mNegative(std::move(negative))
, mGamepadAxes(std::move(gamepadAxes))
, mDeadzone(deadzone)
{
}

Expand Down Expand Up @@ -74,11 +75,20 @@ namespace cubos::engine
/// @param value New value.
void value(float value);

/// @brief Gets the deadzone.
/// @return Deadzone.
float deadzone() const;

/// @brief Sets the deadzone.
/// @param deadzone New deadzone.
void deadzone(float deadzone);

private:
std::vector<InputCombination> mPositive;
std::vector<InputCombination> mNegative;
std::vector<core::io::GamepadAxis> mGamepadAxes;

float mValue{0.0F}; ///< Not serialized.
float mDeadzone{0.0F};
};
} // namespace cubos::engine
22 changes: 19 additions & 3 deletions engine/src/input/axis.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <algorithm>
#include <cmath>

#include <cubos/core/io/keyboard.hpp>
Expand All @@ -20,7 +21,8 @@ CUBOS_REFLECT_IMPL(cubos::engine::InputAxis)
.with(FieldsTrait{}
.withField("positive", &InputAxis::mPositive)
.withField("negative", &InputAxis::mNegative)
.withField("gamepadAxes", &InputAxis::mGamepadAxes));
.withField("gamepadAxes", &InputAxis::mGamepadAxes)
.withField("deadzone", &InputAxis::mDeadzone));
}

const std::vector<InputCombination>& InputAxis::positive() const
Expand Down Expand Up @@ -60,11 +62,25 @@ float InputAxis::value() const

void InputAxis::value(float value)
{
mValue = value;
mValue = std::abs(value) < mDeadzone ? 0.0F : value;

if (std::abs(mValue) > 1.0F)
{
CUBOS_WARN("Axis value out of range: {}", mValue);
mValue = mValue > 1.0F ? 1.0F : -1.0F;
mValue = std::clamp(mValue, -1.0F, 1.0F);
}
}

float InputAxis::deadzone() const
{
return mDeadzone;
}

void InputAxis::deadzone(float deadzone)
{
if (deadzone < 0.0F || deadzone > 1.0F)
{
CUBOS_WARN("Invalid deadzone value: {}. Value must be between 0.0 and 1.0.", deadzone);
}
mDeadzone = std::clamp(deadzone, 0.0F, 1.0F);
}

0 comments on commit d69f034

Please sign in to comment.