Work with collisions

 
 
 

If you make a particle object collide with an object, you can write an expression to trigger expression statements after the collision. For example, you can change the color or opacity of the colliding particles.

To prepare for writing the expression:

  1. Select the particle shape node of the particles in the Outliner or Hypergraph.
  2. Select Particles > Particle Collision Event Editor from the Dynamics menu bar.

    The Particle Collision Events window appears.

  3. Click Create Event.

    This adds an event attribute to the selected particle shape node. The Expression Editor displays the added event attribute in the Attributes list.

    Close the Particle Collision Events window.

To write the expression:

  1. Select the particle shape node of the colliding particles.
  2. Write the runtime or creation expression using the value of any of these attributes of the colliding particle’s shape node:
    Long name Short name Description Data Type

    event

     

    Contains the number of times each particle in the object has hit something (on a per particle basis).

    float array

    eventCount

    evc

    Total number of events that have occurred for all particles of the object.

    integer

    eventTest

    evt

    True if an event has occurred since the last time an expression or MEL getAttr command read the eventTest value.

    boolean

    The eventCount and eventTest are static attributes. A particle shape node has them as soon as you create the particle object. Though they don’t appear in the Expression Editor, you can use their values in an expression. You must first create the event attribute as described previously.

Example

Suppose you’ve created a five-particle object named Peas that falls with gravity and collides with a plane.

You can make the particles turn red when the first particle hits the plane.

  1. Select Shading > Smooth Shade All.

    This step is necessary to make the correct particle color appear when you later use an expression to color particles.

  2. Select PeasShape in the Outliner or Hypergraph.
  3. From the Dynamics menu bar, select Particles > Particle Collision Event Editor.
  4. In the Particle Collision Events window, click Create Event, then close the window.

    This adds an event attribute to PeasShape.

  5. In the Add Dynamic Attributes section of the Attribute Editor, click Color.

    The Particle Color window appears.

  6. Select Add Per Particle Attribute, then click Add Attribute.

    This adds a per particle attribute named rgbPP. This attribute controls the red, green, and blue color scheme of each particle.

    The particles turn black after you add the rgbPP attribute. Adding the rgbPP attribute turns off the default coloring of the particles and gives them a value of <<0,0,0>>.

  7. With PeasShape selected in the Expression Editor, create this runtime expression (before or after dynamics calculations):
    if (event == 1)
    	rgbPP = <<1,0,0>>;
    else if (event == 2)
    	rgbPP = <<0,1,0>>;
    else if (event >= 3)
    	rgbPP = <<0,0,1>>;
    else rgbPP = <<1,1,1>>;
    
  8. Rewind the animation.

    Upon rewind, the particles are black. The particles have the default black rgbPP color because no creation expression exists for the object.

  9. Play the animation.

    The particles fall toward the plane. The runtime expression executes as each frame plays. The event attribute is a per particle attribute. This isn’t obvious because its name doesn’t have PP as the last two characters.

    Because event holds a running count of collisions for each particle, event contains 0 for each particle until the first collision with the plane. Until the first collision occurs, the final else statement executes:

    else rgbPP = <<1,1,1>>;
    

    This statement executes because event doesn’t equal 1, 2, 3, or a number greater than 3. The vector <<1,1,1>> in the RGB color scheme represents the color white.

    When the first particle of PeasShape hits the plane, Maya sets the event attribute for that particle to 1. This triggers execution of the first assignment, which sets the colliding particle’s rgbPP value to <<1,0,0>>. In the RGB color scheme, this vector value represents red. (When red equals 1, green equals 0, and blue equals 0, the resulting color is red.)

    The value of the event attribute reflects the collision count in the frame after each collision. For example, if a particle collides with the plane in frame 10, event is updated in frame 11.

    When the other particles hit the plane for the first time, they also turn red after they collide.

    A particle stays red until it collides with the plane for the second time, when event equals 2. After a second collision, the particle turns green.

    After a third collision, when event is equal to or greater than 3, a particle turns blue. Each particle stays blue for all subsequent collisions as the animation plays.

  10. Rewind the animation.

    The particles turn black again because they receive the default rgbPP value <<0,0,0>>. When you play the animation again, the particles turn white, red, green, and blue in the same sequence as before.

    You can refine the animation by giving the particles a color other than black for the frame that appears upon rewinding. For example, you can give the particles a white color upon rewinding with two techniques: