-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollisionDetectionTest.pde
128 lines (112 loc) · 3.95 KB
/
CollisionDetectionTest.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
Particle c[];
int numParticles = 26*4;
CollisionDetector cd;
CollisionRes cr;
boolean pause = true;
void draw(){
background(255);
//collision handling
if(!pause){
cr.resolveCollisions(cd.detectCollisions());
for(Particle p: c){
p.move();
}
cd.update();
}
fill(20,20,250,125);
stroke(20,20,250,125);
circle(c[60].pos.x,c[60].pos.y,100);
//drawing
cd.draw();
for(Particle p: c){
p.draw();
}
if(keyPressed){
if(key=='g'){
cd = new GridCollisionDetector(c,100);
}
if(key=='s'){
cd = new SimpleCollisionDetector(c);
}
if(key == 'd'){
cd = new DelanuayCollisionDetector(c);
}
if(key == 'n'){
cd = new NullCollisionDetector();
}
}
fill(20,20,250);
stroke(0);
circle(c[60].pos.x,c[60].pos.y,2*c[60].r);
}
void keyPressed(){
if(key == 'a'){
Particle[] ps = new Particle[c.length+1];
for(int i=0;i<c.length;i++)ps[i]=c[i];
ps[c.length] = new Particle();
c = ps;
cd.reset(c);
}
if(key == 'p'){
pause=!pause;
}
if(key == 'r'){
for(Particle p: c){
p.v.x = -p.v.x;
p.v.y = -p.v.y;
}
}
if(key == 'c'){
System.out.println(c.length);
}
}
void setup(){
size(600,600);
c = new Particle[108];
int particleCount = 0;
float rad = 4;
float x_init = 130;
float y_init = 170;
float dx = 16;
float dy = 16;
// E
for(int i=0;i<10;i++){
c[particleCount] = new Particle(x_init + random(1),y_init + (rad+dy)*i, rad); particleCount++;
c[particleCount] = new Particle(x_init + random(1) + rad + dx, y_init + (rad+dy)*i, rad); particleCount++;
}
for(int i=0;i<6;i++){
c[particleCount] = new Particle(x_init + random(1) + (i+2)*(rad + dx),y_init, rad); particleCount++;
c[particleCount] = new Particle(x_init + random(1) + (i+2)*(rad + dx),y_init + rad+ dy, rad); particleCount++;
c[particleCount] = new Particle(x_init + random(1) + (i+2)*(rad + dx),y_init + 8*(rad+dy), rad); particleCount++;
c[particleCount] = new Particle(x_init + random(1) + (i+2)*(rad + dx),y_init + 9*(rad+dy), rad); particleCount++;
}
for(int i=0;i<4;i++){
c[particleCount] = new Particle(x_init + random(1) + (i+2)*(rad + dx),y_init + 4*(rad+dy), rad); particleCount++;
c[particleCount] = new Particle(x_init + random(1) + (i+2)*(rad + dx),y_init + 5*(rad+dy), rad); particleCount++;
}
// G
for(int i=0;i<10;i++){
c[particleCount] = new Particle(x_init + random(1) + 10*(rad + dx),y_init + (rad+dy)*i, rad); particleCount++;
c[particleCount] = new Particle(x_init + random(1) + 11*(rad + dx),y_init + (rad+dy)*i, rad); particleCount++;
}
for(int i=0;i<6;i++){
c[particleCount] = new Particle(x_init + random(1) + (i+12)*(rad + dx),y_init, rad); particleCount++;
c[particleCount] = new Particle(x_init + random(1) + (i+12)*(rad + dx),y_init + rad+ dy, rad); particleCount++;
c[particleCount] = new Particle(x_init + random(1) + (i+12)*(rad + dx),y_init + 8*(rad+dy), rad); particleCount++;
c[particleCount] = new Particle(x_init + random(1) + (i+12)*(rad + dx),y_init + 9*(rad+dy), rad); particleCount++;
}
for(int i=0;i<4;i++){
c[particleCount] = new Particle(x_init + random(1) + (i+14)*(rad + dx),y_init + 4*(rad+dy), rad); particleCount++;
c[particleCount] = new Particle(x_init + random(1) + (i+14)*(rad + dx),y_init + 5*(rad+dy), rad); particleCount++;
}
for(int i=0;i<2;i++){
c[particleCount] = new Particle(x_init + random(1) + (i+16)*(rad + dx),y_init + 6*(rad+dy), rad); particleCount++;
c[particleCount] = new Particle(x_init + random(1) + (i+16)*(rad + dx),y_init + 7*(rad+dy), rad); particleCount++;
}
// change here the type of cd to use a different collision detect algorithm
cd = new DelanuayCollisionDetector(c);
//cd = new DelanuayCollisionDetector(c);
// change here the type of cr to use a different collision resolution algorithm
// for now there's only one algorithm implemented for collision resolution
cr = new CollisionRes();
}