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 DBPro Corner / Timer bases movement problem

Author
Message
Zokomoko
22
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 20th Aug 2004 15:17 Edited at: 20th Aug 2004 15:18
I've a timer based movment code.
It's been acting strange, and i couldn't find the problem.

It's so wierd because I've checked, rechecked,double checked,whatever.

This is how the movement code is like:
Move object 1,speed#*(timer()-speedtimer#)/1000
speedtimer#=timer()

Before the loop, i make speedtimer#=timer()

The code does these commands every loop, even if the speed#=0, so it will sync, not matter the FPS.
But then, if it's 10 fps (!), it moves MUCH FASTER, then 118 fps.
Which doesn't make sense, since if it were to not sync, the higher the fps the bigger the speed, logically.

Note: The speed# is always 20,000, and there's no acceleration.

Am i not implementing the timer based method correctly ?
Lost in Thought
20
Years of Service
User Offline
Joined: 4th Feb 2004
Location: U.S.A. : Douglas, Georgia
Posted: 20th Aug 2004 18:10 Edited at: 20th Aug 2004 18:13
() are your friend you can never have too many of them. Try this. Change sync rate to 0 and the object moves the same speed. I get 480 fps with rate 0 and 11 fps with rate 10 the cube moves the same speed.


[edit] Though it is odd as it shouldn't matter if those extra 2 () are there or not. Sometimes it runs fine without the extra () and sometimes it doesn't. I don't know why.

Zokomoko
22
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 20th Aug 2004 18:14 Edited at: 20th Aug 2004 18:24
The sync rate is 0.
I've changed it one time to 10, and one to 0, which I get 118.
This was to test if it was synced, and obviously it doesn't.

edit: what extra 2?
As i understand, one () should be before the movement begins, often before the big "DO". The second () should be after the movment took place.
Any more, will disrupt the sync, since it calculates how much time has passed since the last sync.
Lost in Thought
20
Years of Service
User Offline
Joined: 4th Feb 2004
Location: U.S.A. : Douglas, Georgia
Posted: 20th Aug 2004 18:26 Edited at: 20th Aug 2004 18:26
If you are running at 118 fps then it drops suddenly to 10 fps it will appear to be moving way faster but its actually moving farther each loop making it look faster when you are closer to it. Fluctuations in fps will make this worse. You might be better doing a rolling average time syncing. Spooky posted something like this ages ago.

The more averages you take the smoother the movement looks but the less accurate the movements are. This is the only thin I can think of that might be causing that. If it is programmed just like you posted.

Lost in Thought
20
Years of Service
User Offline
Joined: 4th Feb 2004
Location: U.S.A. : Douglas, Georgia
Posted: 20th Aug 2004 18:29
What I mean by extra 2 () is your line looks like this
speed#*(timer()-speedtimer#)/1000

and mine was this
speed#*((timer()-speedtimer#)/1000)

Though it shouldn't really matter as it works correctly sometimes without them. But for some reason it fails without them on occasion. I don't know why.

Zokomoko
22
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 20th Aug 2004 18:48 Edited at: 20th Aug 2004 18:56
I'll explain.
I've a ship that goes to warp. While in warp, there's a text showing how much time remaining before reaching target.
The text is working great, it shows the time.

The problem is, when the time reaches 0, it means it's suppose to be at the target location, but it does not.

I've come to the conclusion that the calculations for how much time is remaining were correct, and were not where the problem originates because :
(1) The time calculations uses simple distance,speed,time, equations.
(2) I've a text showing the distance between ship to target location.
When I manually set the sync rate to 10, I see the distance decreasing ALOT FASTER, like 2 times faster, then if I manually set the sync rate to 0, and get 118 fps approx.
Note: I set the sync rate before running the program, and not in-game.

(3) Even if It's just my imagination that the distance is decreasing faster I've noticed this:
There's approx 10,000 units between the ship and the target's location when time reaches 0 while in approx 118 fps.
There's approx only 1,000 units between the ship and the target's location when time reaches 0 while in 10 fps.

(4) I've manually made the calculations and got the same result.
Given the distance, it will take 21.2 seconds to reach it, in 20,000 units per second.

Those reasons tell me, that since the distance is always constant, and therefore cannot be the problem, and since the remaining time calculations cannot be wrong:
The speed which should be constant, is not, but i still can't figure out why.

edit : removing or adding the extra () didn't seem to help.
Lost in Thought
20
Years of Service
User Offline
Joined: 4th Feb 2004
Location: U.S.A. : Douglas, Georgia
Posted: 20th Aug 2004 19:26 Edited at: 20th Aug 2004 20:29
OK run this code.

with a sync rate of 0 I get reported 1030 fps and the following stats
Takes 21.295999527 secs to go 424159.96875 units (19917.354346868360540417662266038 units per second)

with sync rate 10 I get 11 fps and the following stats
Takes 21.1520004272 secs to go 424959.96875 units
(20090.769675076739542701251293401 units per second)

