6
$\begingroup$

I have a simple animation of a 6-sided die that works as expected except that it runs too slow. I have created 60 frames that I would like for it to show all frames at 30 frames per second. I do not want to create a gif or other static representation as the spin of the die should be random even for the same die face.

dsDice = With[{faces = PolyhedronData["Cube", "Faces"]},
    With[{vertices = faces[[1, faces[[2, 1, #]]]]},
       <|
        "Face" -> {
          Texture[Image@
            Graphics[
             Text[Style[ToString[#], 200, Underlined, FontFamily -> "Verdana"]]]],
          Polygon[vertices,
           VertexTextureCoordinates -> 
            RotateLeft[{{0, 0}, {1, 0}, {1, 1}, {0, 1}},
             Switch[#, 1 | 2, 2, 5, 1, _, 3]]]
          },
        "NormalVector" -> 
         Subtract @@ vertices[[{1, 2}]]\[Cross]Subtract @@ 
           vertices[[{1, 3}]]
        |>
       ] & /@ Range[6]
    ] // Dataset;

roll[] :=
 With[{event = RandomInteger[{1, 6}]},
  {
   DynamicModule[{t = 1, spin = RandomChoice[{-1, 1}, 3]},
    Dynamic[
     t = Max[0, t - 1/60];
     Deploy@
      Graphics3D[{EdgeForm[Gray], 
        Rotate[Normal@dsDice[All, "Face"], t (5 \[Pi])/3, spin]},
       ViewPoint -> Normal@dsDice[event, "NormalVector"],
       Background -> LightGray,
       Lighting -> {{"Ambient", White}},
       Boxed -> False,
       ImageSize -> Medium,
       RotationAction -> "Clip"]
     ]]
   ,
   event
   }
  ]

When roll[] is run the all the frames display but is refreshed too slow. I need a solution that just has the animation without any controls. I tried to return an Animate with AppearanceElements->None but the controls were still visible. Any ideas? Currently on version 11.0.1

$\endgroup$
4
$\begingroup$
  • The less in Dynamic the better, you only really need Rotate parameters there.

  • Also, inexact numbers' calculations are faster.

  • It is a good habit to use Animator for this type of FE related iterations. It is more verbose, gives better control and runs completely in the FrontEnd.

  • Also in defining the faces in dsDice the animation will run faster if you do not convert the Graphics of the numbers into Image. (Edmund update)

roll[] :=  With[{ 
  event =  RandomInteger[{1, 6}]
  }, 
  { 
    DynamicModule[{t = 0,  spin = RandomChoice[{-1, 1}, 3]},
      Column[{
        Animator[Dynamic[t], {0, 1}, AppearanceElements -> None, 
          AnimationRate -> .5, AnimationRepetitions -> 1
        ],
        Deploy @ Graphics3D[{
          EdgeForm[Gray], 
          Rotate[Normal@dsDice[All, "Face"], Dynamic[(1-t) (5. \[Pi])/3.], spin]
          }, 
          ViewPoint -> Normal@dsDice[event, "NormalVector"], 
          Background -> LightGray, 
          Lighting -> {{"Ambient", White}}, 
          Boxed -> False, 
          ImageSize -> Medium, 
          RotationAction -> "Clip"
        ]
      }]
    ], 
    event
  }
]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ Also note that things get even faster if the Texture is left as Graphics and not converted to Image. $\endgroup$ – Edmund Feb 2 '17 at 18:59
  • $\begingroup$ @Edmund makes sense, haven't noticed that, thanks for a tip. Feel free to edit that answer with this fix too. $\endgroup$ – Kuba Feb 2 '17 at 19:00

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.