It is currently Fri Nov 24, 2017 6:02 am

All times are UTC - 5 hours




 Page 1 of 1 [ 12 posts ] 
Author Message
 Post subject: Physics VMK 4 - Cannon Ball Projectile Motion
PostPosted: Mon Feb 04, 2008 9:45 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
In VMK 4A, I show you how to load in a VMK file of a cannon that we will be using to shoot cannon balls. I add keyboard control to the cannon so that you can change its pointing direction and pitch angle. A few bugs inside the texture manager are also fixed.

I add the cannon ball to the scene in VMK 4B/C. You'll be able to load the cannon, aim it and shoot it. I forgot to show you how to fix the problem that occures when you change the direction of the cannon while the cannon ball is flying .... I'll do that at the beginning of VMK 4D.

In VMK4D I add gravity to the motion of the cannon ball so that it falls back down to the ground after we shoot it. I also show you how to implement simple collision detection with the ground.


Offline
 Profile  
 
 Post subject: How To Implement The Functions For The Flags
PostPosted: Wed Jan 13, 2010 1:21 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 450
I have the Cannon Ball VMKs complete, and I am about to move on to implementing the wind code VMKs, but before I do that I would like to implement the functions involved in setting up the flags to show the distance's the cannon ball lands. This is where I got lost!

I know that at some point we have to call LoadVMKFile( "resources/models/flags.vmk" );
but indoing this it wants to draw all the flags at the time we call this function.
I do know that we can reference each transform node with in this *.vmk file with our FindNode( "" ); string function but im kindof getting confused here.

Through other VMKs I have not seen the code for these functions, but with in the Scene Header File I did see that you had two function prototypes: bool InitFlags() and void PositionFlags() which the names of the functions themselves are self-explanatory, and we have our member variable
std::vector<NodeTransform*> m_pNT_Flag;

I tried using a for loop to initialize the Flags but still couldn't get it to work. :( I am not sure what to do at this point.


Offline
 Profile  
 
 Post subject: This is what I tried to do
PostPosted: Thu Jan 14, 2010 2:41 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 450
These functions are in the Scene Class

// -------------------------------------------------------------------------------- //
// Name: InitFlags()
// Desc: Initialize The Flags For The Cannon Ball
// -------------------------------------------------------------------------------- //
bool Scene::InitFlags()
{
   LoadVMKFile( "resources/models/flags.vmk" );
   static Node *pNode;
   pNode = m_vpNodes[0]->FindNode( "flags.cob" );
   
   if ( pNode )
   {
      pNode->m_bVisible = false;
      return true;
   }
      

   return true;

} // InitFlags

// -------------------------------------------------------------------------------- //
// Name: PositionFlags()
// Desc: Position The Flags Where The Cannon Ball Lands
// -------------------------------------------------------------------------------- //
void Scene::PositionFlags()
{
   static Node *pNode;
   pNode = m_vpNodes[0]->FindNode( "flags.cob" );
   
   static Node *pNodeBall;
   pNodeBall = m_vpNodes[0]->FindNode( "ball" );
   
   Vector3 vec3;
   vec3 = ((NodeTransform*)pNodeBall)->m_v3Translate;
   
   if ( pNode )
   {
      static NodeTransform *pT = (NodeTransform*)pNode;
      static Node *pN = pT->m_pIn;
      if ( pN && pN->IsTransform() )
      {
         pT->FindNode( "flagA" );
         pT->m_v3Translate = vec3;
         pN->m_bVisible = true;
      }
      
   }

} // PositionFlags


The first function works correctly it does initialize the flags but doesn't render them cause the m_bVisible flag is set to false; but when I try to reference the nested transforms I can not seem to get it to work correctly
I have only tried to do this with flagA to see if I could get it to work first then go and do the rest of the flags.

The second thing is with this way of implementing these functions I do not see where we use the member variable m_pNT_Flag that is of a std::vector<NodeTransform*> in this code. This is where I am getting lost, any help on this we be great! This doesn't even count the fact that flags d-f need to be rotated, but that is not hard to do once we can implement this function correctly.


Offline
 Profile  
 
 Post subject:
PostPosted: Thu Jan 14, 2010 4:11 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
Inside the InitFlags function I searched the scenegraph and found all the node transforms for all the flags. When I find them, I then put them into the std::vector so that I don't have to look for them anymore.

Inside the PositionFlags function I just select the desired flag to move from the std::vector and apply the position of it in the same way that you are doing it.

Run your code in the debugger and take a look at the point in your code when you search for flagA. Do you find it in the scenegraph? Output your scenegraph to your log file to make sure you are looking for the correct node name (capitalization matters) and that the cob file loaded into memory correctly.


Offline
 Profile  
 
 Post subject: Im getting a lot closer but im still stuck
PostPosted: Sun Jan 17, 2010 1:00 am 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 450
Here are the canges I made to the two functions.

// ------------------------------------------------------------------------------ //
// Name: InitFlags()
// Desc: Initialize The Flags For The Cannon Ball
// ------------------------------------------------------------------------------ //
bool Scene::InitFlags()
{
   LoadVMKFile( "resources/models/flags.vmk" );
   static Node *pNode;
   pNode = m_vpNodes[0]->FindNode( "flags.cob" );

   static NodeTransform *pT = (NodeTransform*)pNode;
   static Node *pN = pT->m_pIn;   
   
   if( pN && pN->IsTransform() )
   {
      m_pNT_Flag.push_back( (NodeTransform*)pN );
      m_pNT_Flag[0] = (NodeTransform*)pN;
      m_pNT_Flag[0]->m_bVisible = false;

      m_pNT_Flag.push_back( (NodeTransform*)pN );
      m_pNT_Flag[1] = (NodeTransform*)pN->m_pNext;
      m_pNT_Flag[1]->m_bVisible = false;

      m_pNT_Flag.push_back( (NodeTransform*)pN );
      m_pNT_Flag[2] = (NodeTransform*)pN->m_pNext->m_pNext;
      m_pNT_Flag[2]->m_bVisible = false;

      m_pNT_Flag.push_back( (NodeTransform*)pN );
      m_pNT_Flag[3] = (NodeTransform*)pN->m_pNext->m_pNext->m_pNext ;
      m_pNT_Flag[3]->m_bVisible = false;

      m_pNT_Flag.push_back( (NodeTransform*)pN );
      m_pNT_Flag[4] = (NodeTransform*)pN->m_pNext->m_pNext->m_pNext->m_pNext;
      m_pNT_Flag[4]->m_bVisible = false;

      m_pNT_Flag.push_back( (NodeTransform*)pN );
      m_pNT_Flag[5] = (NodeTransform*)pN->m_pNext->m_pNext->m_pNext->m_pNext->m_pNext;
      m_pNT_Flag[5]->m_bVisible = false;
   }      

   return true;

} // InitFlags

// ------------------------------------------------------------------------------ //
// Name: PositionFlags()
// Desc: Position The Flags Where The Cannon Ball Lands
// ------------------------------------------------------------------------------ //
void Scene::PositionFlags()
{
   // Find Ball Transform And Vector Position
   static Node *pNodeBall;
   pNodeBall = m_vpNodes[0]->FindNode( "ball" );

   Vector3 vec3;
   vec3 = ((NodeTransform*)pNodeBall)->m_v3Translate;

   // Find Flags Transform
   for ( int i = 5; i >= 0; i-- )
   {
      static NodeTransform *pT;
      if ( pT->IsTransform() )
      {
         if ( i > 2 && i <= 5 )
         {
         
            pT->m_v3Translate.m_fX = vec3.m_fX;
            pT->m_v3Translate.m_fZ = vec3.m_fZ;
            pT->m_bVisible = true;
            
         }
         else
         {
            pT->SetRotateAxis( Vector3( 0, 1, 0 ) );
            pT->m_fRotateAngle = 180;
            pT->m_v3Translate.m_fX = vec3.m_fX;
            pT->m_v3Translate.m_fZ = vec3.m_fZ;
            pT->m_bVisible = true;
         }
      }
   }


} // PositionFlags


For the InitFlags() function there should be an easier way to implement this, but for now this does work as it does add the individual transform nodes of each flag into the member variable vector m_pNT_Flag[].
I tried using a for loop but could not seem to get it to work correctly, that is why i did it the long way lol.

Now for the PositionFlags() function i am checking the variables with the debugger, the position vectors are correct for flagA and the m_bVisible variable is being tripped back to true, but it is not rendering the flag to the screen. ??? Ahhh this is starting to become a headache.


Offline
 Profile  
 
 Post subject:
PostPosted: Tue Jan 19, 2010 8:00 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
skilz80, I think you are over thinking this problem. I'll show you how I initalized the flags:

bool Scene::initFlags() {
   loadVMKFile("resources/models/flags.vmk");

   m_pNT_Flag.push_back( (NodeTransform*)m_vpNodes[0]->findNode("flagA") );
   m_pNT_Flag.push_back( (NodeTransform*)m_vpNodes[0]->findNode("flagB") );
   m_pNT_Flag.push_back( (NodeTransform*)m_vpNodes[0]->findNode("flagC") );
   m_pNT_Flag.push_back( (NodeTransform*)m_vpNodes[0]->findNode("flagD") );
   m_pNT_Flag.push_back( (NodeTransform*)m_vpNodes[0]->findNode("flagE") );
   m_pNT_Flag.push_back( (NodeTransform*)m_vpNodes[0]->findNode("flagF") );

   for (int i=0; i<(int) m_pNT_Flag.size(); i++ ) {
      if (m_pNT_Flag.at(i)) {
          //initalize flag state/position   
      }
      else {
         m_pErrorHandler->setError(EC_NotEnoughMemory,"Can not find m_pNT_Flag(%d).",i);
         m_pErrorHandler->showErrorMessage();
         m_pErrorHandler->setError(EC_NoError,"No Error.");
         return false;
      }
   }

   return true;
}//initFlags


As for positioning the flags, you have the right idea. Now just make sure you are using the correct co-ordinate system. Look at the scene graph and make note of the heirarchy. Start with just one flag and figure out what values you need to use to place it on the screen somewhere. Then you'll need to figure out how to place the flag so that it is using the same coordate system as the cannon ball. You can use the debugger to change the flag's position values or you can automate the process by using the arrow keys and making key presses change the values.

You might also want to not render the ground in case your flags are rendering below the ground.... this way you can see everything on the screen.


Offline
 Profile  
 
 Post subject: PositionFlags???
PostPosted: Wed Jan 20, 2010 9:35 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 450
This is what i have for the PositionFlags() function:

// ------------------------------------------------------------------------------ //
// Name: PositionFlags()
// Desc: Position The Flags Where The Cannon Ball Lands
// ------------------------------------------------------------------------------ //
void Scene::PositionFlags()
{
   // Find Ball Transform And Vector Position
   static Node *pNodeBall;
   pNodeBall = m_vpNodes[0]->FindNode( "ball" );

   Vector3 vec3;
   vec3 = ((NodeTransform*)pNodeBall)->m_v3Translate;
   
   // Find Flags Transform
   for ( int i = 0; i < (int)m_pNT_Flag.size(); i++ )
   {
      if ( m_pNT_Flag.at( i ) && (i >= 3) )
      {
         m_pNT_Flag[i]->m_bVisible = true;
         m_pNT_Flag[i]->m_v3Translate.m_fX = -vec3.m_fX;
         m_pNT_Flag[i]->m_v3Translate.m_fZ = -vec3.m_fZ;
      }
      else if ( m_pNT_Flag.at( i ) && (i < 3) )
      {
         m_pNT_Flag[i]->m_bVisible = true;
         m_pNT_Flag[i]->m_v3Translate.m_fX = -vec3.m_fX;
         m_pNT_Flag[i]->m_v3Translate.m_fZ = -vec3.m_fZ;
      }
   }

} // PositionFlags


Im not that great with containers just yet, im still learning them, when i debug the code the iterator i does increment, but when i run the code it does not goto flag b, it just moves flag a to the new location that the ball lands.

How do i increment to the next flag?


Offline
 Profile  
 
 Post subject:
PostPosted: Wed Jan 20, 2010 10:12 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
The container called m_pNT_Flag holds 6 flags. Your PositionFlags function has a for-loop that iterates through all 6 flags in the container and moves them all each time. Since your if and else-if statement inside the for loop are exactly the same you are basically moving all 6 flags to the same position.

You need an extra variable to keep track of which flag you want to move. This variable would select one of the six from the container and move only that one. No for-loop required in this function.

