package net.willware.Orbits;

import java.awt.Graphics;

/**
 * Created by IntelliJ IDEA.
 * User: wware
 * Date: Oct 16, 2004
 * Time: 9:10:08 AM
 */
public class Tether implements SimObject {
	// http://en.wikipedia.org/wiki/Young's_modulus
	public static double youngsModulus = 2.0e11;  // steel
	protected double length;
	private double maxlength = 0.0;
	private double maxtension = 0.0;
	private PointMass end1, end2;
	private double releaseTime = 1.0e40;
	public Tether(double length) {
		this.length = length;
		end1 = end2 = null;
	}
	public Tether(double length, double releaseTime) {
		this.length = length;
		end1 = end2 = null;
		this.releaseTime = releaseTime;
	}
	public void timeStep1() { }
	public void timeStep2() { }
	public void write() { }
	public double getLength() {
		/**
		 * The default behavior is that length is constant. To get
		 * a time-varying length, overload this method.
		 */
		return length;
	}
	public double getMaxLength() {
		return maxlength;
	}
	public double getMaxTension() {
		return maxtension;
	}
	public void setEnd1(PointMass p) { end1 = p; }
	public void setEnd2(PointMass p) { end2 = p; }
	public PointMass getEnd1() { return end1; }
	public PointMass getEnd2() { return end2; }
	public void interact(SimObject otherguy) {
		if (System.time < releaseTime &&
		        otherguy instanceof PointMass) {
			PointMass oneEnd = (PointMass) otherguy;
			PointMass otherEnd;
			if (oneEnd == end1 && end2 != null) {
				otherEnd = end2;
			} else if (oneEnd == end2 && end1 != null) {
				otherEnd = end1;
			} else {
				return;
			}
			Vector d = otherEnd.getPosition().subtract(oneEnd.getPosition());
			double r = d.length();
			if (r > maxlength)
				maxlength = r;
			// use the method in case we overload
			double len = getLength();
			if (r > len) {
				double f = youngsModulus * (r - len) / len;
				if (f > maxtension)
					maxtension = f;
				oneEnd.addForce(d.scale(f / r));
			}
		}
	}
	public void growBoundingBox(Grapher gg) { }
	public void draw(Graphics g, Grapher gg) { }
}
