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 Discussion / Timer-based movement problem

Author
Message
gUESS
17
Years of Service
User Offline
Joined: 18th Feb 2008
Location: Looking for my mind. Have you seen it?
Posted: 18th Feb 2008 08:48
Hi,

I have got timer based movement working alright but the way I have it implemented I don't see how i can apply it to this situation.

I currently have it where it is just a variable that controls how much I increment or decrement certain values like this:



What I need is for it to apply to something similar to applying friction where I multiply a value every loop like this:



The only way I can see doing this is to check it with a timer every loop to see if a certain amount of time has passed before executing this statement.

Any help?
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 18th Feb 2008 12:32
What would you use time based movement for?

gUESS
17
Years of Service
User Offline
Joined: 18th Feb 2008
Location: Looking for my mind. Have you seen it?
Posted: 18th Feb 2008 23:13
I don't quite get what you mean but basically I want to have everything moving consistently no matter what the frame rate.

What I'm am trying to apply this to is something like curvevalue() which I use to simulate friction when ever I'm doing physics stuff. I do got something like a workaround where I just this calculation every set amount of milliseconds. The problem with this is that it can get out of sync if the framerate is low enough.
Ortu
DBPro Master
17
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 19th Feb 2008 06:13
the friction (or any speed adjustment) should be applied to the movement increment in a single formula. something like:

