• Advertisement
Sign in to follow this  

Animation code, simplify code

This topic is 1469 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

In my main loop I check the average frame rate of my animation and based on this, I skip 1, 2 or 3 frames each 5 frames.

The code how I do this is shown below, but I can't imagine that there isn't an easier way to code this. Any suggestions?

(Ps.: it's all based on a animation recorded for 50 fps)

				frameTime = _timer.GetFrameTime();
				lastFramesTime += frameTime;
				++framesPassed;
				++fiveFramesPassed;
				
				if(framesPassed == 50)
				{
					avgFrame = lastFramesTime / 50;
					framesPassed = 0;
					lastFramesTime = 0;
					frameSkip = 0;

					if(avgFrame < 20.0f) frameSkip = 0;
					if(avgFrame > 20.0f && avgFrame < 25.0f) frameSkip = 1;
					if(avgFrame > 25.0f && avgFrame < 33.3f) frameSkip = 2;
					if(avgFrame > 33.3f && avgFrame < 50.0f) frameSkip = 3;
					SetWindowTextA(_d3d.GetHwnd(), _timer.mStrFramerate);
				}
				frameInterval = 1;

				switch(fiveFramesPassed)
				{		
					case 3:
					{
						if(frameSkip == 3) frameInterval = 2;
					}
					break;
					
					case 4:
					{
						if(frameSkip == 2) frameInterval = 2;
						if(frameSkip == 3) frameInterval = 2;

					}
					break;

					case 5:
					{
						if(frameSkip == 1) frameInterval = 2;
						if(frameSkip == 2) frameInterval = 2;
						if(frameSkip == 3) frameInterval = 2;
						fiveFramesPassed = 0;
					}
					break;
				}


// code of running the animation, later on:

					if(!_scene.mCamAnimation.NextFrame(frameInterval))
					{
						active = false;
						PostMessage(_d3d.GetHwnd(), WM_QUIT, 0, 0);
					}

Share this post


Link to post
Share on other sites
Advertisement

Hm, the code is really confusing. What exactly is going on here?

If you just want to find out the current animation frame to display, you could just do:

 

Edit: ...assuming every frame lasts for 1/50th of a second. I'm not sure if I read that correctly in the original post.

double dt; // Time since last update in seconds
double animationTime; // Time since the animation started in seconds
int numAnimationFrames; // Number of frames in animation
double timePerAnimationFrame = 1.0/50; // 50 fps
 
animationTime += dt; // Add passed time to animation
int currentAnimationFrame = int(animationTime / timePerAnimationFrame) % numAnimationFrames; // Get currently displayed frame, and loop
Edited by Madhed

Share this post


Link to post
Share on other sites

What special need do you have for approaching the task in this manner?

Why don’t you just calculate the current frame to render based on how much time has passed?

 

If it’s 50 frames-per-second and you are 0.2 seconds into the animation, display frame number 10.

Madhed just posted the math for it.

 

 

L. Spiro

Share this post


Link to post
Share on other sites
Thanks. Maybe I've confused it a bit with all the nasty code :)
I'll try to explain better:

- I have an animation which I want to run as if it's 50fps
- I have a time delta available to use, which works perfect with "manual movements" etc.
- I have a vector containing all frames and call the function NextFrame(interval) each frame, where interval is 1 if the hardware keeps up with 60fps
- I also have a function in my animation class to set the currentframe as an absolute value

So I see 2 solutions:
1. The one above, save the time then animation starts.
After each frame check the current time, and calculate the absolute frame I need at that moment, then set it as absolute value to be the frame I need
2. Pass in timeDelta / 1 as interval to my Nextframe function. Within this function, keep a static flost and everytime 1.0 is passed, skip that frame

I hope this clears things up, forget the horrible switch and if statements above :)

Share this post


Link to post
Share on other sites

Definitely option 1. The math is really simple, as shown above. It's the normal way to do it. Rounding errors do not accumulate over time like using a static float would.

 

But I honestly don't really understand option 2, so I'm basically saying "don't diverge from the standard way things are done unless your way is both better and necessary." Since you're asking these questions, I figure you don't know if your custom solution is better and it probably isn't solving some problem you needed solved either.

Share this post


Link to post
Share on other sites

Thanks. I fully understand, was just wondering what would be best.

The reason is that some frames have an identifier when a specific 'script' needs to be run in the animation.

 

I've solved it a few minutes ago using option 1. And within my "SetCurrentFrame" option of my animation class, I save the 'last rendered' frame and check if within the frames in between the script identifier was not 0, and if so, I set the script identifier to the new Currentframe. Works great (in worst cases the script starts 3 frames later @20fps then @60fps, no noticable difference

 

Thanks for the help.

 

Just to be sure I understand it correct, an example.

What frame would it be when I ran 60fps constant, after 2 minutes?

 

120.000 frames in total animation

0.016 seconds per frame (60fps)

120 seconds past

 

nextFrame = (120 / 0.016 = 7.200) % 120.000

nextFrame = 7200 % 120.000

(7.200 / 120.000 = 0.06 * 120.000)

nextFrame = 4800

Share this post


Link to post
Share on other sites

Thanks. I fully understand, was just wondering what would be best.

The reason is that some frames have an identifier when a specific 'script' needs to be run in the animation.

 

I've solved it a few minutes ago using option 1. And within my "SetCurrentFrame" option of my animation class, I save the 'last rendered' frame and check if within the frames in between the script identifier was not 0, and if so, I set the script identifier to the new Currentframe. Works great (in worst cases the script starts 3 frames later @20fps then @60fps, no noticable difference

 

Thanks for the help.

 

Just to be sure I understand it correct, an example.

What frame would it be when I ran 60fps constant, after 2 minutes?

 

120.000 frames in total animation

0.016 seconds per frame (60fps)

120 seconds past

 

nextFrame = (120 / 0.016 = 7.200) % 120.000

nextFrame = 7200 % 120.000

(7.200 / 120.000 = 0.06 * 120.000)

nextFrame = 4800

 

 

I can't tell when you're using a decimal point as separating thousands and when you're using it for fractional values.

If you're saying there are 120k frames, the frame after two minutes is 7200 since 7200 % 120000 is 7200.

If you're saying there are 120 frames, 7200 % 120 is 0 and you'd be on frame 0. I can't figure out where 0.06 came from.

 

On the off chance you're confused about what "%" does, it's the modulus (or modulo, same thing) operator and returns the remainder of integer division.

Share this post


Link to post
Share on other sites

There are indeed 120.000 frames. The 0.06 is 7200 / 120.000 (to find out indeed how modulus calculated the right frame :))

Share this post


Link to post
Share on other sites
I can't tell when you're using a decimal point as separating thousands and when you're using it for fractional values.

He is from Europe and is using it as a comma.

 

 

 

 

nextFrame = 7200 % 120.000
(7.200 / 120.000 = 0.06 * 120.000)
nextFrame = 4800

7,200 % 120,000 = 7,200.

% operator doesn’t overflow until the left side is actually larger than the right side.

 

7,200 % 120,000 == 7,200 - ((int)(7,200 / 120,000) * 120,000) == 7,200.

 

 

L. Spiro

Edited by L. Spiro

Share this post


Link to post
Share on other sites

Modulo is the remainder of integer division, so it's going to loop through your 120.000 frames. 0 % 120.000 = 0; 50.000 % 120.000 = 50.000; 121.000 % 120.000 = 1.000; 1.200.001 % 120.000 = 1

Edited by richardurich

Share this post


Link to post
Share on other sites
Yep, I'm indeed from europe. Annoying to have to use comma's and dots situationally. In my projects (c++) dots versus comma's in excel etc in my day job.

Thanks L. Spiro, good to see that I've 'reproduced' the exampe correctly.
Everything all up in running now in my animation.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement