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 / timing speed issue / odd even loop checks

Author
Message
indi
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 15th Nov 2006 08:21
I have an idea to speed up my game based on the timer approach that people use.

If my game is running at 60fps or theoretically at 60 fps, could i deploy a timer to activate 60 times a second?. if so how? Would this synchronization improve speed?


Would a variable switcher eg: first loop is even, 2nd loop is odd, third loop is even etc..
and perform a check every 2nd loop, would that increase speed but retain the smoothness?
perhaps i dont need to use a timer in the variable switch method at all?

Cash Curtis II
19
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 15th Nov 2006 08:41
I measure the speed of everything in units per second, or units per millisecond to save on repetitive math. For a time to activate 60 times a second, you'd have to be running 60 FPS in the first place. You can set sync rate to that effect already. The real purpose of timer based movement and animation is to have the game run the same speed at 60 FPS or 20 FPS.

I posted a very detailed example in Code Snippets. I get a time stamp every loop, and I move and animate everything the amount that it should in the elapsed time. It's easy, and perfect.


Come see the WIP!
indi
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 15th Nov 2006 09:41
ill go check that out , thanks mate

indi
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 15th Nov 2006 09:45
actually, the search appears to return no result.
can you post the link mate.



BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 15th Nov 2006 10:02
Quote: "Would a variable switcher eg: first loop is even, 2nd loop is odd, third loop is even etc..
and perform a check every 2nd loop, would that increase speed but retain the smoothness?
"


That's what I, and I imagine a lot of other people use for the non-critical game components. I like to call it multiplexing, based on the way old telephone exchanges used to work

Generally I split the tasks between 6 cycles. In each block, I'll add different HUD updates, such as score, speed and whatever else doesn't have to be updated every cycle. SO at 60 FPS, my HUD is effectivley running at 10 FPS, which is fast enough. In addition, at least one HUD component changes every cycle so the user is oblivious to what's going on behind the scenes.

As to the timer itself, I agree with Cash.



Cash Curtis II
19
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 15th Nov 2006 10:24
I should say, I do that as well. Non critical components are set to run slower. I use 6 cycles as well, for some reason.(coincidence?) So, it doesn't have anything to do with timer based movement, but it does free up a little processing time.


Come see the WIP!
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 15th Nov 2006 10:32
Quote: "I use 6 cycles as well, for some reason.(coincidence?)"


Mmm...4 wasn't enough for me, 5 was "odd" and so 6 was the next available count that I was comfortable with .



Cash Curtis II
19
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 15th Nov 2006 11:05
Haha, that's awesome. I believe that's the exact same way that I came to 6.


Come see the WIP!
indi
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 15th Nov 2006 11:14
how about that link cash.

indi
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 15th Nov 2006 12:45 Edited at: 15th Nov 2006 13:29
Has anyone used what cash is talking about?
time as a keyword in the code snippets reveals nothing as well.

the keyword timer results in this one.
is this what your referring to cash>?

http://forum.thegamecreators.com/?m=forum_view&t=86368&b=6

david w
18
Years of Service
User Offline
Joined: 18th Dec 2005
Location: U.S.A. Michigan
Posted: 15th Nov 2006 13:36
I use timer based movement, and variable switching for things that are non-critical...I have yet to make anything worthwhile, but I am getting there and this helps out alot.
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 15th Nov 2006 14:41 Edited at: 15th Nov 2006 14:43
Put very simply...

Decide on your ideal FPS. Let's say 50 for simplicity.

MyFPS = 50

At the beginning (or end) of the main loop, get the last cycle duration and store the current time...

lastCycle = Timer() - LoopTime
LoopTime = Timer()


Now, you can factor everything you do by the loop time. Let's say that at 50 FPS, you want your man to walk 10 units...

Move Object MyMan, 10 * (MyFPS / (1000.0 / LoopTime))

So at 50FPS LoopTime is 20 ms and you get...

Move Object MyMan, 10 * (50 / (1000.0 / 20))
= 10 units.


At 100 FPS, LoopTime is 10 ms and you get...

Move Object MyMan, 10 * (50 / (1000.0 / 10))
= 5 units.




indi
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 15th Nov 2006 15:26
awesome batvink, I will have to try it for breakfast.
thanks man.

Cash Curtis II
19
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 15th Nov 2006 16:23
indi - Yeah, that's the one. I forgot it's just animation, but the principle is exactly the same. Frames Per Second, or Units Per Second. Batvink has given you some good stuff, although I predivide what I can, like this...

