-
-
Notifications
You must be signed in to change notification settings - Fork 21.5k
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
Fix deceleration in CharacterBody 2D and 3D templates #92398
base: master
Are you sure you want to change the base?
Conversation
38d9427
to
78e4945
Compare
modules/gdscript/editor/script_templates/CharacterBody2D/basic_movement.gd
Outdated
Show resolved
Hide resolved
modules/mono/editor/script_templates/CharacterBody2D/basic_movement.cs
Outdated
Show resolved
Hide resolved
modules/mono/editor/script_templates/CharacterBody3D/basic_movement.cs
Outdated
Show resolved
Hide resolved
modules/gdscript/editor/script_templates/CharacterBody3D/basic_movement.gd
Outdated
Show resolved
Hide resolved
2c4c8d8
to
01c5eb8
Compare
modules/gdscript/editor/script_templates/CharacterBody2D/basic_movement.gd
Outdated
Show resolved
Hide resolved
modules/gdscript/editor/script_templates/CharacterBody2D/basic_movement.gd
Outdated
Show resolved
Hide resolved
modules/gdscript/editor/script_templates/CharacterBody2D/basic_movement.gd
Outdated
Show resolved
Hide resolved
c8d3a19
to
360d346
Compare
modules/mono/editor/script_templates/CharacterBody2D/basic_movement.cs
Outdated
Show resolved
Hide resolved
modules/mono/editor/script_templates/CharacterBody3D/basic_movement.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The GDScript code style looks good to me, but I think it's up to the physics team to decide.
@@ -22,6 +23,6 @@ func _physics_process(delta: float) -> void: | |||
if direction: | |||
velocity.x = direction * SPEED | |||
else: | |||
velocity.x = move_toward(velocity.x, 0, SPEED) | |||
velocity.x = move_toward(velocity.x, 0, DECELERATION * delta) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's replace the literal 0
by 0.0
everywhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in a separate commit as it is unrelated to this change. I also replaced the other 0
for the input vector swizzling, I hope that's ok?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to keep separate commits, you'll have to squash into one at the end anyway
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious, is there an issue with keeping the 2 independent and functional commits?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we keep things to one commit when ever possible, to not clutter things and makes managing things easier, what's the issue with having just one commit for one change?
You don't have to keep one while working, just for the final fixup so it can be merged
For more details see here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the issue with having just one commit for one change?
It's not a single change since it's cosmetics on top of a fix, and I'm touching code that's unrelated to the fix itself (for example the input vector).
Also, It makes reverting or cherry-picking only the fix harder. It is also confusing to see mixed changes along with a fix, as it makes you question whether 0
vs 0.0
was actually part of the problem
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For these purposes it's a single change, both are part of the same change, if the two need to be separate they should be two PRs instead, the changes aren't going to be reverted separately as the change to float constants is perfectly safe
There are times when a PR needs multiple commits because of the scope of the change, this one is not one of them
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your call, squashed 🤷
The bugfix this PR does is definitely welcome. I'm not completely sure about the name "deceleration" though, since it is a number and not a vector. (But maybe simplicity is more important than mathematical accuracy.) Sorry for all the nitpicking but this code will be seen by many first-time users, so we want to get it right. |
I don't mind renaming that, what do you suggest? Slowdown? MomentumForce? Changing into a vector feels like it's going to open a can of worms to me, but your call. |
I think the point is that deceleration isn't a scalar but a vector, so the name not the type is in question |
Sure, I'm happy to rename to anything, please let me know what you'd prefer. |
The velocity can never be greater than SPEED because direction is normalized. This means the move_toward calls are currently equivalent to assigning the velocity components to 0. Changing SPEED to SPEED * delta makes the friction effect way too small (the character drifts too much) so we introduce a new deceleration constant. The cosmetics regarding zeros written as floating point are unrelated to the fix itself.
Also, alternatively, we could use the incoming velocity.x = move_toward_smooth(velocity.x, direction.x * SPEED, delta * STEEPNESS)
velocity.z = move_toward_smooth(velocity.z, direction.z * SPEED, delta * STEEPNESS) This handles both the acceleration and the deceleration at the same time and actually simplifies the code by removing the branching |
That'd be something for a separate PR, would be good to get this fix in 4.3 and the other method won't be in 4.3, I'd also say it shouldn't be added to the template until it's been tested for a while after merger |
godot/modules/gdscript/editor/script_templates/CharacterBody2D/basic_movement.gd Lines 22 to 26 in fc6d8e6
Why do we even need smooth deceleration here if there is no smooth acceleration? Changing velocity.x from 0 to SPEED is instantaneous. Why not just https://docs.godotengine.org/en/latest/tutorials/physics/using_character_body_2d.html#platformer-movement
|
The deceleration is a standard syntax that's used in various places, if anything I'd say to add acceleration as well, but many games just have immediate speedup, but out of scope for this which just fixes the code to match what was originally intended |
Even given that this is intended to be a simple character controller, making available the possibility to set both acceleration and deceleration rates seems preferable. Example from kidscancode, noteworthy also for showing how to enable modification of values in the inspector ( https://kidscancode.org/godot_recipes/4.x/2d/platform_character/index.html#friction-and-acceleration |
The point of this PR is to fix the code that has been broken for years, not rewrite it. A PR to rewrite it has been stalled forever, so the idea here is to make the code correct so that it can later be improved starting from a correct foundation. There are many opinions on how the default template could be, and this could take a very long time before reaching a consensus, but I don't think it should prevent important fixes in the meantime. |
#73873 is stalled for more than a year, so I thought addressing small but concrete parts of the problem could help making progress.
Physics squad edit:
move_toward
incorrectly #74216