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 ?

Author
Message
Philip
23
Years of Service
User Offline
Joined: 15th Jun 2003
Location: United Kingdom
Posted: 21st Sep 2010 15:15
Could I interject into this argument to request all the feuding participants to post their *ideal* code for their *ideal* solution. I realise code has been posted above but I detect from the development of the argument that some of that code might usefully be reconsidered.

If code is posted, that way the rest of us can at least see precisely each protagonist's solution and give it a test, and then wade in with views.

Cheer if you like bears! Cheer if you like jam sandwiches!
"I highly recommend Philip" (Philip)
KISTech
18
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 21st Sep 2010 20:55 Edited at: 22nd Sep 2010 18:05
This is the first code I posted in this thread, with some minor modifications.



To add smoothing by averaging the factors over a short amount of time. Change your function to this.



You can change the value of smooth to whatever you like to add more or less smoothing. You'll have to test it to see what gives you the best visual effect for your particular application.


Q: Why do you divide the number of milliseconds per loop by 1000.0?
A: When setting up a game you need to know what scale you will be operating in fairly early in the design so that models can be made the appropriate size, and speeds and distances can be set.

For example, if your model is supposed to move 10 DBPro units per second, then "MOVE OBJECT Obj, Speed * factor#" should give you that speed multiplied by the proper decimal representation of the number of seconds that have passed since the last move.

So if 14 milliseconds have passed since the last move, 14 / 1000.0 = 0.014 seconds and if Speed = 10 then in that time the object should move 10 * 0.014 = 0.14 DBPro units.

Multiplying that out we get,
1000 / 14 = 71.43 Frames per second
0.14 * 71.43 = 10.0002 DBPro units per second


Hopefully that clarifies things.

I don't think anyone has any dispute with the first code box above. It's standard among many other TBM examples I've found on the net for other game engines.

The smoothing was not my idea, but when I found it I tried it, and it smoothed out a problem I was having with jerky movement. Use it, play with it, change the smoothing value, and see if it helps. If not, pull the averaging code out and go with the original code.

sladeiw
17
Years of Service
User Offline
Joined: 16th May 2009
Location: UK
Posted: 22nd Sep 2010 01:21
Kistech, might want to check what variable `h` is for.

Also, is there a reason to do everything in floats? Why not divide once right at the end for factor# and save all the float additions etc. Or am I missing something. (Never having run your example)
Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 22nd Sep 2010 11:21
When I implement timer based movement, I tend to implement it from the start. The code becomes part of the main loop, like this:



Like KISTech, I also prefer to divide by 1000. That way "speed" becomes that much easier to grasp since it's in pixels/second or units/second. Very useful if you're programming a game where you use - let's say - 10 units for 1 meter.

Cheers!
Sven B

Philip
23
Years of Service
User Offline
Joined: 15th Jun 2003
Location: United Kingdom
Posted: 22nd Sep 2010 14:41
@KISTech

Thanks for posting your code. I do have a couple of observations. It seems to me, and maybe I am missing something, that you have at least three or four local variables in your function when I think you intend them to be globals. These include "flast", "h" and "t". Is "t" supposed to be "h" - I'm guessing this is a typo? Otherwise t isn't used at all and the function doesn't work.

Also, precisely what, if anything, is your function returning? Presumably nothing? I also assume that factor# is also supposed to be a global?

And shouldn't the "factors(smooth) = Diff# / 1000.0" line be in the for next loop? As presently written, only the array entry at factors(30) will update.

At the moment I'm reading your code as conceptual. I understand what you are trying to do. But your code doesn't actually work as presently written for the reasons set out above.

Cheer if you like bears! Cheer if you like jam sandwiches!
"I highly recommend Philip" (Philip)
KISTech
18
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 22nd Sep 2010 18:11
Quote: "Kistech, might want to check what variable `h` is for."


[face -> palm]
My bad. It's been corrected.

Quote: "These include "flast", "h" and "t". Is "t" supposed to be "h" - I'm guessing this is a typo? Otherwise t isn't used at all and the function doesn't work."


If you look at the upper code those are globals, and h was supposed to be t.

The second code piece variables are meant to be in addition to what is shown in the first one.

Quote: "And shouldn't the "factors(smooth) = Diff# / 1000.0" line be in the for next loop?"


No. The for next loop is moving all of the array elements UP one slot overwriting the first, and then the current value goes into the last slot (in this case 30).

The function doesn't "return" anything, but sets factor#, which is a global.

With the addition of the variables from the first code box, and the corrected typos, this should work fine now.

Philip
23
Years of Service
User Offline
Joined: 15th Jun 2003
Location: United Kingdom
Posted: 22nd Sep 2010 19:10
[face -> palm]

My bad. I only looked at the second code box, not the first. *embarrassed cough* Apologies!

Cheer if you like bears! Cheer if you like jam sandwiches!
"I highly recommend Philip" (Philip)

Login to post a reply

Server time is: 2026-07-22 14:20:19
Your offset time is: 2026-07-22 14:20:19