Jump to content

Animation doesn't start right


ParaToxic
 Share

Recommended Posts

Hello Guys,

I'm writting a FPS Game and I have a problem with the shoot Animation.The animation starts at frame 123 and ends at 142.But sometimes the animation doesn't start at frame 123.

I think the problem is the fmodf command,but i don't know how i can solve that.

 

Code :

 

if(MouseHit(1))
weapon->shoot = true;


if(weapon->shoot)
{

	if(weapon->sequenc != weapon->WEAPON_SHOOT)
	{
		weapon->sequenc = weapon->WEAPON_SHOOT;  
	}
	if(weapon->frame > 141)
	 {
		 weapon->shoottimer = 0;
		 weapon->shoot = false; 
	 }
}
else
{
	weapon->sequenc = weapon->WEAPON_IDLE;
}

and WEAPON_SHOOT define framebegin and frameend and set frame to framebegin:

case Weapon::WEAPON_SHOOT:
	weapon->framebegin = 123;
	weapon->frameend = 142;
	weapon->frame = weapon->framebegin;
	break;

and Animate :

weapon->frame=AppTime()/40.0;
   weapon->frame=fmodf(weapon->frame,weapon->frameend-weapon->framebegin)+weapon->framebegin;
   Animate(weapon->model,weapon->frame,1.0,0,true);

 

Thank you for help ;)

Link to comment
Share on other sites

I've discovered that using the fmodf method sucks. Instead (thanks Rick) I used an iterative method.

 

   if (framebegin != lastframebegin) {
           frame=framebegin;
           }
       else {
           frame=frame+0.1*animationspeed*AppSpeed();
       }
   if (frame >= frameend) {
           frame = framebegin;
   }

Animate(mesh,frame,1.0,0,true);

 

Each action has a framebegin and frameend value. For a new action, frame is set to framebegin. A new action is detected by a change in the framebegin value in each loop. The frame value is incremented without using the fmodf command and animation is played until the frame value reaches the frameend value. Meanwhile other code has determined the value of the next framebegin value for the next action.

 

I hope I explained that well enough. It's off the cuff. Though that code segment is pulled directly from my game project. See video here: http://www.leadwerks.com/werkspace/page/videos/_/leadwerks-engine/my-first-demo-r105

Link to comment
Share on other sites

Thanks so much,it works very well.

 

EDIT:How you defined the variable lastframebegin ?

 

By the side i made a little class named Animation(Since the thread was already created),with all the variables and a Blending Function with the new sequenc and a smooth variable as parameters.For example your currently animation is sequenc 1 and you will change that to sequenc 2 with a smooth faktor of 0.01,you only call the Command BlendingAnim(2,0.01) and it smooth the blendingfaktor to 0.0 with the smooth value (0.01) ,set the new sequenc and go to 1.0.

 

For somebody who needs it ;D

 

Animation.h :

#include <math.h>

class Animation
{
public:
Animation();


float framebegin;
float frameend;
float frame;
float blend;
float blendfaktor;
bool blending;
int sequenc;
int newsequenc;
float blendplus;
float movement;

void BlendingAnim(int s,float a);
void Update();


};

 

Animation.cpp

#include "Animation.h"



Animation::Animation()
{
framebegin = 0.0f;
frameend = 0.0f;
   frame = 0.0f;
blend = 1.0f;
blendfaktor = 0.0f;
   blending = false;
sequenc = 1;
newsequenc = sequenc;
blendplus = 0.0f;
movement = 1.0f;
}
void Animation::BlendingAnim(int s,float a)
{
Animation::blending = true;
Animation::newsequenc = s;
Animation::blendplus = a;

}
void Animation::void AnimOnce(int s,int olds)
{

}
void Animation::Update()
{
Animation::blend +=Animation::blendfaktor;
Animation::movement = 1.0f;


if(Animation::blending == true)
{

	if(Animation::sequenc != Animation::newsequenc && Animation::newsequenc != 0)
	{
		if(Animation::blend > 0.2)
		{
		    Animation::blendfaktor = -Animation::blendplus;
		}
		else if (Animation::blend <= 0.2)
		{
			Animation::sequenc = Animation::newsequenc;
			Animation::blendfaktor = 0.0;
		}
		Animation::movement = 0.0f;
	}
	if(Animation::blend < 1.0 && Animation::sequenc == Animation::newsequenc )
	{
		Animation::blendfaktor = Animation::blendplus;
	}
	else if(Animation::blend == 1.0 && Animation::sequenc == Animation::newsequenc)
	{
		Animation::blendfaktor = 0.0;

		Animation::newsequenc = 0;
		Animation::blending = false;
	}
}
}


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...