[Toxiclibs] PerlinNoise to Specified Target Destination
- import toxi.geom.Vec2D;
- import toxi.math.noise.PerlinNoise;
- PerlinNoise n;
- Vec2D p, pp, t;
- float noiseScale = 300,
- noiseStrength = 10,
- stepSize = 5,
- angle;
- boolean atTarget = false;
- void setup() {
- size(800, 600);
- smooth();
- setPerlinNoise();
- }
- void draw() {
- stroke(0);
- fill(255);
- if(!atTarget) {
- pp.set(p);
- angle = n.noise(p.x/noiseScale, p.y/noiseScale) * noiseStrength;
- // Create our FlowField position
- p.addSelf(cos(angle) * stepSize * 3,
- sin(angle) * stepSize * 3);
- // Steer towards our destination
- p = p.interpolateToSelf(t, .1);
- // Draw where we've been
- ellipse(p.x, p.y, 5, 5);
- line(pp.x, pp.y, p.x, p.y);
- // Check to see if we're within target
- if(abs(t.x - p.x) < 25 && abs(t.y - p.y) < 25) {
- atTarget = true;
- }
- }
- }
- // Click to reset
- void mouseReleased() {
- setPerlinNoise();
- }
- void setPerlinNoise() {
- background(255);
- // Reset variables
- p = new Vec2D(0, height);
- pp = new Vec2D(p.x, p.y);
- t = new Vec2D(width/2, height/2);
- n = new PerlinNoise();
- atTarget = false;
- p.set(0, height);
- n = new PerlinNoise();
- // Draw our target region
- noStroke();
- fill(255, 200, 0);
- ellipse(t.x, t.y, 50, 50);
- }
—
http://jonobr1.com/