-
Notifications
You must be signed in to change notification settings - Fork 0
/
rivers.js
165 lines (142 loc) · 4.83 KB
/
rivers.js
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
// River wandering
class River {
constructor(x, y) {
// variables
this.pos = createVector(x, y);
this.origin = createVector(x, y);
this.vel = createVector(1, 0);
this.acc = createVector(0, 0);
this.arrived = false;
this.complete = false;
this.targetIndex = 0;
this.distToTarget = width;
this.angleToTarget = 0;
this.targetProgress = 0;
this.noiseVal = 0;
this.path = [];
this.dotPath = [];
// parameters
this.mass = 2;
this.maxSpeed = 4;
this.maxForce = 2;
this.maxAngleFromTarget = 0.8 * PI;
this.slowRadius = 30;
this.stopRadius = 20;
this.noiseScale = 60 / width;
}
// Create new river with the same position, velocity and acc
replicate() {
let newRiver = new River(0, 0);
newRiver.pos = this.pos.copy();
newRiver.vel = this.vel.copy();
newRiver.acc = this.acc.copy();
return newRiver;
}
// update progress to reach target
updateTargetProgress(target) {
let distOriginTarget = p5.Vector.sub(target, this.origin).mag();
this.distToTarget = p5.Vector.sub(target, this.pos).mag();
this.angleToTarget = p5.Vector.sub(target, this.pos).heading();
this.targetProgress = 1 - (this.distToTarget / distOriginTarget);
}
// wander, with noise, towards target
wanderToTarget(target) {
let force = createVector(1, 0);
let angleOffset = this.maxAngleFromTarget;
let forceMag = this.maxForce;
this.updateTargetProgress(target);
// if is close to the target
if (this.distToTarget < this.slowRadius) {
// narrow down to target
angleOffset = map(this.targetProgress, 0, 1, angleOffset, 0)
}
// if arrived to the target
if (this.distToTarget < this.stopRadius) {
// stop
forceMag = 0;
this.arrived = true;
}
// apply noise offset on angle to target
let angle = map(this.noiseVal, 0, 1, (this.angleToTarget - angleOffset), (this.angleToTarget + angleOffset));
// create wander force from that angle and max mag
force.setHeading(angle);
force.setMag(forceMag);
force.limit(this.maxForce);
return force;
}
// apply a force to the river
applyForce(force, show = false) {
this.acc.add(force.mult(1 / this.mass));
// debug parameter to draw force vector
if (show) {
this.showVector(force, origin = this.pos);
}
}
// update physics
update() {
this.vel.add(this.acc);
this.vel.limit(this.maxSpeed);
this.pos.add(this.vel);
this.acc.set(0, 0);
// calculate the noise value for this position
this.noiseVal = noise(this.pos.x * this.noiseScale, this.pos.y * this.noiseScale);
// add current position to the river path
this.path.push({
x: this.pos.x,
y: this.pos.y,
weight: this.noiseVal
});
}
// draw lines
show() {
push();
stroke(lineColor);
strokeWeight(2);
let minStroke = 0.1;
let maxStroke = 5;
// loop path drawing lines with variable stroke weight
let previousPoint = this.path[0];
for (let point of this.path) {
strokeWeight(map(point.weight, 0, 1, minStroke, maxStroke));
line(point.x, point.y, previousPoint.x, previousPoint.y)
previousPoint = point;
}
pop();
}
// debug function to draw vector
showVector(vector, origin = createVector(0, 0), scale = 200, color = 'rgb(0,255,0)') {
push();
stroke(color);
strokeWeight(1);
fill(255);
translate(origin.x, origin.y);
line(0, 0, scale * vector.x, scale * vector.y);
pop();
}
showDot(x = 0, y = 0, r = 8, turns = 3) {
if (this.dotPath.length < 1) {
for (var a = 0; a < turns * TWO_PI; a += 0.1) {
let noiseVal = noise(a * 0.2, this.pos.y);
let rOffset = map(noiseVal, 0, 1, 0, 1)
this.dotPath.push({
x: r * rOffset * cos(a) + x,
y: r * rOffset * sin(a) + y,
weight: noiseVal
})
}
} else {
push();
stroke(lineColor);
noFill();
let previousPoint = this.dotPath[0];
let minStroke = 0.1;
let maxStroke = 2;
for (let point of this.dotPath) {
strokeWeight(map(point.weight, 0, 1, minStroke, maxStroke));
line(point.x, point.y, previousPoint.x, previousPoint.y)
previousPoint = point;
}
pop();
}
}
}