Ejemplo n.º 1
0
int main() {

  // Since we use the ‘smooth’ animation algorithm, we must register it.
  motive::SmoothInit::Register();

  // The engine is the central place for animation data.
  motive::MotiveEngine engine;

  // In this example, we animate a one-dimensional floating point value.
  motive::Motivator1f facing_angle;

  // Initialize facing_angle Motivator to animate as a 'Smooth' Motivator.
  // Alternatively, we could initialize as an 'Overshoot' Motivator. All
  // Motivator types have the same interface. Internally, they are animated
  // with different algorithms, and they will move differently towards their
  // targets. However, to switch between Motivator types it is a simple matter
  // of initializing with a different kind of MotiveInit struct.
  //
  // Angles wrap around with modular arithmetic. That is, -pi is equivalent to
  // pi. Valid range for angles is -pi..pi, inclusive of +pi and exclusive of
  // -pi.
  const motive::SmoothInit init(Range(-kPi, kPi), true);
  facing_angle.Initialize(init, &engine);

  // Set initial state of the Motivator, and the target parameters.
  // 'Smooth' Motivators animate to a target-value in a target-time. Not all
  // types of Motivators use all target data.
  const Angle start = Angle::FromDegrees(120.0f);
  const float start_angular_velocity = 0.0f;
  const Angle target = Angle::FromDegrees(-120.0f);
  const float target_angular_velocity = 0.0f;
  const motive::MotiveTime target_time = 100;
  const motive::MotiveTime delta_time = 1;
  facing_angle.SetTarget(
      motive::CurrentToTarget1f(start.ToRadians(), start_angular_velocity,
                                target.ToRadians(), target_angular_velocity,
                                target_time));

  std::vector<vec2> points(target_time / delta_time + 1);
  for (motive::MotiveTime t = 0; t <= target_time; t += delta_time) {
    // All Motivators created with 'engine' are animated here.
    engine.AdvanceFrame(delta_time);

    // The current value of the variable being animated is always available.
    const Angle angle_at_t = Angle::FromWithinThreePi(facing_angle.Value());
    points.push_back(
        vec2(static_cast<float>(t), angle_at_t.ToDegrees()));
  }

  printf("\n%s", Graph2DPoints(&points[0], points.size()).c_str());
  return 0;
}