public class Block extends PhysicsElement { private static int id=0; // Block identification private final float mass; private float width; private double pos_t; private double pos_tPlusDelta; private double speed_t; private double speed_tPlusDelta; private Spring leftSpring, rightSpring; private Block(){ // nobody can create a block without state super(id++); mass=0; } public Block(float mass, float width, float position, float speed){ super(id++); this.mass = mass; this.width = width; pos_t = position; this.speed_t = speed; leftSpring = rightSpring = null; } public Block(float mass, float width, float position, float speed, Spring lSpring, Spring rSpring){ this(mass, width, position, speed); leftSpring = lSpring; rightSpring = rSpring; } public void attachLeftSpring (Spring lSpring) { leftSpring = lSpring; } public void attachRightSpring (Spring rSpring) { rightSpring = rSpring; } public double getPosition() { return pos_t; } public void computeNextState(double delta_t) { double force=0.0; if (rightSpring != null) force = rightSpring.getForce(this); if (leftSpring != null) force += leftSpring.getForce(this); double a = force/mass; pos_tPlusDelta = pos_t + speed_t*delta_t + a*delta_t*delta_t/2; speed_tPlusDelta = speed_t + a*delta_t; } public void updateState(){ pos_t = pos_tPlusDelta; speed_t = speed_tPlusDelta; } public String getDescription() { return "Block_" + getId()+": x"; } public String getState() { return getPosition()+""; } }