/*
	Project: Balls2D
	Author: Simon Gayton
	Copyright 2007 (C) Simon Gayton
*/

import java.awt.*;

/*
  This class represents a 2D ball that is able to
  perform its own Euler/Cromer numerical integration.
*/
public class Ball2D {
  Vector2D pos;
  Vector2D vel;
  Vector2D force;
  Vector2D thrust;
  float rad;
  
  /*
    Default constructor.
  */
  Ball2D() {
    pos = new Vector2D();
    vel = new Vector2D();
    force = new Vector2D();
    thrust = new Vector2D();
  }

  /*
    Calculate the global forces that are applying to this ball.
  */
  void calculateForces() {
    // Apply the global gravity.
    force.add(Balls2D.gravity);
    
    // Apply a simple drag, negatively proportional to velocity.
    force.addScaled(-0.25f, vel);
    
    // Apply the current thrust, this includes penalty forces.
    force.add(thrust);
    thrust.zero();
  }

  /*
    Perform Euler/Cromer numerical integration.
    V = V + deltaTime * acceleration
    X = X + deltaTime * V
  */
  void integrate(float deltaTime) {
    // Update the velocity, since mass is 1.0,
    // force and acceleration are the same.
    vel.addScaled(deltaTime, force);

    // Update the position based on the new velocity.
    pos.addScaled(deltaTime, vel);

    // Set the force accumulator back to the zero vector.
    force.zero();
  }
  
  /*
    Draw the ball.
  */
  void draw(Graphics g) {
    // Draw the white fill.
    g.setColor(Balls2D.white);
    g.fillOval((int)(pos.x-rad),
      (int)(pos.y-rad),
      (int)(2.0f*rad),
      (int)(2.0f*rad));
    
    // Draw the black outline.
    g.setColor(Balls2D.black);
    g.drawOval((int)(pos.x-rad),
      (int)(pos.y-rad),
      (int)(2.0f*rad),
      (int)(2.0f*rad));
  }
}
