Skip to content

Commit e5bbabe

Browse files
authored
fix(joystick): remove discontinuity at deadzone (#230)
* Fix the circular joystick implementation which had a discontinuity when transitioning through the deadzone radius.
1 parent 92d1347 commit e5bbabe

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

components/joystick/include/joystick.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,15 @@ class Joystick : public BaseComponent {
156156
position_.x(0);
157157
position_.y(0);
158158
} else if (magnitude > 1.0f) {
159-
// otherwise, we need to clamp the vector to be within the unit circle
159+
// if it's outside the unit circle, then normalize the vector so that
160+
// it's on the unit circle
160161
position_ = position_.normalized();
162+
} else {
163+
// otherwise we should scale the vector so that it's 0 on the edge of
164+
// the deadzone and 1 on the edge of the unit circle
165+
const float magnitude_range = 1.0f - center_deadzone_radius_;
166+
const float scale = (magnitude - center_deadzone_radius_) / magnitude_range;
167+
position_ *= scale;
161168
}
162169
}
163170
}

0 commit comments

Comments
 (0)