• 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
JHA

Javascript/Canvas constan frame rate

2 posts in this topic

For a long time I have thought my code to ensure constant frame rate was right but recently I noted that the simple code to calculate when to run the next update is imprecise and always triggering at wrong time.

I discovered It when I changed the frame rate to 100 in place of 60 or 30, and the game started running at 90-65 fps. This means my browser can easily run It at 60 fps constant.

Here the code I think is wrong:

[CODE]
var nextTickTimeout = 1000/60 - (startTime - (new Date().getTime()));
if (nextTickTimeout < 0) nextTickTimeout = 0;
if (!quit) setTimeout(gameTick, nextTickTimeout);
[/CODE]

And this is the whole function:

[CODE]
function gameTick ()
{
var startTime = new Date().getTime();
date = new Date();
lastTime = currentTime;
currentTime = date.getTime();
deltaTime = currentTime - lastTime;
milliseconds += deltaTime;

// update game objects status
engine.update();

// render graphics
if (frameCount == Math.floor(frameCount / frameSkip) * frameSkip) engine.draw();

// code to measure FPS (Frames per Second)
if (milliseconds >= 1000)
{
fps = frameCount;

// update FPS for this frame
if (fpsElement != null) fpsElement.innerHTML = frameCount + " FPS";

// reset frameCount and milliseconds
frameCount = 0;
milliseconds = milliseconds - Math.floor(milliseconds / 1000) * 1000;

}
frameCount += 1;

var nextTickTimeout = 1000/60 - (startTime - (new Date().getTime()));
if (nextTickTimeout < 0) nextTickTimeout = 0;
if (!quit) setTimeout(gameTick, nextTickTimeout);
}
[/CODE]

Maybe is not possible to do It in javascript? I don't know how precise is setTimeout.

This is more of an experiment I'm doing to found out how complex the game can be and still run at least at 30-25 fps in a not so new machine. I will change "nextTickTimeout = 1000/60" for "nextTickTimeout = 1000/30" or 25 if 60 probes to be too much.
I have seen HTML 5 games of comparable complexity to the one I want to make so my game should be possible.
0

Share this post


Link to post
Share on other sites
JavaScript implementations have a lot of issues with garbage collection, and other things, that don't really allow you to have a constant frame rate (especially Firefox's GC which is horrible).

Instead of trying to get correct frame rates with intervals or requestFrameAnimation (which is even worse than intervals as far as I can see), make your physics/animation/logic/whatever part to run in a constant frame rate using a method that is explained here: gafferongames.com/game-physics/fix-your-timestep/
0

Share this post


Link to post
Share on other sites
Thank you.

I think that will work. As It still count as constant frame my already written code won't break.

One more think, last time I did (while (!quit)) in Javascript the page became unresponsive (can't click buttons, write in text fields, etc). For those reading this post that may not know yet, in Javascript, the most similar to a main loop you can do and still maintain responsiveness on the page is this:

[CODE]
var quit = false;

function run () {
var span = document.getElementById("thespan");
if (span.innerHTML == "Hello") span.innerHTML = "Bye"; else span.innerHTML = "Hello"; // Quit this

// Apply what I learned at gafferongames.com here

// Call engine.update() one, two or more times per run. In some cases update will be called one time each two or more run.

if (!quit) setTimeout(run, 0); // second parameter will be always 0
}

[/CODE]

I want to post one more time to this topic to show the results obtained from applying what I learned from gafferongames.com in case It is of interest to some.

Edit:
[b]Yes, this is the way to go.[/b]

My update cycles are called assuming a fixed delta time (1000/60 for example). Per each canvas refresh engine.update can be called two or more times, or even no called at all. Time between canvas render accumulates in a counter, when it is equal or greater than the desired delta time I start calling engine.update until it "consumes" the accumulated time, eating "desired delta time" each call.

I don't have use to the linear interpolation showed at gafferongames.com. Even if I calculate alpha, I cannot pass it to a render function, I don't have one. The more I can do is to call engine.update one more time if alpha is between [0.6-1.0] or similar. My update function does not accept parameters, It assumes a fixed delta thus no need for extra parameters.

I changed my method to measure FPS too.

Now I have 60 FPS most of the time in Chrome, with occasional 61 or 59 values. I have to add a lot of sprites to make it go to 30.

The strange thing, is that Firefox says 60 FPS too, but you can tell just by looking that animations aren't smooth. Completely different than Chrome. The FPS counter is measuring correctly, the time needed for a sprite to go from A to B is the same in both browsers, but animations looks strange in Firefox, this is more noticeable if Firefox is running on Linux. I thought solving the constant frame rate thing solves anything but it seems this is something specific to Firefox. Don't sure what is in the way now, maybe the garbage collector or the canvas implementation of each browser.

Edit 2: the solution to that was to use requestAnimationFrame and not setTimeout to end the run function. Animations play smoothly in both browsers.

http://paulirish.com/2011/requestanimationframe-for-smart-animating/
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