-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpring.java
More file actions
40 lines (36 loc) · 995 Bytes
/
Spring.java
File metadata and controls
40 lines (36 loc) · 995 Bytes
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
public class Spring {
double k=40;
double damping=1;
public Ball a;
public Ball b;
private double restlen;
public Spring(Ball a,Ball b,double restlen){
this.a=a;
this.b=b;
this.restlen =restlen;
}
private double damping(){
Vector v1=Vector.sub(b.pos,a.pos);
v1.normalize();
Vector v2=Vector.sub(b.vel,a.vel);
return Vector.dotProduct(v1,v2)*damping;
}
public void applyForce(){
Vector force=Vector.sub(b.pos,a.pos);
double delta= force.mag() - restlen;
double Fs=delta*k;
double Fd=damping();
double Ft=Fs+Fd;
Vector b_a=Vector.sub(b.pos,a.pos);
Vector a_b=Vector.sub(a.pos,b.pos);
b_a.normalize();
a_b.normalize();
b_a.mul(Ft);
a_b.mul(Ft);
a.applyForce(b_a);
b.applyForce(a_b);
}
public double getRestlen() {
return restlen;
}
}