Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clamp client-sent movement speed control #15721

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

appgurueu
Copy link
Contributor

Results in the movement_x and movement_y fields of player:get_player_control() being safe to use (otherwise users would need to compute the length as (x^2 + y^2)^0.5 and clamp that to 1 themselves).

Prompted by #14348 (comment).

Results in the `movement_x` and `movement_y` fields of `player:get_player_control()` being safe to use
(otherwise users would need to compute the length as `(x^2 + y^2)^0.5` and clamp that to 1 themselves).
@appgurueu appgurueu added Trivial The change is a trivial bug fix, documentation or maintenance change, as per the Git Guidelines Bugfix 🐛 PRs that fix a bug labels Jan 27, 2025
*pkt >> player->control.movement_speed;
f32 movement_speed;
*pkt >> movement_speed;
player->control.movement_speed = std::clamp(movement_speed, 0.0f, 1.0f);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh? shouldn't the direction be clamped while speed can be any value > 0?

Copy link
Member

@grorp grorp Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the direction (radians) should wrap around, so any value should be valid.

at least the virtual joystick of the touch controls can be dragged past a speed of 1 (used by virtual_joystick_triggers_aux1), however this is already clamped on the client iirc, so adding server-side validation makes sense.

consideration: might it be a better idea to send the unclamped speed value to the server and document that? since you can always clamp yourself, but you can't undo the clamping.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the client sends NaN, -inf or inf?

Copy link
Contributor Author

@appgurueu appgurueu Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Infinities are fine with clamping. NaN is UB indeed, good call. I had expected something saner (I would have been fine with it being clamped to either boundary).

We should probably also forbid NaN or +-Inf directions.

Copy link
Member

@SmallJoker SmallJoker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bugfix 🐛 PRs that fix a bug One approval ✅ ◻️ Trivial The change is a trivial bug fix, documentation or maintenance change, as per the Git Guidelines
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants