-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFlowField.js
More file actions
134 lines (112 loc) · 3.22 KB
/
FlowField.js
File metadata and controls
134 lines (112 loc) · 3.22 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
/// FLOW FIELD
function FlowField(_Nx, _Ny, _type) {
this.Nx = _Nx;
this.Ny = _Ny;
this.type = _type;
this.N = _Nx * _Ny;
this.xStep = W/_Nx;
this.yStep = H/_Ny;
// -------------------------------------------------
// -------------------------------------------------
this.make2Darray = function()
{
var arr = [];
for(i=0; i<this.N; i++)
{
arr[i] = [];
}
return arr;
}
this.forces = this.make2Darray();
// -------------------------------------------------
this.init = function()
{
var noiseScale = 2;
for(i=0; i<this.Nx; i++)
{
for(j=0; j<this.Ny; j++)
{
switch(this.type)
{
case 0:
var theta = map(noise(i * noiseScale, j * noiseScale), 0, 1, 0, TWO_PI);
this.forces[i][j] = p5.Vector.fromAngle(theta);
break;
case 1:
this.forces[i][j] = new p5.Vector(random(-1,1), random(-1,1)).normalize();
}
}
}
}
this.init();
// -------------------------------------------------
// here we convert a position to discrete cell coordinates
this.positionToIndex = function(x,y)
{
return new p5.Vector(
floor(x/(this.xStep+0.5)), // add 0.5 to avoid rounding to the last grid cell + 1 and cause an out of bound
floor(y/(this.yStep+0.5))
);
}
// -------------------------------------------------
/// should ideally return an interpolated value of all adjacent cells (in quadricentric coordinates)
this.getForceAtPosition = function(x,y)
{
var IJ = this.positionToIndex(x,y);
return this.getForce(IJ.x, IJ.y);
}
// -------------------------------------------------
/// GET FORCE FROM CELL i,j
this.getForce = function(i,j)
{
return(this.forces[i][j].copy());
}
// -------------------------------------------------
this.setForceAtPosition = function(x,y, force)
{
var IJ = this.positionToIndex(x,y);
this.setForce(IJ.x, IJ.y, force);
}
// -------------------------------------------------
/// SET THE CELL i,j WITH force
this.setForce = function(i,j,force)
{
this.forces[i][j] = force;
}
// -------------------------------------------------
this.sumForceAtPosition = function(x,y, force)
{
var IJ = this.positionToIndex(x,y);
this.sumForce(IJ.x, IJ.y, force);
}
// -------------------------------------------------
/// SUM A FORCE TO TEH CURRENT ONE IN CELL i,j AND NORMALIZE
this.sumForce = function(i,j,force)
{
var __F = this.getForce(i,j);
if(__F === undefined)
console.log("Error trying to access force in cell " + i + ", " + j);
this.setForce(i,j,__F.add(force).normalize());
}
// -------------------------------------------------
this.draw = function()
{
//console.log(this.forces[10][10]);
rectMode(CENTER);
for(i=0; i<this.Nx; i++)
{
for(j=0; j<this.Ny; j++)
{
var centerOffset = createVector(this.xStep * 0.5, this.ystep * 0.5);
var lx = i * this.xStep + centerOffset.x;
var ly = j * this.yStep + centerOffset.y;
var _F = this.forces[i][j];
_F.setMag(10);
stroke(100);
fill(150);
rect(lx, ly, 2,2);
line(lx, ly, lx + _F.x, ly + _F.y);
}
}
}
}