Repository navigation
Expand file tree
/
Copy pathGravitySprings.java
More file actions
165 lines (130 loc) · 3.34 KB
/
Copy pathGravitySprings.java
File metadata and controls
165 lines (130 loc) · 3.34 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import processing.core.*;
public class GravitySprings extends PApplet {
Spring[] sparray = new Spring[10];
boolean press = false;
int count = 0; // to limit number of springs
int current; // tracks pressed spring (index of array)
double gravity = 10;
public void setup()
{
size(500, 300);
noStroke();
}
public void draw() {
display();
}
public void mousePressed() {
// prevents overlapping
for (int i = 0; i < count; i++) {
if ((sparray[i].xval - 35 < mouseX) && (mouseX < sparray[i].xval + 35) && (sparray[i].yval - 20 < mouseY) && (mouseY < sparray[i].yval + 20)) {
press = true; // if you are touching a wood block
current = i;
sparray[current].bounce = false;
println("current is " + current);
/*println("k is " + sparray[current].k);
println("displacement is " + sparray[current].displacement);
println("yval is " + sparray[current].yval);*/
}
}
if (!press) {
if (count < 8) {
sparray[count] = new Spring(2, mouseX, mouseY);
sparray[count].drawSpring();
count++;
}
}
}
public void mouseReleased() {
if (press) {
sparray[current].bounce = true;
}
press = false;
}
public void mouseDragged() {
if (press) {
sparray[current].yval = mouseY;
// you can't "push" the spring
if (sparray[current].yval < sparray[current].rest) {
sparray[current].yval = sparray[current].rest;
sparray[current].spheight = sparray[current].rest;
}
// spring can't go too low
if (sparray[current].yval > height - 25) {
sparray[current].yval = height - 25;
sparray[current].spheight = height - 25;
}
sparray[current].spheight = sparray[current].yval;
//sparray[current].spwidth -= 0.1;
if (sparray[current].spwidth < 2) {
sparray[current].spwidth = 2;
}
}
}
public void display() {
background(255);
for (int i = 0; i < count; i++) {
sparray[i].drawSpring();
sparray[i].bounceSpring();
}
}
class Spring {
// constants, for now
double damping = 0.92;
// attributes
double mass; // default 0.8
int xval, yval;
// variables
double vx = 0.0; // Velocity
double a = 0; // Acceleration
double f = 0; // Force
boolean bounce = false;
private int rest;
double displacement;
double mg;
double k;
// spring size
float spwidth = 10;
int spheight;
Spring(double mass_, int xval_, int yval_) {
mass = mass_;
xval = xval_;
yval = yval_;
spheight = yval;
rest = yval;
displacement = yval - rest;
mg = mass * gravity;
k = mg / yval; // f == f => mg = ky
}
void drawSpring() {
// draw block
fill(205, 170, 125);
rectMode(CENTER);
rect(xval, yval, 50, 20);
// draw spring
fill(102);
rectMode(CENTER);
rect(xval, 0, spwidth, 2 * spheight);
// text
//text(Double.toString(mass), xval, yval);
text(Double.toString(k), xval, yval);
}
void bounceSpring() {
if (bounce) {
displacement = yval - rest;
f = -k * displacement;
a = f / mass;
vx = damping * (vx + a);
yval = (int) (yval + vx);
spheight = yval;
}
if (abs((float) vx) < 0.01) {
vx = 0.0;
bounce = false;
}
}
}
// to run as a java "application" - full screen
public static void main(String args[]) {
PApplet.main(new String[] { "--present", "GravitySprings" });
}
}