/* muscle.java 
 * Created on Apr 25, 2006 
 */ 

/* muscle.java 
 * Created on Apr 24, 2006 
 */ 

import processing.core.PApplet;
import processing.core.PConstants;

public class muscle {
	int A, B;
	particleSystem parent;
	float restLength;
	float theta;
	float phaseSpeed;
	public PApplet p;
	public double gdt;
	public float s;
	
	public muscle(PApplet p_, particleSystem parent_, int A_, int B_) {
		p = p_;
		parent = parent_;
		A = A_;
		B = B_;
		theta = (float) 0.0f; // theta
		
		phaseSpeed = 0.2f;
		s = 0.0f;
		setRestLength();
	}

	private void setRestLength() {
		float dx = parent.particles[B].loc.x - parent.particles[A].loc.x;
		float dy = parent.particles[B].loc.y - parent.particles[A].loc.y;
		restLength = PApplet.sqrt(dx*dx + dy*dy);
	}

//	
//	 float G = 0.4;                              // Arbitrary universal gravitational constant
//     theta_acc = (-1 * G / r) * sin(theta);      // Calculate acceleration (see: http://www.myphysicslab.com/pendulum1.html)
//     theta_vel += theta_acc;                     // Increment velocity
//     theta_vel *= damping;                       // Arbitrary damping
//     theta += theta_vel;                         // Increment theta
	
	public void work() {
		Vector3D v = new Vector3D();
		// s += 0.001f; // this is funny.
		gdt = 2;
		 v.x = parent.particles[B].loc.x - parent.particles[A].loc.x;
		 v.y = parent.particles[B].loc.y - parent.particles[A].loc.y;
		v.normalize();
		
		parent.particles[B].loc.x = parent.particles[A].loc.x + (restLength + 7*p.cos(theta))*v.x;
		parent.particles[B].loc.y = parent.particles[A].loc.y + (restLength + 7*p.sin(theta))*v.y;
		theta += phaseSpeed * gdt  / 2;
		
		if(theta > PConstants.TWO_PI) theta -= PConstants.TWO_PI;
	}
	
	public void mdraw() {
		float x1,y1,x2,y2;
		x1 = parent.particles[A].loc.x;
		y1 = parent.particles[A].loc.y;
		x2 = parent.particles[B].loc.x;
		y2 = parent.particles[B].loc.y;
		p.strokeWeight(1);
		p.stroke(255,20,60,100);
		p.line(x1,y1,x2,y2);
		p.noStroke();
		
	}

	
}
 
