Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Timer Based Movement - Question on snippet

Author
Message
Burning Feet Man
18
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 7th Aug 2011 14:52 Edited at: 7th Aug 2011 14:53
Hey all,

I've been staring at a Timer Based Movement snippet for a few minutes now. Although I've tested it months back, it works great, but rehashing over my code today, I suddenly find myself curious as to how part of it works...

From a previous thread;

http://forum.thegamecreators.com/?m=forum_view&t=171247&b=1

Quote: "The loop for smoothing basically just takes the values from the last 30 loops and averages them, so you don't end up with a sudden compensation if the framerate drops and then comes back up. It averages that over a very short amount of time, thus making the movement appear smoother."


With the code below;



So what I'm not understanding, is this part;


for tx = 1 to 29
factors(tx) = factors(tx+1)
factor# = factor# + factors(tx)
next tx


The OP talks about this loop "smoothing" out things and reducing jerkiness. But I thought such a loop would have to take place over 30 sync cycles, not 30 For-Next cycles?

What am I missing here...

Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia
Indicium
18
Years of Service
User Offline
Joined: 26th May 2008
Location:
Posted: 7th Aug 2011 15:15 Edited at: 7th Aug 2011 15:16
That for loop shifts the array down one space. So factors(2) becomes factors(1), and factors(30) becomes factors(29) and factors(30) is now free for a new value.

Hope that makes sense.

KISTech
18
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 7th Aug 2011 22:12
That original poster would be me.

Like Indicium said, that for next loop just shifts the values in the array to make room for the new value. There are probably faster ways to do it, but it works.

The real meat of that bit of code is that it adds all of those factors into factor#, then divides factor# by 30. Giving you the average value of the last 30 factors.

Burning Feet Man
18
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 8th Aug 2011 00:10
Had a slow read through your code again this morning, finally got it understood! Will see if I can improve on it tonight.

Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia
KISTech
18
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 8th Aug 2011 00:15
There isn't much that can be done to improve the loop, since you have to add all the elements in the array together anyway. There are likely other ways to improve smoothing, but I haven't thought of one yet.

GIDustin
18
Years of Service
User Offline
Joined: 30th May 2008
Location:
Posted: 9th Aug 2011 08:12
I believe this version is several nano-seconds faster. Not a whole lot of optimizing though.



Instead of adding up the elapsed time of the last 30 frames, why not divide the time since 30 frames ago by 30?

Of course, now we are just splitting hairs... I am sure the most anal of optimizers could even do one better! Of course, either of our methods is painfully off balance for the first 29 frames.
KISTech
18
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 9th Aug 2011 19:41
Quote: "either of our methods is painfully off balance for the first 29 frames. "


Yeah, that's always been one little flaw in the system. I try and do a best guess on that and fill the array with values close to what it might be. I've been thinking of writing a test scene using a bunch of 3D shapes, but they're all black so they don't show. DBP would still think it has to render them and you could get the timing from that in about 2 seconds to fill the array with values that would be close. Then just set flast to the current timer value before heading into the main loop.

What about storing the result of the 30 values added together. Then you can subtract the oldest and add the newest each loop. That would eliminate the for next loop as well.



Burning Feet Man
18
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 10th Aug 2011 13:13 Edited at: 10th Aug 2011 14:11
Haha, you guys are awesome. Myself being a DBpro novice, I was also wondering if there's a faster way to store and access the factor array, maybe memblocks? I'm not too sure through, as I've never advanced further than arrays and thus haven't practised other array techniques.

At this stage, if I can add anything to this thread, I've found that another handy TBM global variable to add is this one;

Global Factor_360 as float
Factor_360# = 360 * Factor#

Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia
KISTech
18
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 10th Aug 2011 21:08
Ok. What does that do?

Burning Feet Man
18
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 11th Aug 2011 00:49 Edited at: 11th Aug 2011 00:50
I've found it's handy for spinning objects, say I wanted to spin an object two revolutions per second;



In saying and practising this, I'm not too sure if there's a better way, but it's working.

Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia
KISTech
18
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 11th Aug 2011 00:54
Ah, I see. You wouldn't necessarily need a global for that though. You would multiply factor# by your rotation speed at the line where you are rotating it. That's the purpose of making the factor# variable global.

Login to post a reply

Server time is: 2026-07-10 17:19:51
Your offset time is: 2026-07-10 17:19:51