3

What's the math behind something like this? C++ perspective.

enter image description here

More examples on this MSDN page here.

UPDATE: Was asked for a more concrete question. What's the math/animation theory for Penner's tweens^? How do you come up with those formulas? What are the math principles they are based on?

Me and math, we are not BFFs! I'm working on a multi-FLOAT value animator for a UI thing I'm writing and I was wondering what's the math from a native C++ programmer's point of view for generating such a trajectory.

Googled and found code but I'm also looking for a bit of theory from a programming perspective... not just code or pure math. I can whip the code I need together from what I found online but I'd like to understand it in the process. Like this site that allows one to experiment with an easing function generator.

I could also use the Windows Animation Manager (and I might if things get bloody), but that operates on a single float. And just animating RGB requires animating each FLOAT by itself. It leads to huge code-bloat... very bad.

If anyone has some hints, I would very much appreciate it. I'm looking mainly for theory from a programming perspective. The end goal is to write a bunch of different animation algorithms that can animate a set of FLOATs from their initial values to their target values in a period of time or speed and such.

The plan is not just to get the code written, but also to understand what goes on behind it. And then maybe get creative with this animations... unless these prove to be some rigid standard math functions.

MSalters
  • 173,980
  • 10
  • 155
  • 350
CodeAngry
  • 12,760
  • 3
  • 50
  • 57
  • It looks like you want a cubic spline segment. – Vaughn Cato Feb 16 '14 at 18:37
  • More precisely, looks like a Hermite curve (seeing how there's a tangent drawn). – Damon Feb 16 '14 at 20:44
  • I think you need to make your question a bit more concrete. Been thinking about how to word an answer for a while, but it's really a bit broad (someone even voted for close already, so it seems others feel alike). What is the problem you are trying to solve? Most interpolation techniques work for 3-vectors or 4-vectors (like RGBA) the same as for single scalars. Also, there's a considerable number of techniques which can be used in several well-known (and less well-known) ways that can be quite different. For example, you might use a classic Hermite ease in/out function to decide ... – Damon Feb 16 '14 at 21:57
  • ... how much to blend one color into another, or how far to linearly interpolate one position to another. Or, you might use 4 control points to interpolate (in any number of dimensions) between two of the 4 points. On a cubic curve, stunningly the code is exactly the same, regardless of whether it's 1D, 2D, 3D, or nD -- just two matrix/vector multiplies. – Damon Feb 16 '14 at 21:59
  • @Damon I have a basic linear interpolation algorithm done. But I'm trying to add effects to it. Make it non linear, add a bounce at the end. Instead of moving in a direct line, move up a bit and near the end down a bit. Accelerate of decelerate near begin/end. And such... I'm trying to figure out a general way to compose such animations and the math formulas for them. – CodeAngry Feb 16 '14 at 23:01
  • @Damon So the main question would be. How does one come up with the mathematical formulas for this interpolations to make them less... linear (boring). – CodeAngry Feb 16 '14 at 23:08
  • A very easy way of making a linear interpolation "less linear/boring" is to use something like [this](http://www.opengl.org/sdk/docs/man4/html/smoothstep.xhtml). Note the code in the "Description" paragraph. That is a simple 1D Hermite interpolation where all constants (tangents) have been folded already to give an easy equation. How this stuff works is very nicely explained for example in _"Mathematics for 3D Game Programming and Computer Graphics"_ by E. Lengyel. There exist a multitude of web sites too (for free), though it will take some time to wade through that. – Damon Feb 17 '14 at 10:54

2 Answers2

4

So think of the requirements for a tweening function.

  1. The function should represent a valid smooth motion between two positions/states. For those who haven't read the relevant section of the book, this means that f should be a continuous and differentiable function such that f(0) == 0 and f(1) == 1; actual motions are constructed using this as a primitive.
  2. "Ease" (in the animation tweening sense) means "derivative is zero"; this gives the effect that the motion starts and/or ends with zero velocity (i.e. a standstill). So "ease-in" means f'(0) == 0 and "ease-out" means f'(1) == 0.

Everything else is based on aesthetic considerations.

Cubic curves (e.g. Bezier/Hermite splines) are popular partly because they let you control both the position and tangent(speed) at both ends of the curve, but also because they are close to the natural shape that a flexible beam adopts if you constrain its position it at a few points. The cubic shape minimises the internal stress of a flexed beam. (Unsurprisingly, these wooden beams are known to boat designers and other drafters as "splines", for this is where we get the word.)

Historically, hand-drawn cartoon animators have always specified their tweens by feel, based on experience. Key animators draw a chart (called a "timing chart"; look this up on your favourite image search engine) on the side of their key drawings, which tell the inbetweeners how the intermediate cels should be timed.

Camera motion (pan, zoom, rotate), however, were a different matter. Layout/animation artists specified the start and the end of the motion (specified using a field chart), the number of frames over which the motion would happen, instructions on easing and anything else the layout/animation team felt important (e.g. if you had to "linger").

The actual motions needed to be calculated; the audience would notice if one frame of a rotation was out even by a couple of hundredths of a degree. Doing these calculations was part of the job of the camera department.

There's a wonderful book called "Basic Animation Stand Techniques" by Brian Salt which dates from back in the days of physical animation cameras, and describes in some detail the sort of thing they had to do, and to what extent. I recommend it if you're at all interested in this stuff.

Pseudonym
  • 2,571
  • 1
  • 13
  • 19
2

Math is math is math.

A good tutorial on Riemann Sum will demonstrate the concept.

In fundamental programming, you have a math equation that generates a Y value (height) for a given X (time). Periodically, like once a second for example, you plug in a new X (time) value and get the height back.

The more often you evaluate this function, the better the resolution (this is where the diagrams of a Riemann sum and calculus come in). The best you will get is an approximation to the curve that looks like stair steps.

In embedded systems, there is not a lot of resources to evaluate a function like this very frequently. The curve can be approximated using line segments. The more line segments, the better the approximation (improves accuracy). So one method is to break up the curve into line segments. For a given x, use the appropriate linear formula for the line. Evaluation of a line usually takes less resources than evaluating a higher degree equation.

Your curves are usually generated from equations of Physics. So not only do you need to improve on Math, you should also improve on Physics.

Otherwise you can search the web for libraries that handle trajectories.

As we traverse closer to the hardware, a timer can be used to call a method that evaluates the trajectory function for the given X. The timer helps produce a more accurate time value.

Search the web for "curve fit algorithm", "Bresenham algorithm", "graphics collision detection algorithms"

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • 2
    The guy was Bernhard Riemann, spoken like "Rhyman". Might help to have the correct name while looking it up. Other names to drop are Darboux and the Euler method. (The same error occurs frequently with Wiener and Weiner, one is a sausage named after Vienna, the other some disgusting politician.) – Lutz Lehmann Feb 16 '14 at 19:35