/*

	An attractor represents an object with gravitational attraction
	for the other objects around it. The attractor is stationary,
	drawing other objects toward it. We'll use the formulas
	for the gravitational attraction between two bodies to
	determine how this Attractor effects other objects.
*/

public class Attractor {

	// mass of our attractor
	float mass;
	
	// The Gravitational constant
	float G;
	
	// location of our attractor
	Vector2D location;
	
	// Variables for moving our attractor around the screen
	boolean isDragging = false;
	Vector2D dragOffset;
	
	public Attractor(Vector2D loc_, float mass_, float g_) {
	
		// save our construction properties
		location = loc_;
		mass = mass_;
		G = g_;
		
		// give our other properties default values
		dragOffset = new Vector2D(0.0, 0.0);
	}
	
	public void simulate() {
	
		display();
		drag();
		
	}
	
	public Vector2D getGravityForce(SimObject so) {
	
		// get the direction of the gravitational force
		Vector2D forceDirection = Vector2D.subtract(location, so.getLocation());
		
		// get the distance between the objects
		float distance = forceDirection.magnitude();
		
		// calculate the magnitude of the gravitational force
		forceDirection.normalize();
		float force = (G * mass * so.getMass()) / (distance * distance);
		forceDirection = forceDirection.multiply(force);
		
		return forceDirection;
		
	}
	
	public void display() {
	
		// display our attractor
		rectMode(CENTER);
		noStroke();
		
		if (isDragging) {
			fill(128, 128, 128);
		} else {
			fill(16, 128, 16);
		}
		
		ellipse(location.getX(), location.getY(), mass * 2, mass * 2);
		
	}

	void clicked(int x, int y) {
	
		float distance = dist(x, y, location.getX(), location.getY());
		if (distance < mass) {
			isDragging = true;
			dragOffset.setX(location.getX() - x);
			dragOffset.setY(location.getY() - y);
		}
	}

	void stopDragging() {
		isDragging = false;
	}
	
	void drag() {
		if (isDragging) {
			location.setX(mouseX + dragOffset.getX());
			location.setY(mouseY + dragOffset.getY());
		}
	}

}
