Animation

Animation is performed only on the GPU, entities are not truly moved, vertices and bounding box don't change, collision detection doesn't work and so on. To edit entities position you can apply a transformation, but the computation is heavier and the animation slower. You can see the difference between these approaches in the Assembly sample.

The steps to animate a group of entities are the following, you can see the code implementation in MachineSimulator or PistonAnimation samples:

  1. Create groups of moving objects (using Block class and adding entities to the Block.Entities collection)
  2. Add blocks created at point 1) to the Model.Blocks collection
  3. Subclass the BlockReference class for each moving group of objects
  4. Override the BlockReference.Animate() method of the class at point 3) and add the code to compute the data for entities transformation at that specific time frame
  5. Override the BlockReference.MoveTo() method of the class at point 3) and add the code to move the objects on the GPU
  6. Override the BlockReference.IsInFrustum() method of the class at point 3) and call the base with the "center" transformed as the objects in point 5) to avoid undesired clipping. It might be necessary to override also the bounding box scene extents
  7. Add the block references created at point 3) to the Model.Entities collection
  8. Call StartAnimation() providing the time interval between each frame
  9. Call StopAnimation() to stop the animation

 

Timer

The timer used to perform animation is a System.Threading.Timer since the System.Forms.Timer doesn't fire when the UI is busy. This means that code in Animate() is executed in a background thread while the MoveTo() is executed on the main thread.

During an animation, if you need to edit the entities or blocks collections or if you notice some flickering, you can marshal the Animate() call on the main thread.

class MyModel : Model
{
    protected override void OnAnimationTimerTick(object stateInfo)
    {
        BeginInvoke(new Action(() => base.OnAnimationTimerTick(stateInfo))); 
    }
}
Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

0 comments

Please sign in to leave a comment.