-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
spring.pde
68 lines (61 loc) · 2.13 KB
/
spring.pde
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
class Spring {
/* initialize variables */
/* constants */
final float spring_constant, damping, rest_position, mass;
/* simulation variables */
Node n1;
Node n2;
float position, velocity, acceleration, force;
/* constructor */
Spring(Node n1_, Node n2_) {
/* constants */
spring_constant = 2;
damping = 0.92;
rest_position = 15;
mass = 20;
/* simulation variables */
if ((n1_.y >= n2_.y) || (n1_.x >= n2_.x)) {
n1 = n2_;
n2 = n1_;
} else {
n1 = n1_;
n2 = n2_;
}
position = rest_position;
velocity = 0.0;
acceleration = 0;
force = 0;
}
/* draw spring */
void display() {
fill(0);
line(n1.x, n1.y, n2.x, n2.y);
}
/* update spring */
void update() {
/* update spring position */
force = -spring_constant * (position - rest_position); /* force = -ky */
acceleration = force / mass; /* set the acceleration f=ma == a=f/m */
velocity = damping * (velocity + acceleration); /* set the velocity */
position = (sqrt(abs((n2.x - n1.x)*2 + (n2.y - n1.y)*2))); /* set position to distance between the two nodes */
/* update position */
if (position >= 0){
position = position + velocity;
} else {
position = position - velocity;
}
/* make adjust nodes to position via position variables after adding velocity */
/* NOTE: probably a problem is that the coordinate system of processing starts
* from the top left corner and not the bottom left corner so idk if this affects this
* and my brain can't wrap around too many numbers but it seems like this could impact
* the accuracy of the results of these calculations... */
n1.x = -((pow(position, 2) - (2 * n2.x) - (2 * n2.y) + (2 * n1.y))/2);
n1.y = -((pow(position, 2) - (2 * n2.x) + (2 * n1.x) - (2 * n2.y))/2);
n2.x = ((pow(position, 2) + (2 * n1.x) - (2 * n2.y) + (2 * n1.y))/2);
n2.y = ((pow(position, 2) - (2 * n2.x) + (2 * n1.x) + (2 * n1.y))/2);
/* set velocity to 0 when its below 0.1 */
if(abs(velocity) < 0.05) {
velocity = 0.0;
}
}
}