Dominic Hughes said:
Hope that answers your questions and no problem though it's nice to have the consideration as an option.
ok i sorta understand your background, i'll keep my explanation very simple indeed;
thanks and apologies for late reply… i shutdown for christmas -?-, but i see that u have received some decent answers; so i will only touch on what has not been answered (in simple explanation);
Animation these days comes in various forms, but forget everything for a moment…let's picture this for a moment:
look outside through your window, look at a tree and focus your attention on 1 leaf only of that tree ;
now look at your watch (or wall clock) and every second try and see how that leaf behaves;
there are 2 things you should understand with this picture scene:
- the leaf moves (or is animated by the wind) every time (but in our case every second) that you have looked at your watch:
this is called “time-based” animation - now imagine if you could draw by hand on the surface of your window (using a Marker pen or so) every position that this leaf was animated to by the wind:
this is called “frame-based” animation
These 2 concepts aim to achieve the same objective: the animation of the subject-matter (in our case the leaf);
The main difference between these 2 forms of animation is that:
- in time-based animation, time drives the animation like this:
your game engine continuously announces the time (like your watch) and everytime a pre-defined time of interest has been announced (like 1second) then you animate the subject-matter
So if you want to animate your leaf every second, then everytime your engine tells u that a second has elapsed (since the last time it drew something) then you animate this leaf; - in frame-based animation, each video frame count drives the animation like this:
(note: each time you had to draw a new leaf on your window without lifting your pen counts as a video frame rendering), so in analogy your game engine can be coded such that it can continuously announce/notify you each time it is about to draw without lifting its pen, that is, everytime a pre-defined number of frames has been announced (like 24 frames) then you animate the subject-matter
So if you want to animate your leaf every 24 frames, then everytime your engine tells u it is about to draw on its 24th frame (since the last time it drew something) then you animate this leaf ;
note: when i say “since the last time….”, it means:
- the game engine has to be able to reset time to be able to tell how much time is elapsing from the last time, this is simply like pressing a timer reset button (like on real stopwatches: Start | Stop | Reset )
- likewise the game engine has to be able to tell how many video frames have gone on (or been displayed), so that when it gets to the frame of interest it can draw what we want on it
i hope so far this makes sense, if not, read again until it sinks in a bit ?
some game engines have one or the other methods implemented, others have both, it depends on what you want to achieve, how you want to achieve and also very often how the animation data is stored (but let's not go there)…
so let's pick a real example now: GRID5 from Codemasters:
when the company Codemasters claims that their game engine can render GRID5 the game at 60 FPS, it means:
- in frame-based animation: their engine can animate our leaf up to 60 times (frames) a second; so within 1 second of time, that leaf could have been animated 1 to 60 times;
- in time-based animation: their engine can animate our leaf approximately every 16.66666667milliseconds; how do we get to this number?
if 60 frames are rendered in 1 second which is 1000milliseconds then 1 frame can be rendered in (1000/60)msec
now you can imagine that with more than just 1 leaf to render, it does take a lot of code effort to render everything (not just a leaf) in under 16msec (but that's discussion for other web forums -lol-)
anyway now let's answer some of your questions (the Render( ) you have presented contains both time-based and frame-based code):
var last = 0; // no idea
Remember I mentioned about RESET: well, this is how you tell the engine to reset its time-based counter and later on, instead of resetting to 0 again, you just reset it to the current time:
last = now; // last is now, why?
why? because the current time is no longer 0 it has moved on and is depicted in now, so by storing now into last you are telling the engine to effectively reset its time-based counter to the current time in now ?
next:
if(delta > 20 //what does this value represent??) {
first of all you should not put a comment like this, it should be like this:
if(delta ≥ 20) { //what does this value represent??
i assume that was probably an editing error as you typed your question;
so what does 20 represent?
Because you are calculating time in millisecs as in this line:
now = performance.now(); //to get the current timestamp in milliseconds
20 is therefore 20msec
Remember I mentioned pre-defined time?
well, 20msec is the time-based pre-defined time at which you want to animate your "leaf"
and delta is used to measure time since the last time the engine drew something and if the time difference since the last draw is more than 20msec then your code increases the frame number;
if GRID5 does this every 16msec which means that every 1000th msec, it draws: 1000 / 16.6.. = 60 frames,
your engine does this every 20msec which means that every 1000th msec, your engine draws: 1000 / 20 = 50 frames
delta = now - last; // last is 0, how does this work?
yes last is 0 when then engine's stopwatch called performance in your code started, but the next time the Render( ) function is called again last will not be 0, this has been explained to you by @stardog earlier
next:
Dominic Hughes said:
FrameNumber += FrameNumber + 1; // increment FrameNumber by 1 could have been FrameNumber++
this has been explained to you by @alberth
next:
Dominic Hughes said:
delta = 20; // No idea why I need to keep this the same number??
you keep it the same number because you want to the same action the next time delta is the 20th msec again, why? because that's when frame number is increased in your code, in other words, everytime your stopwatch performance hits its 20th msec you increase frame number and then you press the reset button, which is the same as saying: delta = 20;
you can change 20 to whatever you want, but that would change the engine's behaviour as well
finally,
i can see that from previous explanations from other fellow posts, u have improved your Render( ) already, and @8observer8 has also given u a decent improved version of your code;
so hope u see the light a bit more now, and i hope my explanation was simple enough to understand;
All the best and have fun ?