|
2 | 2 |
|
3 | 3 | import android.util.Pair; |
4 | 4 |
|
5 | | -import androidx.annotation.NonNull; |
6 | | - |
7 | | -import java.util.ArrayList; |
8 | 5 | import java.util.HashMap; |
| 6 | +import java.util.Iterator; |
9 | 7 | import java.util.Map; |
10 | 8 |
|
11 | 9 | import processing.core.PApplet; |
12 | 10 | import processing.core.PVector; |
13 | 11 |
|
14 | 12 | class FlowField { |
15 | 13 | private static final Map<Pair<Integer, Integer>, Float> xOffCache = new HashMap<>(); |
16 | | - private float zOff = 0; |
17 | 14 |
|
18 | | - void update(ArrayList<Particle> particles, PApplet parent) { |
19 | | - var pos2Noise = new HashMap<Pair<Integer, Integer>, PVector>(); |
| 15 | + private float zOff = 0; |
20 | 16 |
|
21 | | - for (int i = 0; i < particles.size(); i++) { |
22 | | - var pos = new Pair<>( |
23 | | - PApplet.floor(particles.get(i).pos.x / Sketch.Config.FLOW_FIELD_GRANULARITY) + 1, |
24 | | - PApplet.floor(particles.get(i).pos.y / Sketch.Config.FLOW_FIELD_GRANULARITY) + 1 |
25 | | - ); |
| 17 | + void updateAndApplyTo(Iterator<Particle> particles, PApplet parent) { |
| 18 | + HashMap<Pair<Integer, Integer>, PVector> forceCash = new HashMap<>(); |
26 | 19 |
|
27 | | - if (pos2Noise.containsKey(pos)) |
28 | | - particles.get(i).applyFlowFieldVector(pos2Noise.get(pos)); |
29 | | - else { |
30 | | - PVector v = PVector.fromAngle(parent.noise(xOff(pos), 0, zOff) * 25.132742f).normalize(); |
31 | | - particles.get(i).applyFlowFieldVector(v); |
32 | | - pos2Noise.put(pos, v); |
33 | | - } |
34 | | - } |
| 20 | + particles.forEachRemaining((particle -> particle.applyForceVector( |
| 21 | + getForceVector( |
| 22 | + Pair.create( |
| 23 | + PApplet.floor(particle.pos.x / Sketch.Config.FLOW_FIELD_GRANULARITY) + 1, |
| 24 | + PApplet.floor(particle.pos.y / Sketch.Config.FLOW_FIELD_GRANULARITY) + 1 |
| 25 | + ), |
| 26 | + forceCash, |
| 27 | + parent |
| 28 | + ) |
| 29 | + ))); |
35 | 30 |
|
36 | 31 | zOff += Sketch.Config.FLOW_FIELD_Z_OFF_INCREMENT; |
37 | 32 | } |
38 | 33 |
|
39 | | - private float xOff(@NonNull Pair<Integer, Integer> pos) { |
| 34 | + private PVector getForceVector(Pair<Integer, Integer> pos, HashMap<Pair<Integer, Integer>, PVector> forceCash, PApplet parent) { |
| 35 | + if (forceCash.containsKey(pos)) |
| 36 | + return forceCash.get(pos); |
| 37 | + PVector v = PVector.fromAngle(parent.noise(getXOff(pos), 0, zOff) * 25.132742f).normalize(); |
| 38 | + forceCash.put(pos, v); |
| 39 | + return v; |
| 40 | + } |
| 41 | + |
| 42 | + private float getXOff(Pair<Integer, Integer> pos) { |
40 | 43 | if (xOffCache.containsKey(pos)) |
41 | 44 | //noinspection ConstantConditions |
42 | 45 | return xOffCache.get(pos); |
|
0 commit comments