50 / (1000.0

Order of operations for multiplying and dividing won't change a think. So, using units and frames per millisecond is better than per second, because it saves you tons of calculations each loop.


Come see the WIP!
indi
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 16th Nov 2006 09:38
Ok, Lets say I wanted to perform a series of distance checks every 2nd or 3rd , 4th loop based on a timer?



BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 16th Nov 2006 09:48 Edited at: 16th Nov 2006 09:49




indi
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 16th Nov 2006 09:55 Edited at: 16th Nov 2006 10:05
no way! omg , that blows my mind but i get it, so simple yet elegant.
must test.


edit: obliterates the frame rate, I need to check something with a timer so it doesnt affect the overall fps loss, that was very choppy and now thinking about it, i would need a timer wrapped around the distance function to run and check every half second or 1/4 second.

whats the most effecient way to run some code via a timer with the sample above?

BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 16th Nov 2006 10:30
I would still put the Distance Check in the "Multiplexer", as it allows you to expand on this idea as your game grows. But you could also add a little more to the distance checking function. It will need a global variable, in this case DistanceTimer





indi
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 17th Nov 2006 02:17 Edited at: 17th Nov 2006 02:40
thanks heaps batvink, I had to stare at my eyelids for while before i could reply.
I will test for 1/8 second as well.

edit:
36 - 40 fps! woot! on such a crappy machine as well.
1/8 was better.
improves the fps, when used on one hit distance timers like potions spellbooks and so forth.

jinzai
17
Years of Service
User Offline
Joined: 19th Aug 2006
Location: USA
Posted: 17th Nov 2006 03:13 Edited at: 17th Nov 2006 03:28
indi...I use this code in nearly every program because you will need the actual timer value to stay clear of frame rate dependencies. I think that Cash was telling you that above, but...if you implement a loop counter, you will be locked to a different time base almost each frame, really. If you use the milliseconds elapsed since the last loop, you will free yourself from that dependency, and also...calculations based on time are closer to actual real-world physics, and definitely required for decent simulated physics, imo. As an added bonus, now your game is an alarm clock, too.



tick is a one second heartbeat. You can make them any size you want, and fire timed events from that code like tick does. You can also maintain a runtime counter with it. The code is far less obtrusive than you might imagine. The maximum penalty for use is only on the hour, and most times, its in, then out. timesegment is a particularly nice variable. I also count frames per second, which is not evident here, but...the more the game loop knows about elapsed time and frame numbers, the better equipped it is to perform tasks efficiently. That is a particularly good example of having smart data drive an essentially stupid program. I don't care if my program is stupid, as long as it does what I want it to do.

As to objections about timer(), I don't see them for a number of reasons. First of all, it is derived from Windows' GetTickCount(), or whatever its name is...the point is that it is not supposed to be a millisecond timer anyway, but...it is pretty accurate. Attempting to get better than that is missing the point on several fronts, imo...and not as feasible as you might think. Plus, you'll probably end up wasting time being so accurate.

Anyway, that is my 2p.

EDIT: - BatVink...can you just call timer() once in that loop? (It may be a tick different in the second call, after all, and it takes time. I get timesegment, which is the time since the last frame, at the same point in the frame. It should be valid for all calculations since the last frame.

One last thing : It is a multiplexer. The only "difference" is that most multiplexers divvy up time evenly, and we are not doing that, so this is a "time differential sequenced multiplexer."
Ralen
20
Years of Service
User Offline
Joined: 22nd Jul 2003
Location:
Posted: 17th Nov 2006 04:12
Indi,

What you could do for the even/odd loop thing is just multiply by -1

like this


loopcounter = 1


do

if loopcounter = 1

dostuff

endif


if loopcounter = -1

dostuff

endif

loopcounter = loopcounter * -1


loop


If you need more than odd even just add another variable

loopcounterb = loopcounterb + 1


then put loopcounterb = loopcounterb * 1 in one of your odd even loops than you can get your every 4th frame

basically you can keep adding variables.

just an idea.

indi
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 17th Nov 2006 07:35
thanks heaps guys
running timers around your main stuff modules sure does improve the giddyup's per second.
now i can base events like distance checks and button listen code on timed elements that don't require a check every loop

BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 17th Nov 2006 10:44
Jinzai, I agree with everything you say. I was laying everything out in the simplest terms here, rather than optimised.
In one of the newletters there is a tutorial that results in a module that allows unlimited timers running at different speeds, the ability to react to individual "ticks" or to accumulate ticks, and also to return real time differences in seconds.



jinzai
17
Years of Service
User Offline
Joined: 19th Aug 2006
Location: USA
Posted: 17th Nov 2006 19:43 Edited at: 17th Nov 2006 19:44
@BatVink - T H A N K S for the info. I downloaded the newsletters, but...have not read one word yet. My printer is busted, and I don't read too much on the computer while I am coding, I usually have too many open windows already. I'm not a big fan of online docs, unless I have a dedicated docs computer. I prefer ungainly stacks of printed paper all over, and lots of table space.

I thought that was the case about the snippet, and anyway...seen your code before, it rocks! I almost didn't mention it, but I had wanted to mention Cash's original post because it laid the system out perfectly. Like I said elsewhere, I have events and state machine logic that goes with it (as does everyone), I have not coded it yet. Too many other projects now.

@indi - Those events and state machine variables are being coded for the 1-Wire Weather Station project that HowDo is doing. When I am done, I will post them up in that, and also...I am going to make them available for study in a seperate source file. I am probably only poorly duplicating the work from the newsletter, however. Like Cash said, when you take on the time-based approach, you start to use time as a variable, and then you can eliminate most division with respect to time, which also saves cycles. FP division is still the single most expensive calculation in the x86 command set, I think. (It costs alot, anyway.)
indi
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 19th Nov 2006 00:12 Edited at: 19th Nov 2006 00:14
that sounds great jinzai, I have implemented the timer based approach now into a few main loop elements picking up the speed again by a few more fps.

the select case multiplexer can work but i had to leave out functions that were using the move object and turn object commands for much smooother gameplay, unless i timered those that was the only conversion.

however the timers gives you more freedom so i jumped on it.

I managed to finish the potion find/hold/use/drop mechanics and duplicated it for the rubbing crystals mechanics as well.
viva la array stacks for potion management!! god send.



my fps is around 37 it drops a couple when i snapshot the machine without any optimisation techniques. thats still good for this crappy old machine. it flys on faster systems. new machine set for jan 2007
I started to add the sounds or spfx mechanics for each item and event. I have to splice in the items and inventory after some cleaning and tidying, and then add the dark ai stuff later on when it arrives. Spells will really help with timers, some drain mana per tick, all of them have an animation timer.

Attachments

Login to view attachments

Login to post a reply

Server time is: 2024-04-23 14:50:58
Your offset time is: 2024-04-23 14:50:58