-
Notifications
You must be signed in to change notification settings - Fork 0
/
particle.c
123 lines (116 loc) · 3.42 KB
/
particle.c
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
#include <stdlib.h>
#include <math.h>
#include "perlin.h"
#include "particle.h"
#define PI 3.14159265358979323846
//Get X component of particle's velocity
float getDX(int width, int height, float x,float y,float noise,int mode) {
//Angle,rotation,x/width,y/height
float a,r,xw,yh;
switch(mode){
case 0:
return cos(noise*PI*2);
case 1:
return cos(noise*PI*8)+1;
case 2:
return cos(noise*PI*2)+(x-3*width/4)/(width/3);
case 3:
return sin(noise*PI*2);
case 4:
a = tan(noise*PI*8)*PI*2+5;
r = sin(noise*PI*8);
return cos(a)*r;
case 5:
a = cos(noise*PI*8)*PI*2+5;
r = sin(noise*PI*8);
return cos(a)*r;
case 6:
a = cos(noise*PI*8)*PI*2;
r = sin(noise*PI*8);
return cos(a)*r;
case 7:
a = tan(noise*PI*8)*PI*2;
r = (double)height/1000;
return cos(a)*r;
case 8:
return sin(noise*PI*2/2)*cos(noise*PI*2/3)*3;
case 9:
a = tan(noise*PI*8)*PI*2+5;
r = sin(noise*PI*8);
return cos(a)*r;
default:
return cos(noise*PI*2);
}
}
//Get Y component of particle's velocity
float getDY(int width, int height, float x,float y,float noise,int mode){
//Angle,rotation,x/width,y/height
float a,r,xw,yh;
switch(mode){
case 0:
return sin(noise*PI*2);
case 1:
return tan(noise*PI*8)/2;
case 2:
return sin(noise*PI*2)+(y-2*height/3)/(width/3);
case 3:
return tan(noise*PI*2);
case 4:
a = tan(noise*PI*8)*PI*4+5;
r = sin(noise*PI*8);
return sin(a)*r;
case 5:
a = cos(noise*PI*8)*PI*4+5;
r = sin(noise*PI*8);
return sin(a)*r;
case 6:
a = cos(noise*PI*8)*PI*2;
r = sin(noise*PI*8);
return sin(a)*r;
case 7:
a = tan(noise*PI*8)*PI*2;
r = (double)height/1000;
return sin(a)*r;
case 8:
return cos(noise*PI*2/2)*sin(noise*PI*2/3);
case 9:
yh = y/height;
return cos(noise*PI*2/4)*sin(noise*PI*2/3)*(1+4*yh*yh*yh);
default:
return sin(noise*PI*2);
}
}
//Create single particle using specified parameters
Particle newParticle(int width, int height, int noiseScale, int mode, int xoff, int yoff) {
float x = rand()%(width+400)-200;
float y = rand()%(height+400)-200;
float noise = perlin2d(x/noiseScale+xoff,y/noiseScale+yoff,1,1);
float dx = getDX(width,height,x,y,noise,mode);
float dy = getDX(width,height,x,y,noise,mode);
Particle p = { x, y, x, y, dx, dy, mode };
return p;
}
//Fills Particle array particles using specified parameters
void getParticles(Particle * particles, int numParticles, int width, int height, int noiseScale, int mode, int xoff, int yoff){
for (int i = 0;i<numParticles;i++){
particles[i] = newParticle(width,height,noiseScale,mode,xoff,yoff);
}
}
void updateParticles(Particle * particles, int numParticles,int width,int height,int noiseScale, int xoff, int yoff){
for (int i = 0;i<numParticles;i++){
//Update particles position, direction
Particle p = particles[i];
p.prevX = p.x;
p.prevY = p.y;
p.x+=p.dx;
p.y+=p.dy;
float noise = perlin2d(p.x/noiseScale+xoff,p.y/noiseScale+yoff,1,1);
p.dx = getDX(width,height,p.x,p.y,noise,p.mode);
p.dy = getDY(width,height,p.x,p.y,noise,p.mode);
//Reset particle if out of bounds
if (p.x < -200 || p.y < -200 || p.x > width+200 || p.y > height+200){
p = newParticle(width,height,noiseScale,p.mode,xoff,yoff);
}
particles[i] = p;
}
}