Class Damped

java.lang.Object
cit244.util.Damped

public final class Damped extends Object
A value that moves smoothly toward a target over time, using frame-rate-independent exponential damping.

Create one for each moving value, keep it in a field, and call update(double, double) once per frame.


 Damped cameraX = new Damped(5, 0);

 // each frame:
 double x = cameraX.update(playerX, dt);
 

The target can come from anywhere, including a Mapping: cameraX.update(mouseToX.apply(mouseX), dt).

See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Damped(double rate, double initial)
    Creates a damped value.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    reset(double value)
    Jumps straight to a value, with no smoothing.
    double
    update(double target, double dt)
    Moves the value toward a target.
    double
    Returns the current value without moving it.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Damped

      public Damped(double rate, double initial)
      Creates a damped value.
      Parameters:
      rate - convergence rate in inverse seconds (nonnegative); larger is faster, 0 never moves
      initial - starting value
      Throws:
      IllegalArgumentException - if rate is negative or non-finite, or initial is non-finite
  • Method Details

    • update

      public double update(double target, double dt)
      Moves the value toward a target.

      A NaN or infinite target is rejected before anything changes. Accepting one would leave the value stuck at NaN for good, so the exception points at the frame where the bad target first appeared.

      Parameters:
      target - value to approach (finite)
      dt - elapsed time in seconds (nonnegative)
      Returns:
      the updated value
      Throws:
      IllegalArgumentException - if target is non-finite, or dt is negative or non-finite
    • value

      public double value()
      Returns the current value without moving it.
      Returns:
      current value
    • reset

      public void reset(double value)
      Jumps straight to a value, with no smoothing.
      Parameters:
      value - new current value
      Throws:
      IllegalArgumentException - if value is non-finite