• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
cozzie

Animation code, simplify code

11 posts in this topic

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

0

Share this post


Link to post
Share on other sites

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
1

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

0

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

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.

0

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

0

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.

0

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
0

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
0

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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0