Changes

Jump to navigation Jump to search
no edit summary
Project1-A : [[File:Project1-A.mp4]]
 
== More animated objects ==
 
Let's make things more interesting by adding more moving elements, says a lot of particles moving along several circles in a random fashion. If the positions of the animated objects are deterministic, such as a function of time (`{f(time)`) as shown in our first example, there is no difficulty in calculating the positions and drawing the object at these positions, something like:
 
<texcode>
ObjectPosition := f(time) ;
draw Object shifted ObjectPosition ;
</texcode>
But the introduction of randomness will complicate things a bit: the position of a particular particle at a given time (i.e., a given frame) depends on the position at a previous time. There are two solutions:
* Calculate the positions of the particles for frame one, draw the particles, update the positions at the end of the `MPPage`, and continue to the next frame. The drawback is that we cannot generate any frame we need: to build frame 200, we need to draw the 199 previous frames. Advantage: memory is preserved.
* Calculate the positions of all particles for all frames once, in the `MPinclusions` preamble. The advantage is that we can thereafter generate any frame we need, which can be useful for debugging for example (i.e., generate frame 10, 11, 12, or 100, 200, and 300). Drawback: all these positions take up some memory.
 
Let's take the second solution for simplicity. First off, let's define two tiny but useful macros; the first one gives a random random according to a uniform distribution between two limits, and the second one a random color in certain domain:
<texcode>
vardef ranuni(expr mini , maxi ) =
uniformdeviate (maxi - mini) + mini
enddef ;
 
vardef randomcolor =
(ranuni(0.1,0.8),ranuni(0,0.01),ranuni(0.2,0.8))
enddef ;
</texcode>
 
On each circle c ($c=1,\dots,nbcircles$), we will define some objects $o$ ($o=1,\dots,nbObjects[c])$ (called sometimes particles here), so the total number of objects is $O = \sum_{c=1}^{nbcircles} nbObjects[c]$. Each object $o$ on circle $c$ will have at time $t$ the position `Position[c][o][t]`. This position, express as a number in $[0,1]$ is a coordinate along the path `ThePath[c]`. The position at time 1 is chosen randomly along the path $c$ (line 13), and then will be calculated for each time $t$ from the position at time $t-1$ (line 18). Each object will have its own \type{deplacement}, which depends on the length of the circle modified by random factor (line 14). Lastly, to distinguish easily particles in the following figures, we will assign a random color to each one (line 16): `TheColor[c][o]`
 
<texcode>
\startMPinclusions
TotalNbFrames := 300 ;
 
path ThePath[] , TheObject[][] ;
numeric Position[][][] ;
color TheColor[][] ;
nbcircles := 3 ;
for c=1 upto nbcircles :
ThePath[c] := fullcircle scaled (c*20) ;
nbObjects[c] := floor(arclength(ThePath[c])*0.05) ;
for o=1 upto nbObjects[c] :
TheObject[c][o] := fullcircle scaled ranuni(2,7) ;
Position[c][o][1] := uniformdeviate(1) ;
deplacement := (arclength(ThePath[c])/30000)
*ranuni(0.9,1.1);
TheColor[c][o] := randomcolor ;
for t=2 upto TotalNbFrames :
Position[c][o][t] :=
Position[c][o][t-1] + deplacement;
endfor;
endfor;
endfor;
 
\stopMPinclusions
</texcode>
 
Now that all the calculations have been completed, we just need to draw these objects:
 
<texcode>
\startMPpage
currentframe := #1;
for c=1 upto nbcircles :
draw ThePath[c] withcolor blue ;
for o=1 upto nbObjects[c] :
fill TheObject[c][o] shifted
(point Position[c][o][currentframe]
along ThePath[c])
withcolor TheColor[c][o] ;
endfor;
endfor;
setbounds currentpicture to TheFrame ;
 
\stopMPpage
</texcode>
 
Here is the movie :
[[File:Project1-B.mp4]]
 
We can also play with the timing of the animation (code not shown) : [[File:Project1-B-Slow.mp4]]
 
Project2-A: [[File:Project2-A.mp4]]
Project1-C2-1080: [[File:Project1-C2-1080.mp4]]
Project1-B: [[File:Project1-B.mp4]]
Project1-B-Slow : [[File:Project1-B-Slow.mp4]]
Project1-A : [[File:Project1-A.mp4]]
36

edits

Navigation menu