Looks pretty close to me but not perfect.
[edit] There is a plugin with a high resolution timer in it somewhere on the forums if you want to be more accurate. IanM made it. Link
http://www.matrix1.demon.co.uk/DBPro/downloads/Matrix1Utility04.zip

Zokomoko
22
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 20th Aug 2004 20:59
thank you so much.

good to know the reason for this, even better to know it's not my code's fault.

It makes sense, since the timer is only in milliseconds, that dealing with very large distances and speeds, it will be become greatly imprecise.

the dll will be very usefull, too bad i'm a bit dllphobic, i'll manage.

Lost in Thought
20
Years of Service
User Offline
Joined: 4th Feb 2004
Location: U.S.A. : Douglas, Georgia
Posted: 20th Aug 2004 21:03
Cool. I have been playing with this a bit more and it seems there are 2 problems causing the distance error. When you calculate the needed distance and say the formula comes up with 1.4999999 ... when you assign that to a float variable it will actually assign 1.49999988079 to it becuase of the rounding from using floats. And the other problem was the timer.

Zokomoko
22
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 20th Aug 2004 21:09 Edited at: 20th Aug 2004 21:40
so fixing timer will still leave the problem you mentioned.
assigning the value to a float is not good, is there a variable type that can hold a sufficient amount of data like that value you specified ?

Edit: sorry for bugging ya, but i've DBPRO trial version, does that mean i can't use the dll ?

Never mind, i got it to work
Lost in Thought
20
Years of Service
User Offline
Joined: 4th Feb 2004
Location: U.S.A. : Douglas, Georgia
Posted: 20th Aug 2004 21:27 Edited at: 20th Aug 2004 21:27
Yes use.


With double floats it will be more accurate but still a very small amount of loss from rounding and the results are slightly better.

with a sync rate of 0 I get reported 1020 fps and the following stats
Takes 21.2660007477 secs to go 424019.96875 units (19938.86739 units per second)

with sync rate 10 I get 11 fps and the following stats
Takes 21.1509990692 secs to go 424820 units
(20085.10324 units per second)

Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 20th Aug 2004 21:52 Edited at: 20th Aug 2004 21:53
Sorry to barge in late on this thread - this may be important in your case or it may not be, I don't know..... but I would suggest always set the timer (speedtimer=timer()) as the very last thing before the do-loop.

If you set the timer, then make some objects or load some images etc., then start your timer loop, then there will be a delay which might affect the time values - depending on what you use them for.

By the way, this is the method I use .... it's probably the same in essence as what's above so ignore it if what you have already works.

CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 20th Aug 2004 21:52
Sorry if this was mentioned above but the one thing jumping out at me from the code in the first post is the fact that you are mixing floats and Timer() - use integer with timer()


DBP_NETLIB_v1.4.3 - 65 FREE Functions * Click Logo
Zokomoko
22
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 20th Aug 2004 22:57 Edited at: 20th Aug 2004 23:33
i'm testing it, i'll let you in if it worked


Time, thanks for IanM's dll, is calculated by the billionth of a second. Still, it's exactly as it were last time.
doesn't make sense at all.
Lost in Thought
20
Years of Service
User Offline
Joined: 4th Feb 2004
Location: U.S.A. : Douglas, Georgia
Posted: 21st Aug 2004 01:54 Edited at: 21st Aug 2004 11:02
Yeah I was really sleepy this mornong. Like Ric said you are loosing loop time in your calculations as well. You need to compare the loop time as close as you possibly can to when you set the timer. Something like Ric's example. I'll try and see what I can come with when I'm home and awake this evening. Unless you get it working first.

[edit] There are still losses somewhere. But here is as close as I got without recalculating the distance left vs time left before 21.2 secs is up. It is really jumpy when I recalculate but it does get closer to the object.

The only loses I can see are from this point in the code

this line speedadj# = (endtimer# - begintimer#)/1000 id being executed without affecting the lop time so it is causing somewhat of an error. And the double floats though very accurate lose a little accuracy but I don't think its enough to cause this.

Zokomoko
22
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 21st Aug 2004 10:58
This is my code compared to ric's :

starttime#=timer()
oldtime#=starttime#

do
currenttime#=(timer()-starttime#)/1000
changeintime#=currenttime#-oldtime#
oldtime#=currenttime#

move object [object],speed#*changeintime#

loop

I added the oldtime#, so it will prevent it from going really fast the first time, as without it, the oldtime# was 0.
Lost in Thought
20
Years of Service
User Offline
Joined: 4th Feb 2004
Location: U.S.A. : Douglas, Georgia
Posted: 21st Aug 2004 11:24
OK I think I would just cheat on this one as I have to go to sleep.

with 10 fps it takes me 21.23999 secs to reach the ships position
60 fps it takes me 21.20999 secs
1020 fps it takes me 21.20000 secs to reach it.
I think the sync rate is affecting the calculations as sync rate 0 is perfect timing and really close position.

Zokomoko
22
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 21st Aug 2004 11:45
Thx for the help, i'll try to solve it a different way.

Login to post a reply

Server time is: 2024-11-27 07:55:41
Your offset time is: 2024-11-27 07:55:41