It is time to write the expression.
There are three sections to the opening and closing of the claw. One is high up when the claw is always closed. The second is when it is approaching the ball which is when it will open up. And the third is when it is closing again. (I didn't worry about what happens when I goes down past the ball. Just don't make the claw go down to far in an animation.) Here is the three sections in equation form. (X - Y) = Delta of X and Y <- Brief Math Catch-up...This is just the distance between the two objects
ConPos.z - BallPos.z > 21 (When the claw is always closed)
ConPos.z - BallPos.z > 14 & ConPos.z-BallPos.z<21 (When the claw will open)
ConPos.z - BallPos.z > 7 & ConPos.z-BallPos.z<14 (When the claw will close again)
(The .z denotes the z-axis)
Here it is in English
When the Claw is 21 units away or more it is closed.
From 21 units away to 14 units away the claw will open.
From 14 units away to 7 units away the claw will close.
Now for the angles at >21 the angle will aways be 0 degrees
from 21 to 14 the angle will be changing from 0 to -35 degrees
from 14 to 7 the angle will be changing from -35 back to 0 degrees
Since we just can't type in "open claw in a smooth linear fashion" we have to write an equation for that too (Hmmmm Fun). There is a variety of ways to do this. I did it based on the distance between the two objects.
Ok when the claw is at 21 units away I want it to be at 0 degrees (closed) and when it is at 14 units I want it to be at -35 degrees (open). So what value would be proportional that would make 0 to 7 be 0 to 35. (Answer is 5) so lets look at the sections again with the claws opening/closing included.
"ConPos.z - BallPos.z > 21" (nothing amazing about this)
"ConPos.z - BallPos.z > 14 & ConPos.z - BallPos.z < 21 , (ConPos.z-BallPos.z-21)*5" (uh-oh this is a little complicated)
Lets break it down...the first part is defining the second section (the & is the syntax for "and") and the second part is the conversion from 0 - 7 to 0 - -35. (ConPos.z - BallPos.z) = the distance inbetween, now you need to subtract 21 because that is the distance from the Shaft to the Ball.
For example: the shaft is 48in up and the ball is 27in up at the start of the "opening section". Now without the "-21" the equation would be "(48-27) * 5= 105 degrees". And you wanted 35 degrees. Now with the "-21" it will give you 0. Using the same example, at the end of the "opening section" the equation will be "(41-27) *5 = 70". And again with the "-21" it will be -35 degrees, which is what we want.
"ConPos.z - BallPos.z >7 & ConPos.z-BallPos.z<14 , (7-(ConPos.z-BallPos.z)*5
this is the same exact thing as the 2nd example but instead of "...-21" it is "7-...." for the same reasons. Work it out. =)
Now we have all the equations down we can FINALLY write the script. Which is basically already written above. To split the expression up in to sections we need to make a case for each section with "if" statements. The "if" function follows this syntax "if(a,b,c)" which means in human terms "If a is true, then b, if a is false then c". It HAS to have all three parts or you will get a parsing error. So here is the expression: (cheer)
if(ConPos.z-BallPos.z>21 , Closed ,
if(ConPos.z-BallPos.z>14 & ConPos.z-BallPos.z<21 , degToRad((ConPos.z-BallPos.z-21)*5),
if(ConPos.z-BallPos.z>7 & ConPos.z-BallPos.z<14 , degToRad((7-(ConPos.z-BallPos.z))*5),
Closed)
)
)
If it is the first section (above 21 units) then the claw is closed, (remember the closed variable you made, here it is in action) but if the claw isn't abovet 21 units then.... if it is above 14 units and below 21 units then open the claw from 0 to -35 degrees if it isn't in this "section" then if the claw is below 14 units and above 7 units then close from -35 to 0 degrees, and if the claw is below 7 units (and clipping with the ball) keep the claw closed anyway.
The only thing I didn't cover is the degToRad() function....since the Euler XYZ controller works in radians and humans work in degrees this is a handy conversion function that has been built in. Now Finger 2 should work properly. All that is left to do is to apply the same expression to the rest of the fingers. First apply expression controllers to the rest of the fingers. Note that two of the fingers rotate around the y-axis and two around the x-axis so apply the expression controller to the proper one. Finger 2 and Finger 3 should use the y-axis and Finger 1 and Finger 4 should use the x-axis.
Now close the window and move the shaft up and down. The claw should open/close around the ball.
|