-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRenderClass_v7.java
More file actions
403 lines (282 loc) · 11.6 KB
/
RenderClass_v7.java
File metadata and controls
403 lines (282 loc) · 11.6 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// sdurant12
// 12/25/2012
// a pathfinding particle system, inspired by pathfinding.avi (youtube.com)
// TODO: use verlet integration for speed, and more elegant collision reaction
// Currently: (temp) dividing densityArray by 2 instead of clearing it to zero
// Currently: (temp) dividing densityArray by two when accessing gradient
// to avoid squares
package particlesystem_v7;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.util.Random;
public class RenderClass_v7 extends JPanel implements MouseListener, MouseMotionListener, KeyListener{
private static final int TILE_SIZE = 64;
private static final int PARTICLE_SIZE = 1;
private static int WIDTH;
private static int HEIGHT;
private static int TILE_COUNT_X;
private static int TILE_COUNT_Y;
private boolean[][] traversable;
private float[][] distance;
private float[][] yAccelGrid;
private float[][] xAccelGrid;
private boolean paused = false;
private BufferedImage particleImage;
private int[] particleRaster;
private int[][] densityArray;
// draw on densityRaster using the array for the particles as reference (all ones for fire) (metablobabble texture for water)
// based on the value in densityRaster transfer to particleRaster based off of the
// particle types color gradient array
private Random r = new Random();
ArrayList<Particle> particleAL = new ArrayList<Particle>();
public RenderClass_v7(int width, int height) {
WIDTH = width;
HEIGHT = height;
TILE_COUNT_X = 1 + (WIDTH / TILE_SIZE);
TILE_COUNT_Y = 1 + (HEIGHT / TILE_SIZE);
traversable = new boolean[TILE_COUNT_X][TILE_COUNT_Y];
distance = new float[TILE_COUNT_X][TILE_COUNT_Y];
particleImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
particleRaster = ((DataBufferInt) particleImage.getRaster().getDataBuffer()).getData();
densityArray = new int[WIDTH][HEIGHT];
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(this);
init();
}
public void init() {
for(int xi = 0; xi < TILE_COUNT_X; xi++){
for(int yi = 0; yi < TILE_COUNT_Y; yi++){
traversable[xi][yi] = true;
if( (xi == TILE_COUNT_X/3 || xi == TILE_COUNT_X*2/3 || xi == TILE_COUNT_X/2 ) && yi != TILE_COUNT_Y/3 /*&& yi != TILE_COUNT_Y*2/3*/ ){
traversable[xi][yi] = false;
}
if(r.nextFloat() < .1f && yi != TILE_COUNT_Y/3){
traversable[xi][yi] = false;
}
if(xi < 9 && yi < 5){
traversable[xi][yi] = true;
}
}
}
distance(TILE_COUNT_X/4,TILE_COUNT_Y/4);
// spawn particles
for(int i = 0; i < 800000; i++){
float x = 100 + .5f*r.nextFloat()*TILE_SIZE*TILE_COUNT_X;
float y = 100 + .5f*r.nextFloat()*TILE_SIZE*TILE_COUNT_Y;
if(traversable[(int)(x/TILE_SIZE)][(int)(y/TILE_SIZE)]){
particleAL.add( new Particle( x, y , r.nextFloat()*10, r.nextFloat()*10 ));
}
}
}
// calculates the distance from x,y to every point considered traversable[][]
public void distance( int startX, int startY ){
float[][] tempDistance = new float[TILE_COUNT_X][TILE_COUNT_Y];
for(int xi = 0; xi < TILE_COUNT_X; xi++){
for(int yi = 0; yi < TILE_COUNT_Y; yi++){
tempDistance[xi][yi] = 0;
}
}
ArrayList<Node> heap = new ArrayList<Node>();
//add first node to it.
if(traversable[startX][startY]){
heap.add( new Node(startX,startY,0) );
tempDistance[startX][startY] = .1f;
}else{
// startx and starty are outside of acceptable range
}
while( heap.size() > 0 ){
Node n = heap.get(0);
int x = n.getX();
int y = n.getY();
float d = n.getD();
//Make an array containing the (x,y,d) and then go through it checking x,y
float[] distGrid // a 3*8 array containing (dx,dy,d) for nodes
= {
-1, 0, 1,
0,-1, 1,
0, 1, 1,
1, 0, 1,
-1,-1, 1.4f,
-1, 1, 1.4f,
1,-1, 1.4f,
1, 1, 1.4f,};
for( int i = 0; i < distGrid.length; i+= 3){
int tempX = x + (int)distGrid[i+0];
int tempY = y + (int)distGrid[i+1];
float tempD = d + distGrid[i+2];
// if traversable[][] && if in map && if new will be smaller than old
if(tempX >= 0 && tempY >= 0 && tempX < TILE_COUNT_X && tempY < TILE_COUNT_Y && traversable[tempX][tempY] && (tempDistance[tempX][tempY] == 0 || tempDistance[tempX][tempY] > tempD)){
heap.add( new Node( tempX, tempY, tempD ) );
tempDistance[tempX][tempY] = tempD;
}
}
heap.remove(0);
}
if(traversable[startX][startY]){
distance = tempDistance;
}
acceleration();
}
public void acceleration(){
float[][] tempxAccelGrid = new float[TILE_COUNT_X][TILE_COUNT_Y];
float[][] tempyAccelGrid = new float[TILE_COUNT_X][TILE_COUNT_Y];
for(int xi = 0; xi < TILE_COUNT_X; xi++){
for(int yi = 0; yi < TILE_COUNT_Y; yi++){
tempxAccelGrid[xi][yi] = 0;
tempyAccelGrid[xi][yi] = 0;
int leftTile = xi-1 > 0 ? xi-1 : xi;
int rightTile = xi+1 < TILE_COUNT_X ? xi+1 : xi;
int upTile = yi-1 > 0 ? yi-1: yi;
int downTile = yi+1 < TILE_COUNT_Y ? yi+1 : yi;
leftTile = traversable[leftTile][yi] ? leftTile : xi;
rightTile = traversable[rightTile][yi] ? rightTile : xi;
upTile = traversable[xi][upTile] ? upTile : yi;
downTile = traversable[xi][downTile] ? downTile : yi;
tempxAccelGrid[xi][yi] = distance[leftTile][yi] - distance[rightTile][yi] ;
tempyAccelGrid[xi][yi] = distance[xi][upTile] - distance[xi][downTile] ;
}
}
xAccelGrid = tempxAccelGrid;
yAccelGrid = tempyAccelGrid;
}
public void tick() {
for(int xi = 0; xi < WIDTH; xi++){
for(int yi = 0; yi < HEIGHT; yi++){
densityArray[xi][yi] = 0;//densityArray[xi][yi]/2;
}
}
for(int i = 0; i < particleAL.size(); i++){
// get particle and information
Particle p = particleAL.get(i);
float xPos = p.getxPos();
float yPos = p.getyPos();
float xVel = p.getxVel();
float yVel = p.getyVel();
// if in simulated world (TILE) range
if(xPos +xVel <= TILE_COUNT_X*TILE_SIZE && xPos+xVel >= 0 && yPos+yVel <= TILE_COUNT_Y*TILE_SIZE && yPos+yVel >= 0){
xVel += xAccelGrid[(int)xPos/TILE_SIZE][(int)yPos/TILE_SIZE];
yVel += yAccelGrid[(int)xPos/TILE_SIZE][(int)yPos/TILE_SIZE];
}else /* if not in simulation any more */{
particleAL.remove(i);
}
// if in viewing range
if (xPos + xVel < WIDTH - PARTICLE_SIZE && xPos + xVel > PARTICLE_SIZE && yPos + yVel < HEIGHT - PARTICLE_SIZE && yPos + yVel > PARTICLE_SIZE) {
// if no collision
if (traversable[ (int) ((xPos + xVel) / TILE_SIZE)][(int) ((yPos + yVel) / TILE_SIZE)]) {
xPos += xVel;
yPos += yVel;
xVel += .2 * (densityArray[(int) xPos][(int) yPos] - densityArray[(int) xPos + PARTICLE_SIZE][(int) yPos]);
yVel += .2 * (densityArray[(int) xPos][(int) yPos] - densityArray[(int) xPos][(int) yPos + PARTICLE_SIZE]);
xVel = .9f * xVel;
yVel = .9f * yVel;
} else /* if collision */{
float Vel = .5f * sqrt(xVel * xVel + yVel * yVel);
// if x making it collide
if (!traversable[ (int) ((xPos + xVel) / TILE_SIZE)][(int) (yPos / TILE_SIZE)]) {
yVel = yVel > 0 ? Vel : -Vel;
xVel = -.5f * xVel;
}else /* if x not making it collide */{
xPos += xVel;
}
// if y making it collide
if (!traversable[ (int) (xPos / TILE_SIZE)][(int) ((yPos + yVel) / TILE_SIZE)]) {
xVel = xVel > 0 ? Vel : -Vel;
yVel = -.5f * yVel;
}else /* if y not making it collide */{
yPos += yVel;
}
}
} else /* if not in viewing range */ {
xPos += xVel;
yPos += yVel;
}
p.setParticle( xPos, yPos, xVel, yVel );
// I still simulate if in TILE (simulated) area, but can't draw unless in WIDTH (viewable)
if(xPos < WIDTH - PARTICLE_SIZE && xPos > PARTICLE_SIZE && yPos < HEIGHT - PARTICLE_SIZE && yPos > PARTICLE_SIZE){ // draw on densityArray
for(int xi = 0; xi < PARTICLE_SIZE; xi++){
for(int yi = 0; yi < PARTICLE_SIZE; yi++){
densityArray[ (int)(xPos+xi)][ (int)(yPos+yi)] += 1;
}
}
}
}
//repaint();
}
public float sqrt( float x ){
return x * Float.intBitsToFloat(0x5f3759d5 - (Float.floatToIntBits(x) >> 1));
}
public void paint(Graphics g) {
tick();
for(int xi = 0; xi < TILE_COUNT_X; xi++){
for(int yi = 0; yi < TILE_COUNT_Y; yi++){
if(traversable[xi][yi]){
int blue = ((int)(distance[xi][yi]*6) % 512) < 256 ? (int)(distance[xi][yi]*6) % 256 : 255 - ((int)(distance[xi][yi]*6) % 256);
// gradient distance
g.setColor( new Color( 0, 0, blue ) );
// nice gradient
//g.setColor(new Color( 255*xi/TILE_COUNT_X, 255*yi/TILE_COUNT_Y, 255 ));
}else{
g.setColor( new Color ( 0, 200, 0 ) );
}
g.fillRect( xi*TILE_SIZE, yi*TILE_SIZE, TILE_SIZE, TILE_SIZE);
}
}
g.setColor(Color.WHITE);
g.drawString( "particles: " + particleAL.size(), 10, 10 );
int[] gradient = { 0x00000000, 0xaf9f1604, 0xffdf3509, 0xffef6a10, 0xfffc9b11, 0xffffaa22, 0xffffbb33, 0xffffdd66, 0xffffffaa, 0xffffffff};
//int[] gradient = { 0x00000000, 0x8f666666, 0x8f888888, 0x8f888888, 0x8f666666}; // smoke, low pull to mouse, high dispersion
//int[] gradient = { 0x00000000, 0xffffffff, 0xffffffff, 0xffffffff, 0x8f1144dd}; // water
// iterate throught densityArray and put into particleRaster
for(int xi = 0; xi < WIDTH; xi++){
for(int yi = 0; yi < HEIGHT; yi++){
particleRaster[xi + yi*WIDTH] = gradient[ densityArray[xi][yi] < gradient.length ? densityArray[xi][yi] : gradient.length-1 ];
}
}
g.drawImage(particleImage, -PARTICLE_SIZE/2, -PARTICLE_SIZE/2, WIDTH, HEIGHT, null);
}
@Override
public void mouseClicked(MouseEvent me) {
distance( me.getX()/TILE_SIZE, me.getY()/TILE_SIZE );
}
public boolean getPaused(){
return paused;
}
@Override
public void mousePressed(MouseEvent me) {
}
@Override
public void mouseReleased(MouseEvent me) {
}
@Override
public void mouseEntered(MouseEvent me) {
// NEED TO GET FOCUS HERE SO THAT KEYBOARD EVENTS WORK
requestFocus();
}
@Override
public void mouseExited(MouseEvent me) {
}
@Override
public void mouseDragged(MouseEvent me) {
}
@Override
public void mouseMoved(MouseEvent me) {
distance( me.getX()/TILE_SIZE, me.getY()/TILE_SIZE );
}
@Override
public void keyTyped(KeyEvent ke) {
if( ke.getKeyChar() == 27 /*VK_ESCAPE*/ ){
paused = !paused;
}
}
@Override
public void keyPressed(KeyEvent ke) {
}
@Override
public void keyReleased(KeyEvent ke) {
}
}