Jump to content

  • Log In with Google      Sign In   
  • Create Account

Animation code, simplify code


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 cozzie   Members   -  Reputation: 1611

Like
0Likes
Like

Posted 12 January 2014 - 10:31 AM

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);
					}



Sponsor:

#2 Madhed   Crossbones+   -  Reputation: 2953

Like
1Likes
Like

Posted 12 January 2014 - 11:43 AM

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, 12 January 2014 - 11:51 AM.


#3 L. Spiro   Crossbones+   -  Reputation: 13577

Like
0Likes
Like

Posted 12 January 2014 - 03:06 PM

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


It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#4 cozzie   Members   -  Reputation: 1611

Like
0Likes
Like

Posted 13 January 2014 - 01:54 AM

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 :)

#5 cozzie   Members   -  Reputation: 1611

Like
0Likes
Like

Posted 13 January 2014 - 01:16 PM

Anyone?
I should probably go safe and do option 1

#6 richardurich   Members   -  Reputation: 1187

Like
0Likes
Like

Posted 13 January 2014 - 02:09 PM

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.



#7 cozzie   Members   -  Reputation: 1611

Like
0Likes
Like

Posted 13 January 2014 - 02:48 PM

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



#8 richardurich   Members   -  Reputation: 1187

Like
0Likes
Like

Posted 13 January 2014 - 04:07 PM

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.



#9 cozzie   Members   -  Reputation: 1611

Like
0Likes
Like

Posted 13 January 2014 - 05:25 PM

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



#10 L. Spiro   Crossbones+   -  Reputation: 13577

Like
0Likes
Like

Posted 13 January 2014 - 05:32 PM

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, 13 January 2014 - 05:37 PM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#11 richardurich   Members   -  Reputation: 1187

Like
0Likes
Like

Posted 13 January 2014 - 05:51 PM

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, 13 January 2014 - 05:57 PM.


#12 cozzie   Members   -  Reputation: 1611

Like
0Likes
Like

Posted 14 January 2014 - 01:10 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS