FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
processing/java/examples/Topics/Simulate/Spring/Spring.pde at master · p-render/processing · GitHub
p-render
processing
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
processing
/
java
/
examples
/
Topics
/
Simulate
/
Spring
/
Spring.pde
Copy path
More file actions
More file actions
Latest commit
History
History
History
94 lines (79 loc) · 1.91 KB
Breadcrumbs
processing
/
java
/
examples
/
Topics
/
Simulate
/
Spring
/
Spring.pde
Copy path
File metadata and controls
94 lines (79 loc) · 1.91 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/**
* Spring.
*
* Click, drag, and release the horizontal bar to start the spring.
*/
//
Spring drawing constants for top bar
int
springHeight
=
32
;
//
Height
int
left;
//
Left position
int
right;
//
Right position
int
max
=
200
;
//
Maximum Y value
int
min
=
100
;
//
Minimum Y value
boolean
over
=
false
;
//
If mouse over
boolean
move
=
false
;
//
If mouse down and over
//
Spring simulation constants
float
M
=
0.8
;
//
Mass
float
K
=
0.2
;
//
Spring constant
float
D
=
0.92
;
//
Damping
float
R
=
150
;
//
Rest position
//
Spring simulation variables
float
ps
=
R
;
//
Position
float
vs
=
0.0
;
//
Velocity
float
as
=
0
;
//
Acceleration
float
f
=
0
;
//
Force
void
setup
() {
size
(
640
,
360
);
rectMode
(
CORNERS
);
noStroke
();
left
=
width
/
2
-
100
;
right
=
width
/
2
+
100
;
}
void
draw
() {
background
(
102
);
updateSpring();
drawSpring();
}
void
drawSpring
() {
//
Draw base
fill
(
0.2
);
float
baseWidth
=
0.5
*
ps
+
-
8
;
rect
(
width
/
2
-
baseWidth, ps
+
springHeight,
width
/
2
+
baseWidth,
height
);
//
Set color and draw top bar
if
(over
||
move) {
fill
(
255
);
}
else
{
fill
(
204
);
}
rect
(left, ps, right, ps
+
springHeight);
}
void
updateSpring
() {
//
Update the spring position
if
(
!
move) {
f
=
-
K
*
(ps
-
R
);
//
f=-ky
as
=
f
/
M
;
//
Set the acceleration, f=ma == a=f/m
vs
=
D
*
(vs
+
as);
//
Set the velocity
ps
=
ps
+
vs;
//
Updated position
}
if
(
abs
(vs)
<
0.1
) {
vs
=
0.0
;
}
//
Test if mouse is over the top bar
if
(
mouseX
>
left
&&
mouseX
<
right
&&
mouseY
>
ps
&&
mouseY
<
ps
+
springHeight) {
over
=
true
;
}
else
{
over
=
false
;
}
//
Set and constrain the position of top bar
if
(move) {
ps
=
mouseY
-
springHeight
/
2
;
ps
=
constrain
(ps, min, max);
}
}
void
mousePressed
() {
if
(over) {
move
=
true
;
}
}
void
mouseReleased
() {
move
=
false
;
}
Back
|
FazBrowse Home
|
New Git URL