2
$\begingroup$
Vdd = 
  Graphics3D[
    {Blue, Cuboid[{-0.5, -1, 0}, {1, 13, 0.5}],
     Blue, Cuboid[{1, 1.5, 0}, {4, 2.5, 0.5}],
     Blue, Cuboid[{1, 8, 0}, {3, 9, 0.5}],
     Blue, Cuboid[{3, 8, 0}, {4, 11, 0.5}]}];

I was wondering on how I can animate the cuboids so they show up in the graphics viewport every five seconds, one after the other. Then for an interval at end of the animation, all the four cuboids should be present.

Scenario

The 1st cuboid shows up.
After 5 seconds, the 2nd shows up and both the 1st and 2nd cuboid are present.
After 10 seconds, the 1st, 2nd and 3rd cuboids are present.
And and so on.

$\endgroup$
1
  • 2
    $\begingroup$ Closers have marked the original question (with answers!) as a duplicate, whereas the actual duplicate was left open. $\endgroup$ Mar 3 '18 at 22:13
4
$\begingroup$

One way is with Dynamic + Clock.

Set up the data:

cuboids = {Cuboid[{-0.5, -1, 0}, {1, 13, 0.5}], 
  Cuboid[{1, 1.5, 0}, {4, 2.5, 0.5}], Cuboid[{1, 8, 0}, {3, 9, 0.5}],
  Cuboid[{3, 8, 0}, {4, 11, 0.5}]};

bd = RegionBounds[RegionUnion @@ cuboids];

Here I use 0.5 seconds instead of 5 for the sake of the animation.

Dynamic[Graphics3D[
  {EdgeForm[], Blue, cuboids[[1 ;; Ceiling[Clock[4, 2]]]]}, 
  Boxed -> False, 
  PlotRange -> bd
]]

enter image description here

Edit

Here's a similar way with ListAnimate:

ListAnimate[
  Table[
    Graphics3D[
      {EdgeForm[], Blue, cuboids[[1 ;; n]]}, 
      Boxed -> False, 
      PlotRange -> bd
    ], 
    {n, 4}
  ], 
  AnimationRate -> 2
]

enter image description here

$\endgroup$
4
  • $\begingroup$ Thanks you, but is there a way to use the animate command ? Because I would like to pause, rewind, and fast forward. $\endgroup$
    – Brandon M
    Mar 3 '18 at 5:06
  • $\begingroup$ @BrandonM see my edit. $\endgroup$
    – Chip Hurst
    Mar 3 '18 at 5:11
  • $\begingroup$ Thanks You so much, it was very helpful. Another question. What if I want one block to be a different color and the rest to be blue ? $\endgroup$
    – Brandon M
    Mar 3 '18 at 5:22
  • $\begingroup$ @BrandonM Do cuboids = {{color1, cuboid1}, ..., {color4, cuboid4}};, change the region bounds call to RegionUnion @@ cuboids[[All, 2]], and delete Blue from the ListAnimate (though technically this last part isn't necessary). $\endgroup$
    – Chip Hurst
    Mar 4 '18 at 4:53
2
$\begingroup$

You do not need Animate or its friends. Just use Manipulate. Manipulate does what Animate does and more.

 Animate[expression,{i,0,10,1}]

is same as

 Manipulate[expression,{{i,0,"i"},0,10,1}

Using the tick method

Manipulate[
 tick;
 If[run,
  If[n == 4, n = 1, n++];
  tick = Not[tick];
  Pause[speed]
  ];
 Graphics3D[{EdgeForm[], Blue, cuboids[[1 ;; n]]}, Boxed -> False, 
  PlotRange -> bd]
 ,
 {{speed, 0.5, "speed"}, 0.01, 1, 0.01, Appearance -> "Labeled"},
 Button["run/stop", run = Not[run]; tick = Not[tick]],
 {{tick, False}, None},
 {{run, False}, None},
 {{n, 1}, None},
 TrackedSymbols :> {tick, speed, run}
 , Initialization :>
  (
   cuboids = {Cuboid[{-0.5, -1, 0}, {1, 13, 0.5}],
     Cuboid[{1, 1.5, 0}, {4, 2.5, 0.5}],
     Cuboid[{1, 8, 0}, {3, 9, 0.5}],
     Cuboid[{3, 8, 0}, {4, 11, 0.5}]};
   bd = RegionBounds[RegionUnion @@ cuboids]
   )     
 ]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.