Skip to main content

Function: smoothDampVector2()

smoothDampVector2(position, targetPosition, velocity, maxSpeed, smoothTime, deltaTimeInSeconds): object

Defined in: math/smooth-damp-vector2.ts:26

Gradually changes a 2D position towards a target position using a smoothing function, simulating a critically damped spring. This function is useful for smooth interpolation of positions (e.g., camera movement, UI elements) where you want to avoid overshooting and oscillation.

Parameters

position

Vector2

The current position as a Vector2.

targetPosition

Vector2

The target position to move towards as a Vector2.

velocity

Vector2

The current velocity as a Vector2. This value is modified by the function.

maxSpeed

number

The maximum speed at which the position can change.

smoothTime

number

The approximate time it takes to reach the target. Smaller values result in faster movement.

deltaTimeInSeconds

number

The time elapsed since the last call, in seconds.

Returns

object

An object containing:

  • positionOutput: The new, smoothed position as a Vector2.
  • velocityOutput: The updated velocity as a Vector2.

positionOutput

positionOutput: Vector2

velocityOutput

velocityOutput: Vector2

Remarks

This function prevents overshooting and clamps the maximum change per update to maxSpeed * smoothTime. The velocity parameter is updated to reflect the current rate of change and should be reused between calls.