
// OneSimpleAttractor.java
// Jeff Traer-Bernstein
// Robert Sedgewick and Kevin Wayne
// Slightly reformatted by Andrew Davison, Nov. 2022

/* A particle is attracted to the current location of the mouse.
  Incorporates drag.

   Usage:
     > compile OneSimpleAttractor.java
     > run OneSimpleAttractor
*/


public class OneSimpleAttractor
{
  public static void main(String[] args)
  {
    double rx = 0.0, ry = 0.0; // position
    double vx = 0.0, vy = 0.0; // velocity
    double mass = 1.0;  
    double dt   = 0.5;   // time quantum
    double drag = 0.1;   // mess around with this a bit
    double attractionStrength = 0.01;

    StdDraw.enableDoubleBuffering();
    while (true) {
      // compute the attractive force to the mouse, accounting for drag
      double dx = StdDraw.mouseX() - rx;
      double dy = StdDraw.mouseY() - ry;
      double fx = (dx * attractionStrength) - (drag * vx);
      double fy = (dy * attractionStrength) - (drag * vy);

      // Euler step: update velocity, then position
      vx += fx * dt / mass;
      vy += fy * dt / mass;
      rx += vx * dt;
      ry += vy * dt;

      // draw particle
      StdDraw.clear();
      StdDraw.setPenColor(StdDraw.BLUE);
      StdDraw.filledCircle(rx, ry, 0.02);
      StdDraw.show();
      StdDraw.pause(10);
    }
  } /* main */

} // end of OneSimpleAttractor class
