Conditional statements are statements that only execute if certain conditions are true. These types of expressions take the form of "if" or "if, else", where the expression continues to ask and check on the state of an animation channel or variable, and executes based on the answer of this question.
Lets build a simple conditional statement.
1. Add 3 spheres to the project, each with a radius of 0.5. Put the first sphere at x -2, y 0, z 0; position the second sphere at x 0, y 5, z 0; position the third sphere at x 2, y 0, z 0. Name these spheres Ball_1, Ball_2, and Ball_3 respectively.
2. Add Xpressionist into the project. Go to the Xpressionist Channels tab and load the Position channels for all 3 spheres. Afterwards, type the following expression:
if( Ball_2.Position.Y <= 4 )
Ball_1.Position.Y = abs( Ball_2.Position.Y - 4);
3. Click ok.
4. In one of the world windows, select Ball_2 and move it up and down the Y axis; you will see as this sphere goes lower than 4 units along Y, Ball_1 starts rising along Y.
The first line of the conditional statement asks the question "if" - with the question being, if the Y position of Ball_2 is less than or equal to 4.
The second line of the statement is the answer to this question - if Ball 2's Y position is indeed less than or equal to 4, then this second line is executed; specifically, it takes the absolute value (abs) of the Y position of Ball_2, subtracts 4 from this value (being the starting value of the question). In practical terms, it creates an inverse position of Ball_1 to Ball_2's Y position once Ball_2 goes lower than 4.
If-Else Statements
If you interactively move Ball_2 around along Y, you may notice that if you move it too quickly, Ball_1 might get stuck at a position higher than 0, even though Ball_2 is above 4. What's happening is that because Ball_2 is moving quickly enough, the values jump from a value less than 4 to a value greater than 4 in an instant; with this quick jump, Ball_2 is higher than 4, and so the calculation is no longer being executed. We only told it to do something if the value is 4 or less. This is where an if-else statement comes into play.
1. Edit the previous expression like below:
if( Ball_2.Position.Y <= 4 )
Ball_1.Position.Y = abs( Ball_2.Position.Y - 4 );
else
Ball_1.Position.Y = 0;
Now when you go to fiddle with the Y position of Ball_2, and you go higher than 4 with it, Ball_1 will get set to 0 along Y and won't get stuck in the air. The addition of the else argument makes the if question a multiple choice answer - if the Y position is 4 or less, then the first statement gets executed; otherwise (else), the second statement gets executed.
|