object with multiple movement patterns

edited May 2016 in Questions about Code

Hello!

I'm making some music reactive visuals and have come across a problem. I have simplified my code down to easier discuss the problem, but if you need to see the code in context then let me know :)

So I have an array of "fly" objects, which move based on perlin noise. I want to be able to switch between different movement patterns - functions here called move1 and move 2 (which is a simple circular motion for now).

There are no problems when you switch from move1 to move2

The problem is when you switch back to move1- because I am mapping the X and Y position directly from the perlin noise output, there is a jump in position when it switches back - instead of the fly just changing the movement pattern.

I have tried a few different things to correct the problem but no luck so far. What should I do?

Thank you in advance for any help!

Here is the example code-

Fly f;


void setup(){
  size(600,300);
  background(0);
  f=new Fly();
  strokeWeight(5);
  stroke(255);
}

void draw() {
  background(0);

  if (mousePressed==true)
  {
    f.move2();
  }
  else
  {
    f.move1();
  }
}

class Fly{

  float tx;
  float nx;

  float ty;
  float ny;

  float x = 0;
  float y = 0;

  Fly(){
    tx=0;
    ty=100;
  }

void move1(){

  nx = noise(tx);
  ny = noise(ty);
  x = map(nx,0,1,0,width);
  y = map(ny,0,1,0,height);

  tx += 0.01;
  ty += 0.01;


  stroke(255);
  point(x,y);

}

void move2(){

  nx = sin(tx);
  ny = cos(tx);


  x=x+nx;

  y=y+ny;

  tx += 0.03;
  ty += 0.03;



  point(x,y);

}


}
Tagged:

Answers

  • You could make transition with kind of easing

    Google it here in the Forum but it means like not jump somewhere but rather approach the new point.... Hm....

    On deeper thinking: a real transition : calculate old pattern movement result. And new. lerp() both x and y Positions.

    Then when the amt value (see lerp() reference) is 1.0 just use the new transition

Sign In or Register to comment.