View Daniel Harjanto's profile on LinkedIn

Animating Piano Key with Rigid Body

by Daniel Harjanto a.k.a misterdi

 

As we already have a lot of headache animating the piano player, now there is another challenge, how we animate the piano keys following the player?.

It is possible to do a key-frame for each single touch of the finger, but it is a pain in the arse when they ask to change the piano player animation.

So, we need to find out a system that each key knows when they are pressed by the finger and relax back to the rest position when the finger is lifted.

To make live easier, we could use a rigidbody rig to do the task. Here is the setup and work flow.

1. The setup

To simplify things we just do one piano key setup. (figure.1 setup.jpg). Since dynamics is sensitive to measurement, take a look the setup from top view. (figure 2. topView.jpg)



As you see there are couple things we need to have in the setup.

Geometry:
a. One piano key.
b. A pinned rigidBody (red) position toward the front side of the piano key
c. A spring holder (blue) which also a rigidBody, about 3 unit higher from the pinned rigidBody
d. A dummy (which is a sphere) that will be constraint to a finger of the piano player.

Rigid Body:
Even you can't see it in the picture, there are 4 rigidBody connected to each geometry, which was created using Dynamics>Soft/Rigid Bodies/Create passive Rigid Body menu ( figure.3 RB_creation.jpg).
As a start all rigidBody was created as a passive rigidbody.

Rigid Body Constraint:

  • a hinge constraint for the piano key, place where it should be placed as a pivot point of a piano key ( figure 4. RB_constraint_hinge.jpg, figure 5. orig_hinge_position.jpg and figure 6. final_hinge_position.jpg).





  • a pin constraint, which connect the piano key to the pinned_rigidBody. And position of this pin constraint was move exactly at the centre of mass of the pinned_rigidBody (looak at figure 7. RB_constraint_pin.jpg, figure 8. orig_pin_position.jpg and figure 9. final_pin_position.jpg).







  • c. a spring constraint, which connect the spring_holder to the pinned_rigidBody, with some initial value I set up as figure 9. RB_constraint_spring.jpg).



  •  

     

    2. Why do we setup like this?

    The reason I put a pinned_rigidBody in the setup, not connecting directly the spring_holder to the piano_key is, spring constraint always connected to the center of mass of the rigidbodies (You can set it up differently if you are using the user define position, but that is not intuitive enough). And if the spring connected to the center of mass, you might run into problem of trivial solution of the rigid solver.

    But since rigidbody constraint will always required rigidbodies to be connected, we use pin constraint to do somesort of constraining the pinned_rigidBody to the piano_key. But we move the location of this pin constraint exactly to the center of mass of the pinned_rigidBody so it doesn't have extra transformation when it is animated in dynamics.

     

    3. What's next

    When everything is setup nicely, then it is the time to switch the piano_key and the pinned_rigidBody to active rigid body. Select both object in the Outliner (figure 10.Outliner01.jpg) and change the Active attribute in channel box to on (figure 11. channel_box01.jpg).

    Select pinned_rigidBody and spring_holder and set the collision attribute to off in the channel box (figure 12. channel_box02.jpg). This will ensure that those object will not be calculated for any collision event happening during simulation.

    To test out the rig, select the dummy (sphere) and animate it so it goes into the piano key (do not worry that the piano key is not pressed right now), set several key frame the sphere going up and down.

    Rewind your time line and hit play, you will see now the sphere is pressing the piano keys (piano_RB.mov).

    The scene (Maya 5.0) are included in this tutorial (piano_RB.mb) or in zip file (piano_RB.zip)

     

    4. Beyond the tutorial.

    When you feel the piano key is a bit to flexible, and you want stiffer animation, play with the spring stiffness and spring damping attribute of the spring.
    If you want the piano key down a little at rest position play with the spring rest length attribute.
    You can also have other spring to hold the key from beneath, which will give you more vibrant key movement.

    When you familiar with one setup, you can look at the history part of your script editor and try to create 84 rig with a mel script.
    Instead of using one dummy, create 10 dummy and point constraint this dummy to each finger tips of the piano player and let it run. You will have the piano keys all animated within minutes only.

    The same concept could be applied to different instrumentation like Cool - lean - Tounge or Gah - Mae - Lan.

    But the idea is you concentrate on the animation of the player and let the system do the dirty work for you.

    Next tutorial will be more simple "The domino effect".

    And hopefully I got time to write down the tutorial of "Burning those paper" in time.

    <<back>>