package physics;

import stdlib.Color;
import stdlib.StdDraw;

/**
 * Implementation of a 2-d bouncing ball in the box from (-1, -1) to (1, 1).
 * @author Daniel Weschke
 */
public class BouncingBall { 
    public static void main(String[] args) {
    	StdDraw sd = new StdDraw();

        // set the scale of the coordinate system
        sd.setXscale(-1.0, 1.0);
        sd.setYscale(-1.0, 1.0);

        // initial values
        double rx = 0.480, ry = 0.860;     // position
        double vx = 0.00515, vy = 0.00523;     // velocity
        double radius = 0.05;              // radius

        // main animation loop
        while (true)  { 

            // bounce off wall according to law of elastic collision
            if (Math.abs(rx + vx) > 1.0 - radius) vx = -vx;
            if (Math.abs(ry + vy) > 1.0 - radius) vy = -vy;

            // update position
            rx = rx + vx; 
            ry = ry + vy; 

            // clear the background
            sd.setPenColor(Color.GRAY);
            sd.filledSquare(0, 0, 1.0);

            // draw ball on the screen
            sd.setPenColor(Color.BLACK); 
            StdDraw.filledCircle(rx, ry, radius); 

            // display and pause for 10 ms
            sd.show(10); 
        } 
    } 
}