-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhysics.pde
182 lines (164 loc) · 7.29 KB
/
Physics.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*--------------------------------------------------------------------------------
***physical behaviors***
---------------------------------------------------------------------------------*/
void addSprings(){
for (int i = 0; i < tempPart.size(); ++i) {
Vec3D p = tempPart.get(i);
//create Particles
VerletParticle vp = new VerletParticle(p.copy());
physics.addParticle(vp);
//physics.addBehavior(new AttractionBehavior(vp, depth, -5));
Vec3D force = field.evalFlow(vp);
//force.scaleSelf(2);
vp.addForce(force);
//////// Connect with ground level
if (vp.z<=depth) vp.z=0;
//lock particles
if ((vp.x==0 || vp.x == DIMX || vp.y==0 || vp.y==DIMY || vp.z==0 || vp.z==DIMZ) &&
field.evalTemp(vp)>=(idealTemp-thresTemp) && field.evalTemp(vp)<=(idealTemp+thresTemp)) {
vp.lock();
}
//cross refference with other particles
int spP = 0;
//int maxspP = 5;
for (int j = 0; j < physics.particles.size(); ++j) {
VerletParticle vpOther =(VerletParticle) physics.particles.get(j);
float distance =p.distanceTo(vpOther);
if (distance>0 && distance<minSpringsLength){
physics.removeParticle(vp);
}
//Create new Springs
else if (distance>=minSpringsLength && distance<=maxSpringsLength && spP<maxspP) {
//Evaluate rlFactor
//if (vpOther.z<=DIMZ*.5) rlFactor = map(vpOther.z, 0, DIMZ*.5, minRLFactor, 0.99);
//if (vpOther.z>DIMZ*.5) rlFactor = map(vpOther.z, DIMZ*.5, DIMZ, 1.01, maxRLFactor);
rlFactor = map(vpOther.z, 0, DIMZ, minRLFactor, maxRLFactor);
if (distance*rlFactor <= minSpringsLength) rlFactor=(minSpringsLength/distance);
if (distance*rlFactor >= maxSpringsLength) rlFactor=(maxSpringsLength/distance);
VerletSpring sp = new VerletSpring(vp,vpOther,distance*rlFactor,stiffness);
physics.addSpring(sp);
spP++;
//count++;
}
}
}
}//____End addSprings
void addSpringsSlice(){
for (int i = 0; i < tempPart.size(); ++i) {
Vec3D p = tempPart.get(i);
//create Particles
VerletParticle vp = new VerletParticle(p.copy());
physics.addParticle(vp);
//lock particles
if ((vp.x==0 || vp.x == DIMX || vp.y==0 || vp.y==DIMY || vp.z==0 || vp.z==DIMZ) &&
field.evalTemp(vp)>=(idealTemp-thresTemp) && field.evalTemp(vp)<=(idealTemp+thresTemp)) {
vp.lock();
}
if (vp.isLocked()==false) {
//_______________________________________________________Use constraint
for (int k = 0; k < lNum; ++k) {
Vec3D zero = lOrigins[k];
if (abs(vp.z-zero.z)<(lStep/2)) {
Vec3D force = new Vec3D (vp.x, vp.y, zero.z);
force.subSelf(vp);
//force.scaleSelf(1);
vp.addForce(force);
}
}
}
//cross refference with otherr particles
for (int j = 0; j < physics.particles.size(); ++j) {
VerletParticle vpOther =(VerletParticle) physics.particles.get(j);
//if are in similar layers
if (vpOther.z>=(vp.z-lStep)&&vpOther.z<=(vp.z+lStep)) {
float distance =p.distanceTo(vpOther);
if (distance>0 && distance<minSpringsLength){
physics.removeParticle(vp);
}
//Create new Springs
else if (distance>=minSpringsLength && distance<=maxSpringsLength) {
//Evaluate rlFactor
//if (vpOther.z<=DIMZ*.5) rlFactor = map(vpOther.z, 0, DIMZ*.5, minRLFactor, 0.99);
//if (vpOther.z>DIMZ*.5) rlFactor = map(vpOther.z, DIMZ*.5, DIMZ, 1.01, maxRLFactor);
rlFactor = map(vpOther.z, 0, DIMZ, minRLFactor, maxRLFactor);
if (distance*rlFactor <= minSpringsLength) rlFactor=(minSpringsLength/distance);
if (distance*rlFactor >= maxSpringsLength) rlFactor=(maxSpringsLength/distance);
VerletSpring sp = new VerletSpring(vp,vpOther,distance*rlFactor,stiffness);
physics.addSpring(sp);
//count++;
}
}//if are in similar layers
}
}
}//____End addSpringsSlice
void checkSprings(){
//float rlThres = 0.01;
//float rlThres=0;
//float rlThresMax = .005;
//float rlThresMin = .001;
//float stiffening = .01;
for (int i = 0; i < physics.springs.size(); ++i) {
VerletSpring sp = (VerletSpring) physics.springs.get(i);
float s = sp.getStrength();
if (s<1) {
float newS = constrain(s+stiffening, stiffness, 1);
sp.setStrength(newS);
}
/*
float getRL = sp.getRestLength();
float distance = sp.a.distanceTo(sp.b);
float RLratio= (getRL/distance);
if (sp.a.z<=DIMZ/2) rlThres = map(sp.a.z, 0, DIMZ/2, rlThresMax, rlThresMin);
if (sp.a.z>DIMZ/2) rlThres = map(sp.a.z, DIMZ/2, DIMZ, rlThresMin, rlThresMax);
//rlThres = rlThresMin;
//remove if are not useful
if (RLratio >= 1-rlThres && RLratio <= 1+rlThres && s < .95) {
physics.removeSpring(sp);
}
*/
}
}//____End checkSprings2
void checkSpringsF(){
//float rlThres = 0.01;
//float rlThres=0;
//float rlThresMax = .01;
//float rlThresMin = .001;
//float stiffening = .01;
for (int i = 0; i < physics.springs.size(); ++i) {
VerletSpring sp = (VerletSpring) physics.springs.get(i);
float s = sp.getStrength();
if (s<1) {
float newS = constrain(s+stiffening, stiffness, 1);
sp.setStrength(newS);
}
///*
float getRL = sp.getRestLength();
float distance = sp.a.distanceTo(sp.b);
float RLratio= (getRL/distance);
//if (sp.a.z<=DIMZ/2) rlThres = map(sp.a.z, 0, DIMZ/2, rlThresMax, rlThresMin);
//if (sp.a.z>DIMZ/2) rlThres = map(sp.a.z, DIMZ/2, DIMZ, rlThresMin, rlThresMax);
rlThres = map(sp.a.z, 0, DIMZ, rlThresMin, rlThresMax);
//rlThres = rlThresMin;
//remove if are not useful
if (RLratio >= 1-rlThres && RLratio <= 1+rlThres && s < .98 /*(s>=1)*/) {
physics.removeSpring(sp);
}
//*/
//if (distance>1.5*depth) physics.removeSpring(sp);
}
}//____End checkSpringsF
void slicing(){
for (int i = 0; i < physics.particles.size(); ++i) {
VerletParticle vp =(VerletParticle) physics.particles.get(i);
if (vp.isLocked()==false) {
////_______________________________________________________add Force to particle
for (int k = 1; k < lNum; ++k) {
Vec3D zero = lOrigins[k];
if (abs(vp.z-zero.z)<(lStep/2)) {
vp.z=zero.z;
vp.lock();
}
}
}
}
}//____End slicing