Thread: Speeding up animation as velocity increases???

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    17

    Speeding up animation as velocity increases???

    When moving a chopper picture around the screen using 3 different chopper pictures, how does one increase the animation speed as the chopper speed increases in velocity?

    The drawHelicopter function below switches between each helicopter picture using a counter and then resets. The move function is included to show you how I'm increasing the chopper's speed.


    Code:
    void Helicoptor::drawHelicopter(MyDrawEngine* pTheDrawEngine, MyPicture* pHelicopter, double fRate)
    {
    	sourceRect.top 		= 0;
    	sourceRect.bottom 	= 32;
    	if(frameCounter == 0)
    	{	
    		sourceRect.left		= 0;
    		sourceRect.right	= 64;
    	}
    	if(frameCounter == 1)
    	{
    		sourceRect.left = 64;
    		sourceRect.right = 128;
    	}
    	if(frameCounter == 2)
    	{
    		sourceRect.left = 128;
    		sourceRect.right = 192;
    	}
    
    	destRect.top = (int)yCoord;
    	destRect.bottom = (int)yCoord+32;
    	destRect.left = (int)xCoord;
    	destRect.right = (int)xCoord+64;
    
    	//Blit: Copy from surface to back buffer(temp draw surrface)
    	pTheDrawEngine->Blit(destRect, sourceRect, pHelicopter);
    }
    
    void Helicoptor::move(MyInputs* pTheInputs, double fTime)
    {
    	yCoord = yCoord + yVelocity * fTime;
    	xCoord = xCoord + xVelocity * fTime;
    
    	if(yCoord < 0) yCoord = 0;
    	if(yCoord > 720) yCoord = 720;
    	if(xCoord < 0) xCoord = 0;
    	if(xCoord > 960) xCoord = 960;
    	
    	//Move direction
    	if(KEYPRESSED('Q') && (yVelocity > -61))
    	{
    		yVelocity = yVelocity - pixels_per_sec;
    	}
    	if(KEYPRESSED('A') && (yVelocity < 61))
    	{
    		yVelocity = yVelocity + pixels_per_sec;
    	}
    	if(KEYPRESSED('N') && (xVelocity > -61))
    	{
    		xVelocity = xVelocity - pixels_per_sec;
    	}
    	if(KEYPRESSED('M') && (xVelocity < 61))
    	{
    		xVelocity = xVelocity + pixels_per_sec;
    	}
    
    	frameCounter++;
    	if(frameCounter == 3) frameCounter = 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    One option is to store the frameCounter as a decimal number instead. Each frame you increase it in the lines of:
    Code:
    frameCounter += (xVelocity * xVelocity + yVelocity * yVelocity) * fTime * someConstant;
    The (xVelocity * xVelocity + yVelocity * yVelocity) is to calculate the squared length of the speed vector, or how fast the helicopter is going (squared speed). When you test what frame to draw you simply cast it to an int.

    Another solution is to update the animation each frame, with the delta time from the last frame. This value gets added to a variable that keeps track of how long it has been since the last frame, if this variable is greater than the 1/FPS of the animation you increase the frame.
    Something in the lines of: (pretty much uncommented code, but it should be pretty clear)
    Code:
    AnimationController::VUpdate(float delta)
    {
    
    	if (m_pCurrentAnimation)
    	{
    		m_TimeSinceLastFrame += delta;
    
    		while (m_TimeSinceLastFrame > 1.0f/m_pCurrentAnimation->fps)
    		{
    			m_CurrentFrame++;
    
    			//Check if the current frame is at the end of the animation.
    			if (m_CurrentFrame >= m_pCurrentAnimation->start + m_pCurrentAnimation->length)
    			{
    				m_CurrentFrame = m_pCurrentAnimation->start;
    
    			}
    
    			m_TimeSinceLastFrame -= 1.0f/m_pCurrentAnimation->fps;
    		}
    	}
    
    }
    Good luck!

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    70
    The problem here is that you are always incrementing the animation once very update. So in this sense it is always animating as fast as possible. You'll need to put in code like Wazaa's to make it update based on time, not on frames. So if the velocity is 0 you shouldn't increment the animation at all, if it is say 2 you might increment the animation every 8 updates. If the helicopter is booking then you would increment every update like you are now.

    edit:
    I'm bored so I figured I would write this.
    ex.
    replace
    Code:
    	frameCounter++;
    	if(frameCounter == 3) frameCounter = 0;
    with
    Code:
            //m_animationTimer is a member variable of Helicopter
    	m_animationTimer -= fTime * sqrt(pow(yVelocity,2)+pow(xVelocity,2));//gets magnitude
    	if( m_animationTimer <= 0 )
    	{
    		frameCounter++;
    		if(frameCounter == 3) frameCounter = 0;
    		//m_ANIMATION_TIMER_START is a constant that you should tweak.
    		m_animationTimer = m_ANIMATION_TIMER_START;
    	}
    Last edited by Drac; 02-19-2009 at 12:31 PM.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    17
    Ha ha thats quality that guys. Thanks.

    P.s. now I feel guilty for not doing it myself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. Threads in MFC
    By VirtualAce in forum Windows Programming
    Replies: 4
    Last Post: 12-28-2005, 06:03 PM
  3. Animation class not working
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 03-02-2005, 06:48 AM
  4. Velocity vector troubles
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 01-18-2005, 11:40 AM
  5. 3D animation (difficult?)
    By maes in forum Game Programming
    Replies: 3
    Last Post: 09-08-2003, 10:44 PM