Docstoc

Animation - Download as PowerPoint

Document Sample
Animation - Download as PowerPoint Powered By Docstoc
					EIE360 Integrated Project




                                   Lecture 5


                                   Animation

  References:
  [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006
  [2] Ogre Tutorials – Ogre Wiki
      http://www.ogre3d.org/wiki/index.php/Ogre_Tutorials
  [3] Microsoft MSDN, C++ reference
   Department of                                              1
   ELECTRONIC AND INFORMATION ENGINEERING
   5. Animation by Dr Daniel Lun
Architecture of the 3D
Shooting Gaming System

                                            Joystick
                                          port via USB
                                                            Your
                                                           program
                                            USB port
                                          (For receiving
                                              video)



                                            3D Graphics
                                              System

Video + 3D Graphics send to HMD
 Department of                                                       2
 ELECTRONIC AND INFORMATION ENGINEERING
 5. Animation by Dr Daniel Lun
Animation in OGRE
   Animation, in general, is no different in the computer age
    than it was when some artists first flipped quickly through
    pages containing a series of slightly different images
   In Ogre, the scene is drawn from scratch every frame,
    whether or not it contains animation
   Ogre does not keep track of game characters’ velocity
    and acceleration vectors
   The animation features in Ogre are there only to help
    position and orient your characters as a function of some
    arbitrary variable (usually time, although any variable
    can act as a controller)


     Department of                                                3
     ELECTRONIC AND INFORMATION ENGINEERING
     5. Animation by Dr Daniel Lun
Animation in OGRE (cont)
   An animation in Ogre is a collection of possibly (usually) related
    tracks
   An animation track is a set of data values stored as a function of
    time
   The pair of time point and track value composes a keyframe
     The term keyframe comes from the days of hand-drawn
        animation when master artists would provide the junior artists
        with a set of “key” frames in an animation
   Ogre works just like the junior artists to interpolate from your (master
    artist) keyframes designed using different 3D modeling tools (e.g.
    3ds Max) to render the needed animation
   Refer to the course notes of SD3983 for the details of animation in
    Ogre

    Department of                                                         4
    ELECTRONIC AND INFORMATION ENGINEERING
    5. Animation by Dr Daniel Lun
A Simple Example
   Let’s use a simple example, “a walking robot”, to
    demonstrate the basic idea of creating animation in Ogre
   robot is a skeletally animated mesh object comes with
    Ogre
   It has 5 skeletal animations – Die, Idle, Shoot, Slump,
    and Walk
       They define the keyframes in these animations
       May preview them using OgreMax Win Viewer
   All animations are “in-place”, no translation motion
       To animate a walking robot, need to move the scene node to
        make it look like moving

    Department of                                                    5
    ELECTRONIC AND INFORMATION ENGINEERING
    5. Animation by Dr Daniel Lun
 A Simple Example

                                     An Entity based on robot.mesh
                                     is attached to a sceneNode

    The sky doom

The terrain




     Department of
     ELECTRONIC AND INFORMATION ENGINEERING
     5. Animation by Dr Daniel Lun
                                                        Detected by the
Step-by-step …                                          frameListener of
                                                        Ogre

 Initialization                                Update
 EIE360ProjectApp( … )                         screen
 createScene( … )

 0. Define member variables               Screen Update
 1. Create entity and                     frameRenderingQueued( … )
    scene node
 2a. Define animation state               2b. Update animation state
                                          3. Update walking parameters
                                          4. Translate the scene nodes
                                          5. Start ray trace query
                                              (Walk on the ground)


                                               Finish
                                               update
                                               screen
 Department of
 ELECTRONIC AND INFORMATION ENGINEERING
 5. Animation by Dr Daniel Lun
First step: Create Entity and
SceneNode
   Assume the following two variables are
    defined: Entity * mCharacterEntity;
             SceneNode * mCharacterNode
   Then
     mCharacterEntity = mSceneMgr->createEntity("Character",
       "robot.mesh");
     mCharacterNode = mSceneMgr->getRootSceneNode()
       ->createChildSceneNode("CharacterNode");
     mCharacterNode->attachObject(mCharacterEntity);
     mCharacterNode->setPosition(Vector3(1450, 195, 1300));
            //Set the initial position of the robot
     mCharacterNode->yaw(Degree(270));
            //Let it face to you

    Department of                                          8
    ELECTRONIC AND INFORMATION ENGINEERING
    5. Animation by Dr Daniel Lun
Step-by-step …

 Initialization
                                                 Update
                                                 screen
 0. Define member variables
 1. Create entity and
    scene node
                                          Screen Update
 2a. Define animation state
                                          2b. Update the animation state
                                          3. Update the walking parameters
                                          4. Translate the scene nodes
                                          5. Start ray trace query
                                              (Walk on the ground)


                                                 Finish
                                                 update
                                                 screen
 Department of
 ELECTRONIC AND INFORMATION ENGINEERING
 5. Animation by Dr Daniel Lun
Second Step: Get and Set
Animation State
   As part of the export process, different parts of the
    timeline in an object’s animation can be given names –
    Die, Walk, … One can use these names to “address”
    different animations on an entity
   For each animation, we can obtain or change its current
    state by calling function getAnimationState()
   It returns an animation state that provides access to
    different properties of an animation:
        length – obtain the duration of the animation
        time position – get/set the position of an object at a particular time
        loop – define if the animation should be played once or loop
        enable – define if the animation can be played
        weight – define the weighting when blending with other animations
        Department of                                                        10
        ELECTRONIC AND INFORMATION ENGINEERING
        5. Animation by Dr Daniel Lun
Second Step: Get and Set
Animation State (cont)
   Assume the following variable is defined:
                        AnimationState * mAnimationState;

   Then
          mAnimationState = mCharacterEntity->
                      getAnimationState(“Walk");
                // Get the animation state of “Walk”
          mAnimationState->setLoop(true);
                // Play “Walk” in a loop
          mAnimationState->setEnabled(true);
                // The animation can be played
                // “Walk” can then be played forever until
                // you disable it or set loop to false
    Department of                                           11
    ELECTRONIC AND INFORMATION ENGINEERING
    5. Animation by Dr Daniel Lun
 Second Step: Get and Set
 Animation State (cont)
    For Ogre, the scene will be updated in an irregular period
    To synchronize the animation with that update rate, we may use the
     elapse time between frames to update the time position parameter of
     the animation state
       mAnimationState->addTime(evt.timeSinceLastFrame);


Show time of          Show time of        Show time of
  Frame n        t1    Frame n+1     t2    Frame n+2       Since there may
                                                           not be a
                                                           keyframe at that
                                                           time, Ogre will
               addtime               addtime               interpolate one
               (t1)                  (t2)                  based on the
                                                           available
                                                         t keyframes
     Time positions defined by the designer                             12
Step-by-step …

 Initialization
                                                 Update
                                                 screen
 0. Define member variables
 1. Create entity and
    scene node
                                          Screen Update
 2a. Define animation state
                                          2b. Update the animation state
                                          3. Update the walking parameters
                                          4. Translate the scene nodes
                                          5. Start ray trace query
                                              (Walk on the ground)


                                                 Finish
                                                 update
                                                 screen
 Department of
 ELECTRONIC AND INFORMATION ENGINEERING
 5. Animation by Dr Daniel Lun
Third Step: Update the Walking
Parameters
   Our target is to allow the robot to walk to different
    random locations in the game world
   So we need to update the destination where the robot
    should walk to
   The distance, direction and speed should also be
    defined and updated in real time
   Assume the following parameters have been defined
                        Real mWalkSpeed;
                        Real mDistance;
                        Vector3 mDirection;
                        Vector3 mDestination;
    Department of                                           14
    ELECTRONIC AND INFORMATION ENGINEERING
    5. Animation by Dr Daniel Lun
Third Step: Update the Walking
Parameters (cont)
   A random destination can be determined by adding a random vector
    to the original position of the robot

mDestination = Ogre::Vector3(Math::RangeRandom(-200,200),
 0, Math::RangeRandom(-200,200)) + Vector3(1450, 195, 1300);
                           //An arbitrary reference point

   Math::RangeRandom(-200,200) will return a random real no.
    between -200 and 200
   The direction of the robot can be found by
mDirection = mDestination - mCharacterNode->getPosition();

   mDirection will keep a vector that shows the difference in
    position between the current position of the robot and the destination
     Department of                                                      15
     ELECTRONIC AND INFORMATION ENGINEERING
     5. Animation by Dr Daniel Lun
 Third Step: Update the Walking
 Parameters (cont)
   When the direction is found, we need to rotate the robot to ensure it is
    facing to the destination
   First, we need to find out the current direction the robot is facing such
    that we can rotate it if it is not right
Ogre::Quaternion faceQuat = mCharacterNode->getOrientation();
Vector3 faceVec = faceQuat * Vector3::UNIT_X;

   mCharacterNode is a pointer to the SceneNode the character is
    attaching to
   faceQuat * Vector3::UNIT_X multiplies the orientation
    Quaternion of the character with a unit vector (1,0,0), which is direction
    the robot is facing originally
   Resulting in a vector that gives the direction the robot is facing currently
     Department of                                                          16
     ELECTRONIC AND INFORMATION ENGINEERING
     5. Animation by Dr Daniel Lun
Quaternion
   By definition, a 
                        Quaternion  is the geometrical quotient of two
                                       Q
    vectors A and B , i.e.            A
                                 Q                                           Q
                                     B                                B
   Hence vector A can be obtained from vector B
                                                                                
    by applying a geometrical multiplication               A  QB                A
                                                       
    (NOT dot product or cross product) on B
   A Quaternion uses 4 parameters (w, x, y, z) to
                                                                           (i, j, k = 0,1,0)
    achieve the conversion
                                                                         
      (x, y, z) defines the axis on which the rotation is carried out.  B
        They are usually the normalized values of the actual axis (i, j, k)   
       w = cos(/2) defines the rotation angle  along axis (x,y,z)               
                                                                                   A
       Usually the normalized “unit Quaternion” is used,

    Department of                                              Q  cos / 2,0, sqrt (0.5),0
                                                                                          17
    ELECTRONIC AND INFORMATION ENGINEERING
    5. Animation by Dr Daniel Lun
Quaternion (cont)
   For unit Quaternion, the parameters (x, y, z) need to be
    normalized such that (w2 + x2 + y2 + z2) = 1
   One of the approaches to do so
                                   i 2 * 1  w2  
                                   i  j2  k 2 
               x  sgn(i ) * sqrt  2               
                                                   
                                    j 2 * 1  w2  
                                    i  j2  k 2 
               y  sgn( j ) * sqrt  2               
                                                    
                                   k 2 * 1  w2  
                                   i  j2  k 2 
               z  sgn(k ) * sqrt  2               
                                                   
    where sgn(.) is the sign of (.)
     Department of                                             18
     ELECTRONIC AND INFORMATION ENGINEERING
     5. Animation by Dr Daniel Lun
Versor – A Special Quaternion
   By definition, a Quaternion Q is the geometrical quotient of two
                                
    vectors A and B , i.e.        A
                            Q 
                                 B                        
   There is no requirement to the magnitude of A and B . It means that
    the vector length can be increased or decreased after the
                                        
    geometrical multiplication A  QB
                                       
   In case that the length of A and B is the same, it is a special kind of
    quaternion called versor
   Versor is of particular interest in computer graphics since it refers to
    vector rotations which are generally performed in the field of study




     Department of                                                         19
     ELECTRONIC AND INFORMATION ENGINEERING
     5. Animation by Dr Daniel Lun
More Versor
   By definition, a versor refers to the rotation by an
    angle  on a plane along an axis defined by x, y, and z,
    which is normal to the plane
       Q  w  cos / 2, x, y, z    w2  x 2  y 2  z 2  1              


   Hence for our example, when Q is multiplied
                                         
    to B , it will rotate by  and become A
                                                                   
                                                                   B
                                                                       
                                                                           
                                                                           A

    Department of                                                                  20
    ELECTRONIC AND INFORMATION ENGINEERING
    5. Animation by Dr Daniel Lun
  Quaternion (cont)
    Quaternion is used in Ogre to describe vector rotation
    Can be used to define the orientation of the robot
Ogre::Quaternion faceQuat = mCharacterNode->getOrientation();
// returns a quaternion describing how much of a rotational
// offset the character has acquired from the direction it
// started facing. For robot.mesh, it is UNIT_X
Vector3 faceVec = faceQuat * Vector3::UNIT_X;
             // means geometrical multiplication
            y                            axis of rotation (0,1,0)
                                                                    angle of rotation
                               yaw(Degree(270))                     270o
                          x
            z

Originally the robot is at (0,0,0) and   faceQuat = (cos(270o/2),0,1/2,0)
facing Unit_X, i.e. (1,0,0)              faceVec = (0,0,1) =           21
                                              faceQuat * Unit_X
Quaternion (cont)
    Alternatively, if we have the direction to be rotated, we can get the
     required quaternion to facilitate the rotation
    Vector3 src(Vector3::UNIT_X); //src is set to (1, 0, 0)
    Ogre::Quaternion quat = src.getRotationTo(Vector3(0,1,0)) ;
           //Get the quaternion for rotating src to (0, 1, 0)
    mCharacterNode->rotate(quat);
                   y
                                                                            
                                                                            A      = 90o

                                     x
               z                                                                     
                                                                                     B

                                                                     (i ,j, k = 0,0,1)
Originally the robot is at (0,0,0) and
                                               quat = (cos(90o/2), 0, 0, 1/2)
facing Unit_X, i.e. (1,0,0)
                                               face Vector = (0, 1, 0)
      Department of                                                                22
      ELECTRONIC AND INFORMATION ENGINEERING
      5. Animation by Dr Daniel Lun
 Third Step: Update the Walking
 Parameters (cont)
    Recall faceVec is the vector the robot is currently facing
Ogre::Quaternion faceQuat = mCharacterNode->getOrientation();
Vector3 faceVec = faceQuat * Vector3::UNIT_X;

    And the parameter mDirection keeps a vector that shows the
     direction the robot should go
    So we can rotate the robot to make it facing its destination
Ogre::Quaternion quat = faceVec.getRotationTo
                         (mDirection * Vector3(1, 0, 1));
                         //Only x and z axes data are useful
                         //y is masked to 0
mCharacterNode->rotate(quat);
    Using Quaternion saves our effort in trigonometric calculation

      Department of                                                   23
      ELECTRONIC AND INFORMATION ENGINEERING
      5. Animation by Dr Daniel Lun
Third Step: Precaution
   A “divide by zero” error will result if we rotate the robot
    by 180o
   Should detect this situation and use yaw() instead
if ((1.0f + faceVec.dotProduct(mDirection)) < 0.0001f)
   mCharacterNode->yaw(Degree(180));
       //If the dot product of the current face vector and
       //   the desired direction is -1, yaw the robot 180o
else
{
   Ogre::Quaternion quat = faceVec.getRotationTo
                      (mDirection * Vector3(1, 0, 1));
   mCharacterNode->rotate(quat);
}
     Department of                                                24
     ELECTRONIC AND INFORMATION ENGINEERING
     5. Animation by Dr Daniel Lun
Step-by-step …

 Initialization
                                                 Update
                                                 screen
 0. Define member variables
 1. Create entity and
    scene node
                                          Screen Update
 2a. Define animation state
                                          2b. Update the animation state
                                          3. Update the walking parameters
                                          4. Translate the scene nodes
                                          5. Start ray trace query
                                              (Walk on the ground)


                                                 Finish
                                                 update
                                                 screen
 Department of
 ELECTRONIC AND INFORMATION ENGINEERING
 5. Animation by Dr Daniel Lun
Fourth Step: Let the Robot
Translate
   The Walk animation is in-place, we need to translate the
    scene node the robot attached to make it move
   First estimate the distance between the destination and
    the current position
mDistance = mDirection.normalise();
//compute the norm from the vector mDirection
//At the same time, mDirection is converted to a unit vector

   Then use a parameter                      mDirection
    mWalkSpeed (assume it has
    been declared) to record the
    desired speed of movement,
                                                             mDistance
    e.g. mWalkSpeed = 3.5f;
     Department of
     ELECTRONIC AND INFORMATION ENGINEERING
                                              mDestination               26

     5. Animation by Dr Daniel Lun
Fourth Step: Let the Robot
Translate (cont)
   Assume the robot is translated every time the scene is
    updated
   The walking speed needs to be synchronized with the
    screen update rate
       We don’t want the robot to walk fast in a fast computer and slow
        in a slow computer
Real move = mWalkSpeed * evt.timeSinceLastFrame;
//The actual amount of movement depends on the elapse time
//   from the last frame. Hence no matter how fast the
//   screen is updated, the amount of movement will just
//   be the same
mDistance -= move;
//The remaining distance is reduced after each move
    Department of                                                          27
    ELECTRONIC AND INFORMATION ENGINEERING
    5. Animation by Dr Daniel Lun
Fourth Step: Let the Robot
Translate (cont)
   So if the remaining distance is smaller than 0,
    the animation stops
if (mDistance <= 0.0f)
{
       mCharacterNode->setPosition(mDestination);
       mDirection = Vector3::ZERO;
       // Vector3::ZERO is an indicator chosen to signal
       // the end of the animation
}
else mCharacterNode->translate(mDirection * move);
       //If not, translate “move” amount in “mDirection”




    Department of                                          28
    ELECTRONIC AND INFORMATION ENGINEERING
    5. Animation by Dr Daniel Lun
Step-by-step …

 Initialization
                                                 Update
                                                 screen
 0. Define member variables
 1. Create entity and
    scene node
                                          Screen Update
 2a. Define animation state
                                          2b. Update the animation state
                                          3. Update the walking parameters
                                          4. Translate the scene nodes
                                          5. Start ray trace query
                                              (Walk on the ground)


                                                 Finish
                                                 update
                                                 screen
 Department of
 ELECTRONIC AND INFORMATION ENGINEERING
 5. Animation by Dr Daniel Lun
Fifth Step: Walk on the Ground
   By default, the robot may not walk on the ground
       It may walk in the air or into the ground!
   The RaySceneQuery technique can be used to make
    sure the robot is walking on the ground
   First, create a Ray object
Vector3 characterPos =                               Walk in the air!
   mCharacterNode->getPosition();
Ray ray(Vector3(characterPos.x,
   100000,characterPos.z),
   Vector3::NEGATIVE_UNIT_Y);

• The ray starts way up in the sky
• It not starts at the position of the object
  to ensure the ray will touch the terrain                     30

  even if the robot is under the terrain
Fifth Step: Walk on the Ground
(cont)
   Create a RaySceneQuery object and execute it
   Ogre will try to see if the ray will intersect with any
    movable objects or hit any world fragments, such as the
    terrain
   A vector of results will be returned
RaySceneQuery *raySceneQuery = mSceneMgr
                                  ->createRayQuery(ray);
RaySceneQueryResult & qryResult = raySceneQuery->execute();
RaySceneQueryResult::iterator i = qryResult.begin();

   In our simple example, the vector contains only one item,
    i.e. the world fragment “terrain”
    Department of                                             31
    ELECTRONIC AND INFORMATION ENGINEERING
    5. Animation by Dr Daniel Lun
Fifth Step: Walk on the Ground
(cont)
   Since in this example the vector has only one element,
    we can directly use the iterator i to find out the
    intersection point of the ray to the terrain
   Now the robot is made walking on the ground
if (i != qryResult.end() && i->worldFragment)
       //To make sure that there is something in the vector
       // and that element is a world fragment
{   mCharacterNode->setPosition(
       characterPos.x,
       i->worldFragment->singleIntersection.y,
       //Set the height on the object to the
       //intersection point between the ray and the terrain
       characterPos.z);
}
    Department of                                            32
    ELECTRONIC AND INFORMATION ENGINEERING
    5. Animation by Dr Daniel Lun
RaySceneQueryResult
   The execute() function of RaySceneQuery returns a
    reference to a vector of structs, each with three variables
       distance: tells you how far away the object is along the ray
       One of the other two variables will be non-null
         movable: a pointer to a MovableObject object if the ray hits a
           movable object (like your robot)
         worldFragment: a pointer to a WorldFragment object if the ray
           hits a world fragment (like the terrain)
   The information of the objects that have been hit by the
    ray will be kept in the vector one by one
       One object will have only one entry no matter how many times
        the ray hits it
     Department of                                                     33
     ELECTRONIC AND INFORMATION ENGINEERING
     5. Animation by Dr Daniel Lun
 RaySceneQueryResult::iterator
                   d    d    …   d    d
                   *m   *m   …   *m   *m

                   *w   *w   …   *w   *w

        i = qryResult.begin()         qryResult.end()
while (i != qryResult.end())
{
       if (i->worldFragment)
       {    mCharacterNode->setPosition(
            characterPos.x,
            i->worldFragment->singleIntersection.y,
            characterPos.z);
            break;
       }
       i++;                                             34
}
See the result




  Department of                            35
  ELECTRONIC AND INFORMATION ENGINEERING
  5. Animation by Dr Daniel Lun

				
DOCUMENT INFO