-2.5 C
New York
Friday, December 5, 2025

physics: decelerate the participant’s momentum with out altering course


Carrying move_toward individually on every axis will scale back xyz in direction of 0 on the identical velocity till considered one of them reaches the vacation spot, leaving the remaining velocity pointing parallel to the remaining axis.

For example we begin with velocity.x = 4 and velocity.z = 6and DEACC = 1. Here is what occurs after every deceleration tick, taking a look at our remaining velocity, the change in velocity in comparison with the earlier step, and the heading angle our velocity makes relative to the z-axis:

handed velocity.x velocity.z velocity change bearing
0 4 6 7.2 33.7°
1 3 5 5.8 -1.4 31.0°
2 2 4 4.5 -1.4 26.6°
3 1 3 3.2 -1.3 18.4°
4 0 2 2.0 -1.2 0.0°
5 0 1 1.0 -1.0 0.0°
6 0 0 0.0 -1.0

You’ll be able to see how the course of the remaining velocity adjustments at every tick till just one axis nonetheless has a non-zero worth. That is the curve you are taking a look at.

The “change” column additionally reveals that our price of deceleration will not be fixed: we decelerate extra sharply when touring diagonally and launch the brakes somewhat when the velocity aligns with a coordinate axis. It’s because decelerating DEACC A delta-V with magnitude is utilized individually to every of the 2 axes:

$$|Delta v | = sqrt{textual content{DEACC}^2 + textual content{DEACC}^2} = sqrt{2}cdottext{DEACC}$$.

That is 41.4% greater than the delta-V we get when just one axis adjustments.

If you wish to take the habits you get when the speed factors instantly alongside the xo axis and make it constant in any respect angles, then you could apply deceleration to each parts collectively, slightly than individually:

# Isolate horizontal parts (so we do not sluggish a fall)
var vel2d := Vector2(velocity.x, velocity.z)

# Step the entire vector DEACC models towards (0, 0)
vel2d = vel2d.move_toward(Vector2.ZERO, DEACC)

# Unpack again into the unique 3D velocity (preserving y)
velocity.x = vel2d.x
velocity.z = vel2d.y

That is equal to making use of the scalar. move_toward technique for vector size:

# Magnitude of horizontal parts = floor velocity
var oldSpeed := sqrt(velocity.x*velocity.x + velocity.z*velocity.z)

# If not shifting, we're executed (avoids division by zero)
if oldSpeed > 0:
    # Decelerate the velocity
    var newSpeed := move_toward(oldSpeed, 0, DEACC)

    # Apply the identical scale issue to each x and z
    var scale := newSpeed / oldSpeed
    velocity.x *= scale
    velocity.z *= scale

As a result of x and z change by the identical quantity at every tick, we preserve the course velocity factors inward, eliminating the curve you had been observing. These options additionally be sure that deceleration is fixed in all instructions, slightly than decelerating extra sharply when shifting diagonally.


The above options protect the linear deceleration utilized in your query. This works as if the braking power got here from a rope hooked up to a weight on a pulley, making use of a continuing backward pull till we cease and launch the rope (to keep away from additional accelerating). backward in direction of the pulley). Which means there’s a fool proper on the finish after we cease and let go of the rope: our acceleration adjustments from the fixed DEACC per tick to nothing in a discontinuous leap.

As an alternative, many actual programs will exhibit nonlinear deceleration, the place the deceleration power is proportional to the present velocity (as a result of a quicker shifting object creates extra friction with the bottom/air, shedding extra vitality in the identical period of time). This will likely appear extra pure to you, as a result of it avoids the sharp tug on the finish.

A standard means to do that is with a easy “exponential output facility”:

# Tune inertia to your liking; 0 = prompt cease, 1 = no velocity loss
var inertia := 0.9
velocity.x *= inertia
velocity.z *= inertia

The draw back to that is that mathematically it by no means reaches zero: it simply will get nearer and nearer. To keep away from a slight annoying drift, you’ll be able to restrict the velocity to zero as soon as the bottom velocity is “sufficiently small”.

The simple out strategy is acceptable for one thing like a disc sliding on a floor or a rolling automobile, however arguably the linear strategy could also be higher for a personality with strolling/working legs, or for making the controls really feel tight and predictable – attempt every one and see what feels greatest in your recreation.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles