-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spring.java
149 lines (134 loc) · 3.94 KB
/
Spring.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import java.awt.*;
public class Spring extends PhysicsElement implements Simulateable, Elastic {
private static int id=0; // Spring identification
private final double restLength;
private final double stiffness;
private SpringAttachable a_end, b_end;
private double aLoosePosition, bLoosePosition;
private double currentLength = 0;
private double delta_x = 0;
private SpringView view;
private Spring() { // nobody can create a block without state
this(0,0);
}
public Spring(double restLength, double stiffness) {
super(id++);
this.restLength = restLength;
this.stiffness = stiffness;
a_end = b_end = null;
aLoosePosition=0;
bLoosePosition = restLength;
currentLength = restLength;
view = new SpringView(this);
}
public boolean isAattachedTo(SpringAttachable sa){
return (sa == this.a_end);
}
public boolean isBattachedTo(SpringAttachable sa){
return (sa == this.b_end);
}
public void attachAend(SpringAttachable sa) { // note: we attach a spring to a ball,
if(a_end == null){ // not the other way around.
//a_end.detachSpring(this);
a_end = sa;
sa.attachSpring(this);
}
}
public void attachBend(SpringAttachable sa) { // note: we attach a spring to a ball,
if (b_end == null){ // not the other way around.
//b_end.detachSpring(this);
b_end = sa;
sa.attachSpring(this);
}
}
public void detachAend() {
if (a_end == null)
return;
a_end.detachSpring(this);
aLoosePosition = a_end.getPosition();
a_end=null;
}
public void detachBend() {
if (b_end == null)
return;
b_end.detachSpring(this);
bLoosePosition = b_end.getPosition();
b_end=null;
}
public double getAendPosition() {
if (a_end != null)
return a_end.getPosition();
else
return aLoosePosition;
}
public double getBendPosition() {
if (b_end != null)
return b_end.getPosition();
else
return bLoosePosition;
}
public double getRestLength() {
return restLength;
}
public double getForce(SpringAttachable ball) {
double force = 0;
if ((a_end == null) || (b_end == null))
return force;
if ((ball != a_end) && (ball != b_end))
return force;
double a_pos = getAendPosition();
double b_pos = getBendPosition();
double stretch = Math.abs(b_pos-a_pos)-restLength;
force = stretch*stiffness;
if ((ball==a_end)^(a_pos<b_pos)) return -force;
return force;
}
public void updateView (Graphics2D g){
view.updateView(g);
}
public void setSelected(){
view.setSelected();
}
public void setReleased(){
view.setReleased();
}
public boolean contains(double x, double y){
return view.contains(x,y);
}
public String getDescription() {
return "Spring_"+ getId()+":a_end\tb_end";
}
public String getState() {
String s = getAendPosition() + "\t" + getBendPosition();
return s;
}
public void computeNextState(double delta_t, MyWorld w) {
//currentLength = Math.abs(b_end.getPosition() - a_end.getPosition());
if ((b_end == null) || (a_end == null))
delta_x = 0;
else {
currentLength = b_end.getPosition() - a_end.getPosition();
delta_x = currentLength - restLength;
}
}
public void updateState() {
}
public void dragTo(double x) {
if ((a_end == null) && (b_end == null)) {
aLoosePosition = x - currentLength/2;
bLoosePosition = currentLength/2 + x;
} else if ((a_end != null) && (b_end == null)) {
currentLength = Math.abs(a_end.getPosition() - bLoosePosition);
((PhysicsElement)a_end).dragTo(x - currentLength/2);
bLoosePosition = currentLength/2 + x;
} else if ((a_end == null) && (b_end != null)) {
currentLength = Math.abs(aLoosePosition - b_end.getPosition());
aLoosePosition = x - currentLength/2;
((PhysicsElement)b_end).dragTo(currentLength/2 + x);
} else if ((a_end != null) && (b_end != null)) {
currentLength = Math.abs(a_end.getPosition() - b_end.getPosition());
((PhysicsElement)a_end).dragTo(x - currentLength/2);
((PhysicsElement)b_end).dragTo(currentLength/2 + x);
}
}
}