A dynamical system I modeled in Java (two balls attached by a string bouncing around a room).

read more

Closed system bouncing

Three dimensional feel

Energy decay

Notes for the dynamical spring system:

Well, we know that the tension is proportional to the length of extension.

We have to calculate a spring constant, then we can find the overall tension as a function of direct distance between the two masses, then we can break that up into components.

Spring constant will be of the form: x Netwons / Meter

Mass.java

			
public class Mass {
	
	public double startingxJerk; 
	public double startingyJerk;
	public double xJerk;
	public double yJerk;
	public double startingxAcceleration;
	public double startingyAcceleration;
	public double startingyVelocity;
	public double startingxVelocity;
	public double xAcceleration;
	public double yAcceleration;
	public double xVelocity;
	public double yVelocity;
	public double xPosition;
	public double yPosition;
	public double currentTime;
	public boolean underInfluence;
	public double mass;
	public Spring spring;
	public int yincrementer;
	public int xincrementer;
	public boolean collision;
	public double energyLoss = .1;
	

	public Mass(double startingxJerk, double startingyJerk, double startingxAcceleration, double startingyAcceleration,double startingxVelocity, double startingyVelocity,double mass,
			double xPosition, double yPosition, double currentTime,boolean underInfluence) {
		
		this.startingxAcceleration = startingxAcceleration;
		this.startingyAcceleration = startingyAcceleration;
		this.startingyVelocity = startingyVelocity;
		this.startingxVelocity = startingxVelocity;
		this.xPosition = xPosition;
		this.yPosition = yPosition;
		this.currentTime = currentTime;
		this.underInfluence = underInfluence;
		this.mass = mass;
		this.yVelocity = startingyVelocity;
		this.xVelocity = startingxVelocity;
	}	
	
public void update(){
			
	if(this.underInfluence){
		this.xAcceleration=(((spring.Xtension)+this.startingxAcceleration))/this.mass;
		//this.yAcceleration=(((spring.Ytension)+this.startingyAcceleration))/this.mass;
	}
	if(!this.underInfluence){
		this.xAcceleration=((-spring.Xtension)+this.startingxAcceleration)/this.mass;
		//this.yAcceleration=((-spring.Ytension)+this.startingyAcceleration)/this.mass;
	}
	
	//if(!this.underInfluence&&this.xVelocity>=10){
		//this.xAcceleration=0;
		//this.xVelocity=1;
		
	//}
	
	
	yincrementer--;
	xincrementer--;
	
	
	
	
	this.xVelocity += this.xAcceleration;
	this.yVelocity += this.yAcceleration;
	
	
	this.xPosition=this.xPosition+(this.xVelocity);
	this.yPosition=this.yPosition+(this.yVelocity);
	
		
	
}
	
	
}

massOne.java

			

public class massOne {


	public double xacceleration = .000001; 
	public double xvelocity = 0; 
	public double xposition = 90; 
	public double yposition = 60; 
	public double currentTime = 0;


	public void update(){

		this.xvelocity += this.xacceleration; 

		this.xposition=this.xposition+(this.xvelocity);


	}



}

massTwo.java

			

public class massTwo {

	public int xacceleration = 0; 
	public double xvelocity = 0; 
	public double xposition = 90; 
	public double yposition = 200; 
	public double currentTime = 0;
	
	massOne massOne = new massOne();
	//spring spring = new spring();
	
	public void update(){
		
		
		
		this.xvelocity += this.xacceleration; 
		
		this.xposition=this.xposition+(this.xvelocity);
		
		
	}
	
}

Spring.java

			
public class Spring {
	private Mass massOne;
	private Mass massTwo;
	public double startingX = 0; 
	public double startingY = 0; 
	public double endingX = 0; 
	public double endingY = 0;
	public double tension = 0; 
	public double Xtension = 0; 
	public double Ytension = 0; 
	public double currentTime = 0;
	public double gravity = 0;
	public double springConstant = 3; 
	public double scaler = .001;
	public double angle = 0;

	public Spring(Mass massOne, Mass massTwo) {
		this.massOne = massOne;
		this.massTwo = massTwo;
		massOne.spring = this;
		massTwo.spring = this;
		//this.Ytension = massTwo.mass*gravity;
	}

public void update(){
			
		
		this.startingX = massOne.xPosition; 
		this.startingY = massOne.yPosition; 
		this.endingX = massTwo.xPosition; 
		this.endingY = massTwo.yPosition;
		//this.tension = scaler*springConstant*Math.hypot((massOne.xPosition-massTwo.xPosition), (massOne.yPosition-massTwo.yPosition)); //in newtons/pixel
		this.Xtension = scaler*springConstant*(massOne.xPosition-massTwo.xPosition);
		this.Ytension = scaler*springConstant*(massOne.yPosition-massTwo.yPosition);
		//System.out.println(this.Ytension);
		
		
		
	
}

}

SpringController.java

			
public class SpringController {

	Mass ex = new Mass(0,0,0,0,.1,0, 2, 50, 50, 0,false);
	Mass exSquared = new Mass(0,0,.2,0,0,0, 2, 100, 200, 0,true);
	Mass exCubed = new Mass(6,0,0,0,3,6, 10, 90, 200, 0,true); 
	//Mass exQuarted = new Mass(0,0,3,6, 10, 90, 200, 0,true);
	Spring springexToexSquared = new Spring(ex, exSquared);
	

