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.

Newcomers AppGameKit Corner / Creating a time based counter related to a changing variable

Author
Message
NeedHalp
8
Years of Service
User Offline
Joined: 20th May 2016
Location:
Posted: 2nd Jun 2016 06:40
I have a float variable called accerleration which is used to determine how fast my distance counter counts. Accerleration will never be 0 but closer it is to 0, the slower this counter counts up and higher it is, the faster it counts. I know that you could do something like this:
[/code
if (accerleration < 0.1 and accerleration > 0)
if time > 0.5
count slowly
endif
resetCounter
endif]

But the problem with this is that I have a code like this:
[code if time > 0.1
//* Handle accerleration *
HandleAccerleration()
ResetTimer()
endif]

And I need to have something like this because I need my accerleration to update every .1 second. I am resetting the timer every single time I update the acceleration which doesn't give the timer enough time to go up to .5 second where this can update my counter. Is there anyway for me to have two separate timers that are independent of each other? If so, I could use one particular timer for the accerleration update while I could use the other one for the counter update.
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 2nd Jun 2016 12:35
You shouldn't ResetTimer() every cycle.
You can store the timer value in a variable, and use this as your base for the next calculation.
Also, the way you are doing it would not update every 0.1 seconds. It will update every 0.1 seconds plus the time beyond 0.1 that you captured it at. This becomes more inaccurate if you reset the timer each time, instead of using a constant base value.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
NeedHalp
8
Years of Service
User Offline
Joined: 20th May 2016
Location:
Posted: 2nd Jun 2016 19:53
Thank you BatVink! It worked perfectly. But it seems to me that incrementing the nextUpdate is a bit slower compared to resetting the timer each time. The counter counted slower compared to resetting the timer. Since the differenceis miniscule, it really doesn't matter though. Any idea why that might be?
Dybing
13
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 2nd Jun 2016 23:42
It's slow because you're using floats. I avoid floats like the plague when I can. There is a solution though, you can use getMilliseconds() instead, which returns an integer. So the resulting code would look like:



Another thing regarding floats, they really aren't. It is just some jiggery-pokery done to integers that eats up a ton of clock-cycles. In particular when doing operations on two or more floats that have different precision (ie where the decimal point is). So if something is time critical - floats is out the window. You're better off working from integers. AppGameKit being at least 32 bit you can count up to about 2 billion (assuming all AppGameKit integers are signed) before you'll potentially get into trouble. Unfortunately, there is no way to set the bit length of integers in AppGameKit - or whether they can be signed (use negative values) or not.
Jeff Miller
19
Years of Service
User Offline
Joined: 22nd Mar 2005
Location: New Jersey, USA
Posted: 3rd Jun 2016 01:05
Are you sure you want to increment distance with acceleration rather than velocity?
NeedHalp
8
Years of Service
User Offline
Joined: 20th May 2016
Location:
Posted: 3rd Jun 2016 02:33
Ah I see. Thanks for the reply!! And you're right, it should be velocity technically speaking.

Login to post a reply

Server time is: 2024-11-23 22:50:38
Your offset time is: 2024-11-23 22:50:38