Animating 3d models in DarkGDK using dbAppendObject

Been watching 3d Animation tutorials lately but I’ve never really found a tutorial or detailed example for that matter that actually teaches how to animate 3d models in DarkGDK in a less unclear way. And so I’m sharing this example in hope that it will be used by some computer enthusiast out there like myself as a starting point in creating their very own 3d computer game.

I have used the Babe model in this example. Babe model is a Dark Matter Model that is accesible by default when you install DarkGDK and is located at C:\Program Files\The Game Creators\Dark GDK\Media\Dark Matter Models\People. The controls and actions of our model in this example are illustrated in the following table.

ControlsActions
W,A,S,DMove.x
TabDie.x
QImpact.x
No key PressedIdle.x


Enough said, here’s the code:


  1. // Dark GDK - The Game Creators - www.thegamecreators.com  
  2. // the wizard has created a very simple project that uses Dark GDK  
  3. // it contains the basic code for a GDK application  
  4. // whenever using Dark GDK you must ensure you include the header file  
  5. #include "DarkGDK.h"  
  6. // the main entry point for the application is this function  
  7. void DarkGDK ( void )  
  8. {  
  9. // turn on sync rate and set maximum rate to 30 fps  
  10.  dbSyncOn();  
  11.  dbSyncRate(30);  
  12.   
  13.  //loads our 3d model  
  14.  //we'll be borrowing the Babe DirectX Darkmatter model  
  15.  //it's included in the DarkGdk package located in  
  16.  //C:\Program Files\The Game Creators\Dark GDK\Media\Dark Matter Models\People\Babe  
  17.  //Just copy the contents of the Babe folder to your game project folder  
  18.  dbLoadObject("H-Babe-Idle.x",2);  
  19.  //Idle.x has 25 frames  
  20.  //to find how how many frames there is in a .x animation  
  21.  //Click Start>All Programs>The Game Creators>DarkGDK>Documentation>Information>Type "Frame"   
  22.  //in the Textbox and the command used to determine the number of frames will reveal itself to you.  
  23.  //appends the number of frames of the Move.x animation to the loaded model  
  24.  //Move.x has 25 frames  
  25.  dbAppendObject("H-Babe-Move.x",2,26);  
  26.  //Attack.x has 24 frames  
  27.  //adds the number of frames of Attack.x to the loaded model  
  28.  dbAppendObject("H-Babe-Attack1.x",2,51);  
  29.  //Die.x has 50 frames  
  30.  //adds the number of frames of Die.x to the loaded model  
  31.  dbAppendObject("H-Babe-Die.x",2,76);  
  32.  //Impact.x has 10 frames  
  33.  //adds the number of frames of Impact.x to the loaded model  
  34.  dbAppendObject("H-Babe-Impact.x",2,126);  
  35.  //positions the object in the -y axis cause its a little bit top aligned by default when loaded  
  36.  dbPositionObject(2,0,-1,0);  
  37.   
  38.   
  39.  while (LoopGDK())  
  40.  {  
  41.  //display descriptive text  
  42.  dbSetTextSize(14);  
  43.  dbText(10,0,"WASD-Movement");  
  44.  dbText(10,10,"Spacebar-Attack.x");  
  45.  dbText(10,20,"Tab-Die.x");  
  46.  dbText(10,30,"Q-Impact.x");  
  47.  dbText(10,40,"No key pressed-Idle.x");  
  48.     
  49.   
  50.    
  51.    //if the W or A or S or D or Spacebar or Tab or Q is pressed  
  52.    if ( dbKeyState(17) || dbKeyState(31) ||dbKeyState(32) || dbKeyState(30)||dbKeyState(57) || dbKeyState(15)|| dbKeyState(16))  
  53.    {  
  54.    //stop the presently looping animation  
  55.   dbStopObject(2);  
  56.    //retrieves the object present angle  
  57.    int ObjAngleY=dbObjectAngleY(2);  
  58.    //determines if the object is presently looping  
  59.    int looping=dbObjectLooping(2);  
  60.       //if the key pressed is W  
  61.    if(dbKeyState(17))   
  62.    {   
  63.    //move the object on the -Z axis  
  64.      dbMoveObject(2,-0.05f);  
  65.    //is the object presently not playing?  
  66.    if (looping==0 )  
  67.    {  
  68.    //if yes then execute move.x  
  69.       //move Move.x has 25 frames  
  70.    //the number of frames from the starting frame to the ending frame must be at least 25 frames  
  71.   
  72.     dbLoopObject ( 2,26, 51 );  
  73.    };  
  74.    }  
  75.    //if the key pressed is S  
  76.    if (dbKeyState(31))  
  77.    {  
  78.    //move the object on the Z axis  
  79.     dbMoveObject(2,0.05f);  
  80.    //is the object currently not playing?  
  81.    if (looping==0 )  
  82.    {  
  83.       //if yes then execute move.x  
  84.       //move directx animation has 25 frames  
  85.    //starting frame up to the end frame must be at least 25 frames  
  86.   
  87.     dbLoopObject ( 2,26, 51 );  
  88.   
  89.    };  
  90.    }  
  91.    //if the A key is pressed  
  92.   
  93.    if (dbKeyState(30))  
  94.    {  
  95.   //rotate the object along the Y axis  
  96.   //for instance, if the objects current angle is 275-5  
  97.   //The object will then be rotated 270 degrees which is exactly south  
  98.   //from the user's POV  
  99.   //the dbWrapValue is to ensure that the object rotation will not exceed 360 degrees  
  100.   
  101.     dbYRotateObject(2,dbWrapValue(ObjAngleY-5.0f));  
  102.    }  
  103.   
  104.   //if the D key is pressed  
  105.    if (dbKeyState(32))  
  106.    {  
  107.    
  108.      //rotate the object along the Y axis  
  109.   //for instance, if the objects current angle is 85+5  
  110.   //The object will then be rotated 90 degrees which is exactly north  
  111.   //from the user's POV  
  112.   //the dbWrapValue is to ensure that the object rotation will not exceed 360 degrees  
  113.     dbYRotateObject(2,dbWrapValue(ObjAngleY+5.0f));  
  114.    }  
  115.      //if the space key is pressed  
  116.    if(dbKeyState(57))  
  117.    {  
  118.   //is the object currently not playing?  
  119.    if (looping==0 )  
  120.    {  
  121.    //if yes then execute Attack.x  
  122.    //Attack.x has 24 frames so from the starting to ending frames  
  123.    //must be at least 24 frames  
  124.     dbLoopObject ( 2,51,77 );  
  125.   
  126.   
  127.    };  
  128.    }  
  129.    //if the Tab key is pressed  
  130.    if(dbKeyState(15))  
  131.    {  
  132.    //is the object currently not playing?  
  133.    if (looping==0 )  
  134.    {  
  135.    //if yes then execute Die.x  
  136.    //Die.x has 50 frames so from the starting to ending frames  
  137.    //must be at least 50 frames  
  138.     dbLoopObject ( 2,78,127 );  
  139.   
  140.   
  141.    };  
  142.    }  
  143.        //if the Q key is pressed  
  144.     
  145.    if(dbKeyState(16))  
  146.    {  
  147.   //is the object currently not playing?  
  148.    if (looping==0 )  
  149.    {  
  150.   //if yes then execute Impact.x  
  151.      //Impact.x has 10 frames so from the starting to ending frames  
  152.      //must be at least 10 frames  
  153.     dbLoopObject ( 2,127,137 );  
  154.   
  155.   
  156.    };  
  157.    }  
  158.   
  159.   
  160.    }  
  161.   else  
  162.   //if no key is pressed  
  163.   //execute Idle.x  
  164.   //Idle.x has 25 frames so from the starting to the ending frames miust be at least 25 frames  
  165.   {  
  166.    dbLoopObject(2,1,25);  
  167.   }  
  168. // update the screen  
  169.   dbSync();  
  170.  }   
  171. //return to windows  
  172. return;  
  173. }  



Another way of animating models is by using the dbShowObject and dbHideObject commands. I'll post an example of that here when I have time. If you want more information on animating darkGDK models or creating games per se, you can visit DarkGDK's official site www.thegamecreators.com