Once you are done moving the flag, you can update your variable to select the next flag to be moved.


Offline
 Profile  
 
 Post subject: Ok
PostPosted: Wed Jan 20, 2010 11:16 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 450
I changed it to this:

void Scene::PositionFlags()
{
   // Find Ball Transform And Vector Position
   static Node *pNodeBall;
   pNodeBall = m_vpNodes[0]->FindNode( "ball" );

   Vector3 vec3;
   vec3 = ((NodeTransform*)pNodeBall)->m_v3Translate;
   
   // Find Flags Transform
   static int i = 0;
   if ( m_pNT_Flag.at( i ) && (i >= 3) )
   {
      m_pNT_Flag[i]->m_bVisible = true;
      m_pNT_Flag[i]->m_v3Translate.m_fX = -vec3.m_fX;
      m_pNT_Flag[i]->m_v3Translate.m_fZ = -vec3.m_fZ;
      i++;
   }
   else if ( m_pNT_Flag.at( i ) && (i < 3) )
   {
      m_pNT_Flag[i]->m_bVisible = true;
      m_pNT_Flag[i]->m_v3Translate.m_fX = -vec3.m_fX;
      m_pNT_Flag[i]->m_v3Translate.m_fZ = -vec3.m_fZ;
      i++;
   }
   else if ( i >= 6 )
   {
      i = 0;
   }

} // PositionFlags


this does work, it moves to the next flag, but once flagF show up and I fire the cannon again i get an Unhandled exception @ 0x75aefbae.
exception: std::out_of_range at memory location 0x003fe5b0..

and the Vector file is opened to line 1263.

Shouldn't the last statement set i back to 0 which should be flagA?
How do i get it to point back to the first flag?


Offline
 Profile  
 
 Post subject:
PostPosted: Thu Jan 21, 2010 6:13 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
I see the reason why your code crashes. The best way for you to see why it does not work is to set up a break point in this function and step through your code when you fire the cannon. The error message explains it pretty well why your code doesn't work.


Offline
 Profile  
 
 Post subject: I Finally Got It To Work :)
PostPosted: Sat Feb 06, 2010 10:12 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 450
Here is the implementation that I use to get the flags to work correctly
// ------------------------------------------------------------------------------- //
// Name: PositionFlags()
// Desc: Position The Flags Where The Cannon Ball Lands
// ------------------------------------------------------------------------------- //
void Scene::PositionFlags()
{
   // Find Ball Transform And Vector Position
    static Node *pNodeBall;
    pNodeBall = m_vpNodes[0]->FindNode( "ball" );

    Vector3 vec3;
    vec3 = ((NodeTransform*)pNodeBall)->m_v3Translate;

    // Find Flags Transform
   static int i;
   if ( m_pNT_Flag.at( i ) && ( i >= 3 ) )
   {
      m_pNT_Flag[i]->m_bVisible = true;
      m_pNT_Flag[i]->m_v3Translate.m_fX = -vec3.m_fX;
      m_pNT_Flag[i]->m_v3Translate.m_fZ = -vec3.m_fZ;
      m_pNT_Flag[i]->SetRotateAxis ( Vector3( 0,1,0 ) );
      m_pNT_Flag[i]->m_fRotateAngle = 270.0f;
   }
   else if ( m_pNT_Flag.at( i ) && ( i < 3 ) )
   {
      m_pNT_Flag[i]->m_bVisible = true;
      m_pNT_Flag[i]->m_v3Translate.m_fX = -vec3.m_fX;
      m_pNT_Flag[i]->m_v3Translate.m_fZ = -vec3.m_fZ;
      
   }
   if ( m_pNT_Flag.at( i ) && ( i == 5 ) )
   {
      i = -1;
   }
   i++; 

} // PositionFlags


When I added the last if statement, it took me some time to think why this did not work when I was changing the variable i back to 0, then I remembered that the vectors are 0 based and that I am incrementing this after the if statement, so I ended up changing this value from 0 to -1 and all works well now :))


Offline
 Profile  
 
 Post subject:
PostPosted: Sun Feb 07, 2010 6:36 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
The last if statement that you have would be better structured if you rewrote it this way:

if( i == 5 ) {
   i = 0;
}
else {
   i++;



Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 12 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  

cron