-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollision.pde
179 lines (171 loc) · 4.58 KB
/
Collision.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
interface CollisionDetector{
Particle[][] detectCollisions();
void draw();
void update();
void reset(Particle[] p);
}
class SimpleCollisionDetector implements CollisionDetector{
Particle p[];
SimpleCollisionDetector(Particle p[]){this.p = p;}
void reset(Particle[] p){
this.p = p;
}
Particle[][] detectCollisions(){
int numCollisions = 0;
for(int i=0;i<p.length;i++){
for(int j=i;j<p.length;j++){
if(p[i].collidesWith(p[j]))numCollisions++;
}
}
Particle[][] res = new Particle[numCollisions][2];
int colCounter = 0;
for(int i=0;i<p.length;i++){
for(int j=i;j<p.length;j++){
if(p[i].collidesWith(p[j])){
res[colCounter][0] = p[i];
res[colCounter][1] = p[j];
colCounter++;
}
}
}
return res;
}
void update(){};
void draw(){};
}
class NullCollisionDetector implements CollisionDetector{
NullCollisionDetector(){}
void reset(Particle[] p){}
Particle[][] detectCollisions(){
return new Particle[0][2];
}
void update(){};
void draw(){};
}
class GridCell{
ArrayList<Particle> p;
int x,y;
int gridCellSize;
GridCell(int x, int y, int gridCellSize){
this.p = new ArrayList<Particle>();
this.x = x;
this.y = y;
this.gridCellSize = gridCellSize;
}
void clear(){
p.clear();
}
void add(Particle a){
if(!p.contains(a))p.add(a);
}
void update(){
ArrayList<Particle> toDel = new ArrayList<Particle>();
for(Particle a: p){
if(a.pos.x < x*gridCellSize || a.pos.x > (x+1)*gridCellSize){
toDel.add(a);
}
if(a.pos.y < y*gridCellSize || a.pos.y > (y+1)*gridCellSize){
toDel.add(a);
}
}
for(Particle a: toDel){
p.remove(a);
}
}
}
class GridCollisionDetector implements CollisionDetector {
Particle p[];
int gridCellSize;
GridCell gcs[][];
void reset(Particle[] p){
this.p = p;
gcs = new GridCell[height/gridCellSize][width/gridCellSize];
for(int i=0;i*gridCellSize<height;i++){
for(int j=0;j*gridCellSize<width;j++){
gcs[i][j] = new GridCell(i,j,gridCellSize);
}
}
}
GridCollisionDetector(Particle p[], int gridCellSize){
this.p = p;
this.gridCellSize = gridCellSize;
gcs = new GridCell[height/gridCellSize][width/gridCellSize];
for(int i=0;i*gridCellSize<height;i++){
for(int j=0;j*gridCellSize<width;j++){
gcs[i][j] = new GridCell(i,j,gridCellSize);
}
}
}
Particle[][] detectCollisions(){
for(int i=0;i*gridCellSize<height;i++){
for(int j=0;j*gridCellSize<width;j++){
gcs[i][j].update();
}
}
for(Particle a: p){
if(a.pos.x < width && a.pos.x >0 && a.pos.y<height && a.pos.y>0)
gcs[int(a.pos.y/gridCellSize)][int(a.pos.x/gridCellSize)].add(a);
}
int[] dx = {-1,0,1,1,1,0,-1,-1};
int[] dy = {1,1,1,0,-1,-1,-1,0};
ArrayList<Particle[]> res = new ArrayList<Particle[]>();
for(int i=0;i*gridCellSize<height;i+=2){
for(int j=0;j*gridCellSize<width;j+=2){
ArrayList<Particle> toCollide = gcs[i][j].p;
for(int k=0;k<8;k++){
if(i+dx[k]>=0 && (i+dx[k])*gridCellSize<width && j+dy[k]>=0 && (j+dy[k])*gridCellSize<height){
toCollide.addAll(gcs[i+dx[k]][j+dy[k]].p);
}
}
for(int ii=0;ii<toCollide.size();ii++){
for(int jj=ii+1;jj<toCollide.size();jj++){
if(toCollide.get(ii).collidesWith(toCollide.get(jj))){
Particle[] a = {toCollide.get(ii),toCollide.get(jj)};
res.add(a);
}
}
}
}
}
Particle[][] a = new Particle[res.size()][2];
for(int i=0;i<res.size();i++){
a[i][0] = res.get(i)[0];
a[i][1] = res.get(i)[1];
}
return a;
//return super.detectCollisions();
}
void update(){};
void draw(){
stroke(40);
for(int i=0;i*gridCellSize<height;i++){
line(0,i*gridCellSize,width,i*gridCellSize);
}
for(int i=0;i*gridCellSize<width;i++){
line(i*gridCellSize,0,i*gridCellSize,height);
}
}
}
class DelanuayCollisionDetector implements CollisionDetector{
Particle[] p;
DelaunayMesh ms;
DelanuayCollisionDetector(Particle[] p){
this.p = p;
this.ms = new DelaunayMesh(p);
}
void update(){
//TODO CHANGE UPDATE MESH METHOD TO DELETE NEXT LINE AND STOP REBUILDING THE MESH AT EACH TIME STEP
this.ms = new DelaunayMesh(p);
ms.update();
}
Particle[][] detectCollisions(){
return ms.detectCollisions();
}
void reset(Particle[] p){
this.p = p;
this.ms = new DelaunayMesh(p);
}
void draw(){
ms.draw();
}
}