-
Notifications
You must be signed in to change notification settings - Fork 0
/
Attractors.pde
215 lines (161 loc) · 6.74 KB
/
Attractors.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//import processing.opengl.*;
import peasy.*;
PeasyCam cam;
PFont font;
int sizeX, sizeY ;
int gridSizeX, gridSizeY;
Attractor gF[] = new Attractor[3];
Planet pl[] = new Planet[10000];
Grid grid;
boolean showAccelerationVectors = false;
Attractor sun;
boolean showAttractorLabel = true;
boolean RESET = false;
void setup(){
if(! RESET){
// size(800, 600, OPENGL);
size(800, 800, P3D);
sizeX = width; sizeY = height;
gridSizeX = int((sizeX + sizeY)/2) ;
gridSizeY = int((sizeX + sizeY)/2);
//------------------------------------------------------------------------------------------
// initialize camera ---> needed library: import peasy.*;
cam = new PeasyCam(this, sizeX/2 , sizeY/2, 0, 1200); // default settings on double click
cam.setMinimumDistance(.001);
cam.setMaximumDistance(50000);
cam.setRotations( -0.7301672, -0.274728, 0.30156484);
} // end if(! RESET)
int border = 0;
//------------------------------------------------------------------------------------------
// initialize Attractors
for(int i = 0; i < gF.length; i++){
int id = i;
float x = (400 * i);
float y = (400 * i);
float radius = random(20, 40);
color col = color( random(200,255), random(200,255), random(1));
Attractor nextAttractor = new Attractor(id, new PVector(x,y), radius, col);
nextAttractor.setDensity(100);
//nextAttractor.setMinimumDistance(100);
gF[i] = nextAttractor;
} // end for i
this.sun = new Attractor(99, new PVector((width/2), 0), 1, color(200,200,1));
this.sun.setDensity(10);
//------------------------------------------------------------------------------------------
// initialize Planets
for(int i = 0; i < pl.length; i++){
int id = i;
PVector initialLocation = new PVector(random(border, gridSizeX-border), random(border, gridSizeY-border), random(0, 600));
PVector initialVelocity = new PVector(random(0,3), random(0,1));
float radius = random(5,10);
color col = color( random(110, 255), random(110, 255), random(110, 220));
Planet newPlanet = new Planet(id, initialLocation, initialVelocity, radius, col);
newPlanet.setMaxSpeed(50);
newPlanet.assignAttractor(gF[0]);
pl[i] = newPlanet;
} // end for i
//------------------------------------------------------------------------------------------
// font
//font = loadFont("ArialMT-48.vlw");
//textFont(font);
frameRate(60);
} // end void setup
void draw(){
//println(frameRate);
background(0); noLights();
moveAndScaleSpheres(); // if mousePressed and key 'm' is pressed
// calculate the planets new Information
for(int i = 0; i < pl.length; i++){
//for(int j = 0; j < gF.length; j++)
//{
// PVector accel = gF[j].getForce(pl[i].currentLocation);
// pl[i].addAcceleration(accel);
//};
// Use my assigned Attractor and apply the force
PVector accel = pl[i].assignedAttractor.getForce(pl[i].currentLocation);
pl[i].addAcceleration(accel);
// Add a constant acceleration to help control the system
PVector normalAcceleration = this.sun.getForce(pl[i].currentLocation);
pl[i].addAcceleration(normalAcceleration);
pl[i].calculatePosition();
if(frameCount % 500 == 0)
{
pl[i].assignAttractor(gF[2]);
}
} // end for i
for(int i = 0; i < gF.length; i++) gF[i].drawSphere(20, 30); // draw Attractors
for(int i = 0; i < pl.length; i++) pl[i].drawPSphere();
if (showAttractorLabel) for(int i = 0; i < gF.length; i++) gF[i].drawLabel(); // draw AttractorsLabel
} // end void draw
void keyReleased(){
if(key == 't'){
if(showAttractorLabel) showAttractorLabel = false; else showAttractorLabel = true;
}
if(key == 'r') {
RESET = true;
setup();
}
} // end void keyReleased()
boolean mouseStillPressed = false;
int indexGF = 0; // index of Attractor
int indexGP = 0; // index of gridpoint
void moveAndScaleSpheres(){
for(int i = 0; i < gF.length; i++) gF[i].setSelectionState(false);
if (keyPressed ){
cam.setMouseControlled(false);
if(key == 'm' || key == 'M'){
float scrX, scrY, mouseDisShortest;
if(!mouseStillPressed){
// find nearest Attractor
scrX = screenX( gF[indexGF].location.x, gF[indexGF].location.y, 0 );
scrY = screenY( gF[indexGF].location.x, gF[indexGF].location.y, 0 );
mouseDisShortest = sqrt( sq(mouseX - scrX) + sq(mouseY - scrY) );
for(int i = 0; i < gF.length; i++){
scrX = screenX( gF[i].location.x, gF[i].location.y, 0 );
scrY = screenY( gF[i].location.x, gF[i].location.y, 0 );
float mouseDis = sqrt( sq(mouseX - scrX) + sq(mouseY - scrY) );
if( mouseDis <= mouseDisShortest){
indexGF = i; mouseDisShortest = mouseDis;
}
} // end for i
//scrX = screenX( gF[indexGF].x, gF[indexGF].y, 0 );
//scrY = screenY( gF[indexGF].x, gF[indexGF].y, 0 );
//cam.beginHUD(); stroke(0,255,255); strokeWeight(10); point(scrX, scrY); cam.endHUD();
} // end if(!mouseStillPressed){
gF[indexGF].setSelectionState(true);
// find nearest gridpoint
scrX = screenX( grid.px[indexGP], grid.py[indexGP], 0 );
scrY = screenY( grid.px[indexGP], grid.py[indexGP], 0 );
mouseDisShortest = sqrt( sq(mouseX - scrX) + sq(mouseY - scrY) );
for(int i = 0; i < grid.px.length; i++){
scrX = screenX( grid.px[i], grid.py[i], 0 );
scrY = screenY( grid.px[i], grid.py[i], 0 );
float mouseDis = sqrt( sq(mouseX - scrX) + sq(mouseY - scrY) );
if( mouseDis <= mouseDisShortest){
indexGP = i; mouseDisShortest = mouseDis;
}
} // end for i
//scrX = screenX( grid.px[indexGP], grid.py[indexGP], 0 );
//scrY = screenY( grid.px[indexGP], grid.py[indexGP], 0 );
//cam.beginHUD(); stroke(0,255,255); strokeWeight(20); point(scrX, scrY);cam.endHUD();
if (mousePressed){
mouseStillPressed = true;
if (mouseButton == LEFT){
float dx = grid.px[indexGP] - gF[indexGF].location.x;
float dy = grid.py[indexGP] - gF[indexGF].location.y;
gF[indexGF].location.x += dx/6;
gF[indexGF].location.y += dy/6;
} // end if
if (mouseButton == RIGHT) {
gF[indexGF].radius += 1.0*(pmouseY - mouseY)/10.0;
if( gF[indexGF].radius < 10) gF[indexGF].radius = 10;
if( gF[indexGF].radius > 100) gF[indexGF].radius = 100;
} // end if
} else {
mouseStillPressed = false;
} // end if else
} // end if (key == 'm' || key == 'M'){
} else {
cam.setMouseControlled(true);
} // end if else
} // end void moveAndScaleSpheres()