	//double startingxJerk, double startingyJerk, double startingxAcceleration, double startingyAcceleration,double startingxVelocity, double startingyVelocity,double mass,double xPosition, double yPosition, double currentTime,boolean underInfluence

}

system.java

			
import java.awt.Graphics;
import java.awt.Rectangle;


public class system extends java.applet.Applet implements Runnable {
	    int frame;
	    int delay;
	    int increment=1;
	    int numberOfPoints=0;
	    int xycoords[][]= new int [2][100000];
	    double endpoints[][] = new double[2][100000];
	    int xycoords2[][]= new int [2][100000];
	    int angles[]= new int [10000];
	    Thread animator;
	    

	    /** m
	     * Initialize the applet and compute the delay between frames.
	     */
	    public void init() {
		String str = getParameter("fps");
		int fps = (str != null) ? Integer.parseInt(str) : 50;
		delay = (fps > 0) ? (1000 / fps) : 100;
	    }

	    /**
	     * This method is called when the applet becomes visible on
	     * the screen. Create a thread and start it.
	     */
	    public void start() {
		animator = new Thread(this);
		animator.start();
	    }

	    /**
	     * This method is called by the thread that was created in
	     * the start method. It does the main animation.
	     */
	    public void run() {
	    	 
		while (Thread.currentThread() == animator) {
		    // Display the next frame of animation.
			increment++;
		    repaint();
		    
		    // Delay for a while
		    try {
			Thread.sleep(delay);
		    } catch (InterruptedException e) {
			break;
		    }

		    // Advance the frame
		    frame++;
		}
	    }

	    /**
	     * This method is called when the applet is no longer
	     * visible. Set the animator variable to null so that the
	     * thread will exit before displaying the next frame.
	     */
	    public void stop() {
		animator = null;
	    }
	    

	    /**
	     * Paint a frame of animation.
	     */
	    SpringController objects = new SpringController();
	    Rectangle massOneBounds = new Rectangle((int)objects.ex.xPosition,(int)objects.ex.yPosition,20,20);
	    Rectangle massTwoBounds = new Rectangle((int)objects.exSquared.xPosition,(int)objects.exSquared.yPosition,20,20);
	    
	  boolean collision=false;
	  boolean drawn=false;
	  int constantincrementor=0;
		public void paint(Graphics g) {
			// try {
			// Thread.sleep(500);
			// } catch (InterruptedException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			// }
			//hey.setA(500);
			//hey.setR(10);
			//hey.setV(2);

			if (!drawn) {
				//hey.collision1 = true;
				drawn = true;
				this.setSize(1920, 1200);
				

			}
			
			
			//g.drawRect(hey.abound, hey.bbound, hey.cbound, hey.dbound);//I just realized that my points are the center of the circle
			
			massOneBounds.x=(int)objects.ex.xPosition;
			massOneBounds.y=(int)objects.ex.yPosition;
			massTwoBounds.x=(int)objects.exSquared.xPosition;
			massTwoBounds.y=(int)objects.exSquared.yPosition;
			
			//massOne.currentTime=this.increment;
			
			
			
			//if(Math.abs(objects.massTwo.xPosition-objects.massOne.xPosition)<=10&&Math.abs(objects.massTwo.yPosition-objects.massOne.yPosition)<=10){
				
			//}
			
			objects.springexToexSquared.update();
			objects.ex.update();
			objects.exSquared.update();
			
			
			
			
			
			xycoords[0][numberOfPoints]=(int) objects.ex.xPosition;
			xycoords[1][numberOfPoints]=(int) objects.ex.yPosition;
			xycoords2[0][numberOfPoints]=(int) objects.exSquared.xPosition;
			xycoords2[1][numberOfPoints]=(int) objects.exSquared.yPosition;
			
			numberOfPoints++;
			
			if(numberOfPoints>1){
			for(int i=0; i<numberOfPoints-1;i++){
				
			g.drawLine(xycoords[0][i], xycoords[1][i], xycoords[0][i+1], xycoords[1][i+1]);
			g.drawLine(xycoords2[0][i], xycoords2[1][i], xycoords2[0][i+1], xycoords2[1][i+1]);
			
			
			}
			
			}
			
			
			
			//collision = hey.xymove(increment);
			
			if (collision) {
				increment = 1;
				collision = false;
				//angles[constantincrementor]=hey.theta;
				constantincrementor++;
			}

			g.fillOval((int) objects.ex.xPosition, (int) objects.ex.yPosition, 20, 20);
			g.fillOval((int) objects.exSquared.xPosition, (int) objects.exSquared.yPosition, 20,20);
			g.drawLine((int)objects.springexToexSquared.startingX +10, (int)objects.springexToexSquared.startingY +10, (int)objects.springexToexSquared.endingX +10, (int)objects.springexToexSquared.endingY+10);
			//g.drawLine((int)objects.massOne.xPosition +10, (int)objects.massOne.yPosition +10, (int)objects.massOne.xPosition+10+ (10)*(int)objects.massOne.xVelocity, (int)objects.massOne.yPosition +10+ (10)*(int)objects.massOne.yVelocity);
		}		

			
	}