prev index next |
Animation
|
POV-Ray Tips,
Tricks, and Techniques |
Sine Curves
(and Using Them in Animations)?
|
XGamer wonders: | I need a little help with POV-Ray animation math...
Could someone help explain a sine curve to me and how it works? I'm
not sure I quite understand how to make one. If you could post a response,
I think it would be beneficial to all those people out that that get confused
sometimes with POV-Ray animation math... :)
|
|
Chris Colefax responds:
|
The main use of sine curves (and various other functions)
in POV-Ray animation is for creating non-linear motion. We're all
familiar with statements like this:
rotate y*360*clock
used to animate objects. Knowing that the clock variable starts at 0 at the beginning of the animation, and ends at 1, we can see that the first statement rotates the object once around the origin, and the second moves the object from one spot to another. In both cases, the speed of the object as it moves is the same throughout the animation, ie. for every frame (no matter how many frames there are) the object is rotated or translated the same amount (this is linear motion). This is fine, except that in real life objects rarely behave this way. This is due to various forces such as friction and gravity that act on all objects on earth. In space, of course, this shouldn't apply, and we know that an object that starts moving will continue to move at the same speed in a straight line forever (until another force acts upon it). But on Earth the same object will be pulled down to the ground, or slowed down because it is in contact with something else. So, to create realistic animations we need a way of moving the objects so that they speed up, and slow down, and get pulled to the ground, etc. The way to do this is to modify the clock variable using various mathemetical functions. The best way to work out which functions to use is to think about their graphical represention, ie. graphs. Rather than trying to represent these using text, save the following POV-Ray scene and render it: camera {location <.5,0,-3> look_at <.5,0,0>}
Instead of using the clock variable, we have created our own Count variable, which also goes from 0 to 1, but in a single frame. That way, we can see the graph of the function without having to actually render an animation. Rendering the file without making any changes, you will see that we get a straight line (that's why it's called linear!). This line starts at <0,0> and ends at <1,1>. The x-axis (across) represents the progress through the animation (and always goes from 0 to 1) while the y-axis (up) shows the value of the clock variable (or MClock in this case) at that time, eg. at the start of the animation (zero units across) MClock equals zero, while at the end of the animation (one unit across), MClock equals one (we know this already, but now we can actually see it!). Now try replacing the sixth line down with this: #declare MClock = sin(Count * 2 * pi) / 3 When you render this you will see the graph of the sine function: a smooth curve that starts at 0, goes up, goes down, and then finishes at 0. How does this help us with animation? Well, the graph not only shows us what the value of MClock is throughout the animation, but the slope of the graph shows us the relative speed of the object being animated. A flatter slope indicates that the object is moving slower, while a steeper slope indicates that the object is moving faster. A flat line at any point of the graph indicates that the object is not moving at all! i.e. at the highest and lowest points of the sine graph the object would actually stop moving and turn around. Now let's say you wanted to animate a bouncing ball. You could use something like this: #declare MClock = 1 - abs(2*Count - 1) To actually use it you would have something like this: #declare MClock = 1 - abs(2*clock - 1)
Notice that we replace 'Count' with 'clock', and then use the modified clock variable 'MClock' instead of the plain old clock variable. Anyway, when you render the graph of this (replacing the sixth line of the above POV-Ray file with '#declare MClock = 1 - abs(2*Count - 1)') we see that we have a line that goes up (all at the same speed, because the slope of the line doesn't change), and then turns around in an instant and goes down again. Of course, in real life the ball would actually slow down as it reached the top, stop for a split second, and then start falling, getting faster and faster until it hits the ground, then bouncing back up again. If we remember back to our sine graph, we will realise that this is exactly what happened in the first half of the graph! If we now use a line like this: #declare MClock = sin(Count * pi) and render the graph we will see that we have a line that looks much more like the movement of a bouncing ball (it is still not physically accurate, but we'll get to that later). To actually use this in an animation, we would have: #declare MClock = sin(clock * pi)
once again replacing the 'Count' with 'clock', and using 'MClock' where we would have just used 'clock'. Of course, there are many more functions available in POV-Ray (check section 7.1.8.1 in the docs for a full list), and these can be combined in a literally infinte number of ways, eg: #declare MClock = sin(Count*2*pi)/4 + sin(Count*16*pi)/10 Render this to see an interesting variation on the plain old sine curve! Of course, to fully utilise all these function requires some knowledge of their graphs, and the best way to find out is to try plugging in different functions with different values and see what you get. To get you started, here are some basic, and useful, functions you can try: Starts out slow, and speeds up (eg. accellerating car):
Starts out fast and slows down (eg. explosion):
Physically correct bouncing ball:
Starts out slow, speeds up, and slows down again (many uses):
Starts out slow, speeds up, then snaps back (eg. recoil):
Starts out fast, slows down, then snaps back (eg. rubber band):
You can just copy these lines into the above POV-Ray file (replacing
line 4) to see the graph of the function. Once again, to actually
use them in an animation change the 'Count' to 'clock', and then use 'MClock'
in place of the 'clock' variable. This can be in rotations, scalings,
translations, turbulence, color_maps, whatever it is you want to animate.
|
|