More circles

Let's define the count of the circles that we want the application to handle. Add the following line of code just after the #include statements:

#define CIRCLE_COUNT 100

Now, let's go to the class declaration part and change the declarations of our variables to arrays:

Vec2f currentPosition[CIRCLE_COUNT];
Vec2f targetPosition[CIRCLE_COUNT];
float circleRadius[CIRCLE_COUNT];

As you can see, we used the previously defined constant as the size of our arrays. By doing that we can change the circle count easily later.

Next, we have to change some code in the setup() method implementation:

void BasicAnimationApp::setup() {
  for(int i=0; i<CIRCLE_COUNT; i++) {
    currentPosition[i].x=Rand::randFloat(0,getWindowWidth());
    currentPosition[i].y=Rand::randFloat(0,getWindowHeight());
    targetPosition[i].x=Rand::randFloat(0,getWindowWidth());
    targetPosition[i].y=Rand::randFloat(0,getWindowHeight());
    circleRadius[i] = Rand::randFloat(1, 10);
  }
}

Basically, we wrapped the same code we had before into a for loop that iterates over all our parameter arrays and sets initial values for each of them.

Don't compile yet as we still have to make changes to the update() and draw() methods in a similar way. Change our update() method as follows:

void BasicAnimationApp::update() {
  Vec2f difference;
  for (int i=0; i<CIRCLE_COUNT; i++) {
    difference = targetPosition[i] - currentPosition[i];
    difference *= 0.95f;
    currentPosition[i] = targetPosition[i] - difference;

    if (currentPosition[i].distance(targetPosition[i]) < 1.0f) {
      targetPosition[i].x =
      Rand::randFloat(0,getWindowWidth());
      targetPosition[i].y =
      Rand::randFloat(0,getWindowHeight());
    }
  }
}

And lastly, change our draw() method implementation as follows:

void BasicAnimationApp::draw() {
  gl::clear( Color( 0, 0, 0 ) );
  for (int i=0; i<CIRCLE_COUNT; i++) {
    gl::drawSolidCircle( currentPosition[i], circleRadius[i] );
  }
}

Compile and run our application now. This looks a bit more interesting!

More circles

It seems that 100 circles are not enough, so how about we set the CIRCLE_COUNT constant to 1000?

#define CIRCLE_COUNT 1000

No problem!

But what if we don't want to focus on quantity, but on the quality of movement? This is where animation easing joins the game.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.104.27