Example sin Function Revisited

Regarding the runtime expression, we are looking for a function that will make a nice transition for our goalWeights from 0.0 to 1.0. The sin (sine) function that we used in Example 2 should do just fine, as it will make a smooth oscillation (Figure 19.26).

Figure 19.26 Sine function.

We also need a changing variable to drive the expression; let's use the built-in time attribute for that. Start out with a runtime expression in the Expression Editor, as follows:

  • Runtime
  • a title="Setting goal weight on a per object basis Maya Dynamics" href="/dynamics/setting-goal-weight-on-a-per-object-basis.html">goalWeight[0] = sin(time);

This statement will make our goalWeight values oscillate throughout the animation. To see the numbers in the Script Editor, let's add some print commands to our expression. To do this, add the following statements to the runtime expression:

print ("Goal one = " + goalWeight[0] + "\n");

Now open the Script Editor and play the animation. Figure 19.27 shows the output.

We need the goalWeight values to stay in the positive range, so we will use the abs (absolute value) function again. In this case, the abs function will introduce discontinuities in the sin wave where it would normally cross the 0 value. This will cause the function to no longer be smooth, but later this will work for us in the animation to make it seem like the particle ball is being slowed and caught by the planes. In the Expression Editor, replace the above goalWeight expression with ballShape.goalWeight[0] = abs(sin(time));

and press Enter. The output is shown in Figure 19.28.

Now we need to have another oscillating function that can be offset from the first. We could use sin again and offset it, but let's use the function cos (cosine) instead, which is already 90 degrees out of phase with sin. Figure 19.29 shows how sin and cos relate on a graph.

Figure 19.27 Negative values for goal one.

^Script Editor jag Edit Script Help goal one = 0.823080879

go til one = 0.8460231632

goal one = 0.8674968696

goal one = 0.887464723

goal one = 0.9058920619

goal one = 0.922746899

goal one = 0.9379999768

goal one = 0.SS16248179 Figure 19.28 Positive values for goal one from abs function.

Figure 19.29 Relationship between sine and cosine.

We will simply apply the same function to goalWeight[1]. So now let's open the runtime expression and add the following code segment:

// Runtime goalWeight[0] = abs(sin(time)); goalWeight[1] = abs(cos(time));

print ("goal one = " + goalWeight[0] + "\n"); print ("goal two = " + goalWeight[1] + "\n");

Figure 19.30 shows the results of playing the animation.

Figure 19.30 A single bouncing sphere.
0 0

Post a comment

  • Receive news updates via email from this site