Skip to main content

Function: smoothDampVector2()

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

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

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 is not mutated; use the returned velocityOutput as the velocity for the next call.

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. velocityOutput reflects the new rate of change and should be passed back in as velocity on the next call.