26 February 2011

Particle Example 3 : Particle expression

As I described in the earlier post, there are two kinds of expressions which are ordinary and particle. In this post, we will use a particle expression to constrain the particles emitted by an Omni emitter to a surface that we define mathematically.

When to use a particle expression..

  • When you want to apply different forces to each particle,  making it difficult to use field.
  • When you have a well-defined mathematical model for your particles' behaviour. 
  • When you want to apply complex decision making to each particle's behaviour independently of the others in the particle object. 

  1. Create new scene. Select the Dynamic menu set. Go to Particles - Create Emitter and click option box.
  2. Set emitter type : Omni and leave other settings. Click Create. Playback your animation. You will see a cloud of particles being emitted in all directions.
  3. Drag over the particles and press Ctrl+A or open the Attribute Editor.
  4. Scroll down to Per Particle (array) Attributes and open the tab.
  5. Right click on the box of Position and choose Runtime expression After Dynamics. Enter the below expression. Creat.
          vector $pos = position;
          float $posY = ($pos.x) * ($pos.x) + ($pos.z) * ($pos.z);
          position = <<$pos.x, $posY, $pos.z>>;



     6.  Again, right click on the box of Position and open Creation expression.
          Enter the same expression as above. Create.
  • Runtime expressions are executed only on the frames after the first frame that a particle exist. If we didn't put our constraint into the creation expression, particles wouldn't stay on the surface during the first frame that they exist. We want our constraint to apply to all particles, that's why we made same expression as creation expression. I will explain difference between runtime expressions and creation expressions in the next post.
     7.  When you playback the animation, you will see the glitches under the paraboloid
          of particles. To prevent this, you should add some lines in the runtime expression
          and creation expression you have created before.

          vector $vel = velocity;
          velocity = <<$vel.x, 0, $vel.z>>;
 
     8.  Playback the animation and check if there are glitches under the paraboloid.




Analyze Expression


vector $pos = position : Create a vector variable called $pos. Set its value to the current
                                      particle's position.

float $posY = ($pos.x) * ($pos.x) + ($pos.z) * ($pos.z) :
  Create a float type variable called $posY. Its value is calculated by multyplaying the
  position X and Z  components by themselves and adding them.
  It's not possible to get the X and Z value of a particle's position by writing position.x.
  However, it is possible to get the X and Z value of the $pos variable by writing
  $pos.x and $pos.z.

position = <<$pos.x, $posY, $pos.z>> :
  Remember the vector variable has specific way to put values in it using
  " << Val X, Val Y, Val Z >>". Vector variable "position"'s value has decided by components
  $pos.x, $posY  and $pos.z.

 + added lines

vector $vel = velocity : Create a new vector variable called $vel and set its value to the
                                   current particle's velocity.

velocity = <<$vel.x, 0, $vel.z>> : setting the initial velocity in Y to 0 so that the particles
                                                    are not moved away from the surface.


Expressions After Dynamics


Choosing "Expressions After Dynamics" is important for the above expression to work properly.

If it's unchecked
  • expression execute first. When an expression examines the value of the particle's position or velocity, it sees the value those attributes had on the previous frame. Then, after the expression has had its opportunity to change the per-particle attributes, the dynamics engine applies the effect of forces created by fields and goals.
If its checked
  • It makes the dynamics engine run first. Then, when we examine position, we see the position established for the particle by the dynamics engine on this frame, and any changes we make establish the final value for the particle's position. 













Reference
  • Mark R.Wilkins and Chris Kazmier., 2003. MEL Scripting for Maya Animators. SanFrancisco: Diane D. Cerra
  • Marc-André Guindon., 2005. Learning Maya 7 : The Special Effects Handbook. Unknown: Sybex

    No comments:

    Post a Comment