User Tools

Site Tools


gamedev:smoothvalue

Smooth Values

Smooth values provide support to smoothly slide a value through unpredictable input values over time. This is used for locomotion handling where the player produces unpredictable input values. If the change is directly applied motion is erratic and unnatural. Smoothing the input values produces a more enjoyable result. Since though the start and end time of the smoothed value is not known only an appropximation can be done. A lot of different approaches exist but they all lack an enjoyable, stable and widely usable result.

The perfect solution approaches the GLSL smoothstep function. This function has a gently acceleration and deacceleration phase and is in general a very enjoyable result. The smooth values provided by the Drag[en]gine uses a velocity controlled algorithm that has been tuned to mimic the GLSL smoothstep function inside the limits of unpredictable input values. The graphic below shows a comparison of the GLSL smoothstep function in red and the Drag[en]gine Smooth Value function in green. For the important parts the functions are quite similar and as such the result is reasonably close to the perfect solution.

GLSL SmoothStep versus Drag[en]gine SmoothValue comparison

GLSL SmoothStep versus Drag[en]gine SmoothValue comparison.

For this to work a few parameters have to be set to allow proper smoothing.

Smoothing Parameters

Smooth values store the current value and the goal value. The goal value is adjusted by the game scripts to reflect the input of the player or another source of unpredictable input.

The smoothing is controlled by the adjust time and adjust range parameters. The adjust time controls the speed of sliding the current value towards the goal value. Rougly spoken adjust time determines the time after which the current value has approached close enough to the goal value.

The adjust range parameter is a tuning parameter to deal with situations where the change speed required to adjust the current value becomes large, Such large values can result in strange results or plain out break the smoothing in bad cases. The adjust range parameter is used to clamp the change speed to a maximum fitting the smooth value configuration. As rule of thumb this value is best set to the “maximumValue - minimumValue” hence the parameter name.

The smoothing is carried out using the update method using the elapsed time since the last frame update or a user chosen time step for special effects like bullet time.

Implementations

The Drag[en]gine provides a couple of smooth value implementations for easy use:

  • Smooth Float: Smooth a single floating point value.
  • Smooth Double: Smooth a single double precision floating point value.
  • Smooth Vector: Smooth a 3-component floating point vector. The vector length between new and old values is used as value change speed. This is useful for 3D position smoothing.
  • Smooth DVector: Smooth a 3-component double precision floating point vector. Same as Smooth Vector but with higher precision.

Usage example DragonScript

// during initialization time
var SmoothFloat smoothLookUpDown = SmoothFloat.new()   // create smooth value
smoothLookUpDown.setAdjustTime( 1.0 )                  // adjust look up-down angle across 1 second
smoothLookUpDown.setAdjustRange( 180.0 )               // maximim angle (90 degree) - minimum angle (-90 degree)

// during game frame update
smoothLookUpDown.setGoal( newPlayerUpDown )            // set value changed by player input
smoothLookUpDown.update( Engine.getElapsedTime() )     // smooth value using elapsed time since last frame update
useSmoothedValue = smoothLookUpDown.getValue()         // use smoothed value which is the current value
You could leave a comment if you were logged in.
gamedev/smoothvalue.txt · Last modified: 2024/03/14 16:45 by dragonlord