What the interpolate methods do is to gradually move your vector to the target vector by a factor of 0 < n < 1 on every step, so for example you specify the target vector, then you specify the factor which will dictate how many steps will take to get to the destination, if you choose 1.0 the vector will jump to the target in 1 step along the way but if you select 0.5 the vector will move to half the distance to its target, them will move half of the remaining distance and the process continue until it reach its target,
Run the following code and play with the factor value (line 33) to get the idea
- import toxi.geom.Vec2D;
- import toxi.math.noise.PerlinNoise;
- PerlinNoise n;
- Vec2D p, pp, t;
- float noiseScale = 300,
- noiseStrength = 10,
- stepSize = 15,
- angle;
- boolean atTarget = false;
- void setup() {
- size(320, 240);
- 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, sin(angle) * stepSize);
- */
-
- // Steer towards our destination
- p = p.interpolateTo(t, 0.5);
-
- // Draw where we've been
- line(pp.x, pp.y, p.x, p.y);
- ellipse(p.x, p.y, 5, 5);
-
-
- // Check to see if we're within target
- if(dist(p.x, p.y, t.x, t.y) < 25) {
- atTarget = true;
- println( "at target" );
- }
- }
- }
- // Click to reset
- void mouseReleased() {
- setPerlinNoise();
- }
- void setPerlinNoise() {
-
- background(255);
-
- // Reset variables
- p = new Vec2D(0, height);
- pp = new Vec2D(p);
- t = new Vec2D(random(width/2, width), random(0, height));
- 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);
- }
Now the problem we have here is that the angle is distorting the p vector too much and it will never reach the target, the times it does is just by luck, Im very interested in solving this problem, so I will post any progress when I get a chance
Cheers
- rS