position=newvalue(position,direction,(speed*time elapsed since last check*friction)

speed=distance/time to cover distance i.e. 1unit per 50ms speed#=1.0/50.0

Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 19th Feb 2008 06:43
@guess

One thing you can do is program your game or physics demo to run at a certain FPS on your machine. You then create an adjustment variable that represents a percentage of your desired fps to the actual fps. Use this percentage to apply to positioning, rotation, etc of your objects. This will keep everything moving and rotating at relative distances but will be smoother the closer the actual fps is to your desired fps and more choppy the slower the actual fps is. Because it is a percentage, the movements should be pretty much the same visually.

In the following 2 examples, I have a cube that is supposed to rotate 2 units per refresh. I want the sync rate to be 60 FPS ideally. In example one, everything is fine, and the factor (adjment#) ends up being a little less than 1 because the actual fps runs at about 62. So the speed of rotation is 1.9something units per refresh.


Now in example 2, I add about 1000 spheres just to eat up FPS. On my machine, the actual fps drops down to about 30 but the ideal is still 60 so the cube's rotation gets calculated at about 4 units per refresh. When running this example, give it a minute to create all of the spheres.

You'll notice that the rotation of the cube appears at about the same except it's just a little more jagged because it rotates a little faster to compensate for the reduction in fps:


If you use the adjustment# variables on all objects in terms of their speed and/or rotation, your game should run at the same speed on every machine. The higher the machine's fps, the smoother it will be. The lower, the more jagged it will be.

Enjoy your day.
gUESS
17
Years of Service
User Offline
Joined: 18th Feb 2008
Location: Looking for my mind. Have you seen it?
Posted: 20th Feb 2008 01:07
Latch,
I already have something like that implemented and it works fine. The problem though is when I try to use my adjment# with something like:



Using the timer value (adjment#) on this doesn't work properly in any way I see.

My question was if there was any way of getting this to work properly aside from doing something like this:



The snippet here will only work properly if the frame rate is high enough (e.i. Actual_Frames_Per_Second >= syncTime).
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 21st Feb 2008 22:59 Edited at: 28th Mar 2008 21:20
First Off. Congrats! You're trying to go about it the Right Way.

Second - You should make a function that calls the CLOCK ONCE!

Third - The Timer Function Needs to Make Sure at least One Millisecond has passed before trying to change its "OLD TIME" and "NEW TIME" values. (Keeps math predictable.)
[edit]Ok.. Not Quite... Instead I think you should just set Time Elapsed to ZERO, BUT Keep your OLD TIME. When 1 or more milliseconds passes - THEN calc elapsed time from that. [/edit]

Forth - You should TRY to make Your Calc'd multipliers in One place (just for Organization - and partially for speed reasons as it prevents calc'ing stuff twice.

Ok...We In Dark Basic Classic? Hmm...


[edit]Code Change[/edit]

Hope that is helpful

gUESS
17
Years of Service
User Offline
Joined: 18th Feb 2008
Location: Looking for my mind. Have you seen it?
Posted: 22nd Feb 2008 00:02
Sorry if I might not be clear enough (or It might be me misunderstanding you guys) but I already have Timer-Movement working very well for normal movement. Its just that I don't really know how to apply it to something that works like a curvevalue. I have here an example program that might help.

jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 22nd Feb 2008 00:26 Edited at: 22nd Feb 2008 00:27
Ok - this isn't a double - post - there is a delay for the noob hahah

anyway - he said that ... Well.. My intpretation was - You misunderstood me GUYS - Timer movement isn't the Problem you idiots - Its I'm trying to do physics and get something like Curve Value!

LOL.... OH.... Yeah - Cool - I knew you were doing it right LOL

I ran into that - and I learned the importance of a "Heartbeat" in your game. If you make a heart beat - for your physics and stuff... you can USE Curve Value BECAUSE Heart beat you make your code only call at a specified (timer based) interval.... Like every tenth of a second.

Example - Physics engines sometimes use Herat beat" approach so TIME doesn't really effect the calculations because the time is a constant (usually.... enough where it doesnt matter)

Try it.

[edit] I hope people know I was joking and he didn't call anyone any names...he was most polite.[/edit]

gUESS
17
Years of Service
User Offline
Joined: 18th Feb 2008
Location: Looking for my mind. Have you seen it?
Posted: 23rd Feb 2008 06:56
That heart-beat thing sound like the way to go and I have toyed with it before.

I was kind of hoping to use some calculation based on the timerMove-variable but oh well.

The only problem I see with the heart-beat, game tick thingy is that if the frame rate lasts longer then however long the tick is supposed to be, that part of your game will start to go out of sync. Though in retrospect if that does happen, the frame-rate will be unacceptable and that should be the main priority.

Anyways, thanks for helping me out.

@jason
One more thing, how were you able to see my post before it got approved?
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 23rd Feb 2008 15:11
Email Notifications.

Either everyone get's em or I'm priveledged. I Doubt the latter.

One does need to click the "Recieive Email Notifications" when they post though to get email.

Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 23rd Feb 2008 18:23
@gUESS
OK, now I get it. After going through multiple timer based methods, it's not the movement, it's the gradual reduction in speed that is the problem.

So, say the car moves at 10 units per refresh at 50 fps. That means in theory it would move at 5 units per refresh at 100 fps. But applying .98*10 versus .98*5 doesn't produce the same slow down.

So the formula becomes 10*.98 = 5 * x
x = 1.96
But this doesn't make sense because that would actually make the car speed up so we have to use a different method (not multiplication of the friction coefficient alone) perhaps to slow the car.

The factor has to be time. We have to have a longer period of time to slow the car down when it's moving at less units per refresh. So we need a motion equation that takes into account time,veolcity and instead of just friction, acceleration.

s=(iv*t)+(a*(t^2))/2
where
s=the distance the car will move
iv=the initial velocity of the car
a=the decceleration
t=the time interval

Enjoy your day.
gUESS
17
Years of Service
User Offline
Joined: 18th Feb 2008
Location: Looking for my mind. Have you seen it?
Posted: 26th Feb 2008 23:30
Latch,

I think I see somewhat how that is supposed to work but I tried it and am confused on a few things.

How do find the value for a (deceleration)?
Is it based on the current velocity like velocity * 0.98?
Could it be a constant instead (negative/positive).

Also, what exactly is t (time interval)?
Is it supposed to be something like moveVar# from the example I posted earlier?

Please explain.
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 27th Feb 2008 00:01
I recently released Iron Infantry DBPro Demo + EXE + Source - and my Tanks and the Helicopters decelerate - and are based on timer movement. Seek it out (Or follow my sig) and view the source code - it might help....plus its fun to play.

Login to post a reply

Server time is: 2025-06-04 01:53:40
Your offset time is: 2025-06-04 01:53:40