-
Notifications
You must be signed in to change notification settings - Fork 0
/
Verlet.pde
160 lines (131 loc) · 3.41 KB
/
Verlet.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
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
Point point1, point2, point3;
ArrayList<Stick> sticks = new ArrayList<Stick>();
ArrayList<Point> points = new ArrayList<Point>();
Stick stick1, stick2, stick3, stick4;
float bounce, gravity, friction;
void setup(){
size(800,800);
background(255);
color red = color(255,0,0);
color blue = color(0,255,0);
color green = color(0,0,255);
points.add(new Point(red, 0, 0));
points.add(new Point(red, 100, 0));
points.add(new Point(red, 100, 100));
points.add(new Point(red, 0, 100));
sticks.add(new Stick(points.get(0), points.get(1)));
sticks.add(new Stick(points.get(1), points.get(2)));
sticks.add(new Stick(points.get(2), points.get(3)));
sticks.add(new Stick(points.get(0), points.get(3)));
stick1 = new Stick(points.get(0), points.get(1));
stick2 = new Stick(points.get(1), points.get(2));
stick3 = new Stick(points.get(2), points.get(3));
stick4 = new Stick(points.get(0), points.get(3));
bounce = 0.9;
gravity = 0.9;
friction = .99;
}
void draw(){
background(255);
for (int i = 0; i < points.size(); i++){
points.get(i).display();
}
stick1.display();
stick2.display();
stick3.display();
stick4.display();
/*for (int i = 0; i < sticks.size(); i++){
sticks.get(i).display();
}*/
update();
}
void update(){
updatePoints();
//updateSticks();
}
class Point {
float x, y, oldx, oldy;
color c;
float bounce, gravity, friction;
Point (color c, float x, float y){
this.c = c;
this.x = x;
this.y = y;
this.oldx = x-5;
this.oldy = y-5;
}
void display(){
stroke(0);
fill(c);
ellipse(this.x, this.y, 10, 10);
}
}
void updatePoints(){
float vx, vy;
for (int i = 0; i < points.size(); i++){
Point p = points.get(i);
vx = (p.x - p.oldx) * friction;
vy = (p.y - p.oldy) * friction;
p.oldx = p.x;
p.oldy = p.y;
p.x += vx;
p.y += vy;
p.y += gravity;
if (p.x > 800) {
p.x = 800;
p.oldx = p.x + vx * bounce;
}
else if (p.x < 0){
p.x = 0;
p.oldx = p.x + vx * bounce;
}
if (p.y > 800){
p.y = 800;
p.oldy = p.y + vy * bounce;
}
else if (p.y < 0){
p.y = 0;
p.oldy = p .y + vy * bounce;
}
}
}
class Stick {
Point point1, point2;
double length;
Stick (Point point1, Point point2){
this.point1 = point1;
this.point2 = point2;
this.length = distance(point1, point2);
}
double distance (Point p0, Point p1){
double dx = p1.x - p0.x;
double dy = p1.y - p0.y;
return Math.sqrt(dx * dx + dy * dy);
}
void display(){
fill(0,0,0);
line(point1.x, point1.y, point2.x, point2.y);
}
}
void updateSticks(){
for(int i = 0; i < sticks.size(); i++){
Stick stick = sticks.get(i);
float dx = stick.point2.x - stick.point1.x;
float dy = stick.point2.y - stick.point1.y;
double distance = Math.sqrt(dx * dx + dy * dy);
double difference = stick.length;
double percent = difference / distance / 2;
double adjustX = dx * percent;
double adjustY = dy * percent;
stick.point1.x -= adjustX;
stick.point1.y -= adjustY;
stick.point2.x += adjustX;
stick.point2.y += adjustY;
}
}
/*
CHANGES:
-TURNED "BALLS" INTO "POINTS"
-TOOK OUT WIDTH AND HEIGHT PARAMETERS OF POINTS, CHANGED TO 10 X 10
-TOOK OUT UPDATEPOINTS() FUNCTION FROM POINT CLASS
*/