TimeDelta

For questions about using Classic.

Post » Tue May 12, 2009 1:10 am

I'm terribly sorry to have to bug you all about TimeDelta again, but i don't understand how i can use it to make my game run at the same speed, no matter what the refresh rate whatever monitor might be.

http://apps.sourceforge.net/mediawiki/construct/index.php?title=Time_Delta

I have looked here, and I don't understand it. I've never come across a situation as used in the example. I don't know what the distance the object is covering. I don't know how to translate that example into something that works the other way around.

I've rewritten this at least 10 times and i still can't figure out how to explain myself.

I would like my game to run the same on any monitor. As far as i know the 'always' command is out the window for this? And 'Every x ticks' too?


Always-- object Set Y to object +1

How would i rewrite what that does, with the magical 'TimeDelta'?

Somehow i need to replace 'Always' with 'TimeDeltAlways'.

So could i use (TimeDelta*60)-- object Set Y to object +1?

Ok, hope that is somewhat understandable, numbers mince my thought process(and frustrate the hell out of me). If i haven't been clear or I've left something out etc, please do ask for me to clarify and I'll do whatever i can.
B
9
S
2
G
4
Posts: 346
Reputation: 2,726

Post » Tue May 12, 2009 1:45 am

Think of it this way - how much do you want an object to move per second?

Like this. Want an object to move 100 pixels per second? Then use always, set x position to .x+(100*timedelta). Want it to be twice as fast? Use 200 instead.

You don't need to replace the always. Timedelta is a value that over the course of a second will add up to 1. So if you did: always set x to .x+(1*timedelta) then every second the sprite would be 1 pixel farther. Each frame it will move the fraction of a second the frame took to render. So if you're doing 60 frames per second, then it will move 1/60th of a pixel per frame. Does that make sense?
Moderator
B
87
S
32
G
33
Posts: 3,005
Reputation: 27,397

Post » Tue May 12, 2009 2:39 am

I think i get it, now i just need to figure out the correlation between +1 and the TimeDelta equivalent.

So when i have;

Always-- object Set Y to object +1

It's moving 1 pixel, every tick(FPS60), so that's 60 pixels a second.

With TimeDelta, +1 became 200*TimeDelta to get an approximately similar speed.

Is there a direct correlation, can i match all my +1s and +5s to a specific TimeDelta number?
B
9
S
2
G
4
Posts: 346
Reputation: 2,726

Post » Tue May 12, 2009 3:38 am

What you really need to do is get out of the habit of using .x+number - the more you do it, the easier it gets. You'll get used to it. :)

The correlation is the fps - if your .cap is running at 60 fps, then .x+1 and .x+(60*timedelta) should produce the same results. I'm not sure why you're getting x+(200*timedelta) at the same speed as .x+1. Is the application using v-sync, fixed or unlimited fps? You can check your fps by checking the 'show fps in caption' option in the application properties.
Moderator
B
87
S
32
G
33
Posts: 3,005
Reputation: 27,397

Post » Tue May 12, 2009 4:23 am

I figured that it was down to the FPS, but the whole 200 thing is throwing me off.

I have got the game in Vsync, and i have the FPS displaying all the time. My refresh rate is 60, but I have been changing it to look for differences.

I've just had a play around with it. I have one event set up

Always-- Rain:Set X to Rain X-(60*TimeDelta)
And one with
Always-- Rain:Set X to Rain X-1

I toggle one off and one on, and there is a drastic difference in speed. I have the Y set to 180 and 3 respectively and TimeDelta is in slow motion.

Now 200 was just an approximation but it's certainly closer to the original speed. From one to the other it seems that even 240*TimeDelta(4*60) isn't as fast as +1.

What do you think? :P
B
9
S
2
G
4
Posts: 346
Reputation: 2,726

Post » Tue May 12, 2009 5:25 am

fps = frames per second, f/s

timedelta = time elpsed per frame (in seconds), seconds per frame, s/f

1/ (60 [f/s]) = (1/60)[s/f]

if you were moving 1 pixel per frame at locked 60fps, in 60 frames you moved 60 pixels
Thus:
Always-- object Set Y to object +1
Becomes:
Always-- object Set Y to object +(60*Timedelta)

and this:
Always-- object Set Y to object +5
Becomes:
Always-- object Set Y to object +(5*60*Timedelta)

that is, assuming you were running at locked 60fps.
B
3
S
2
G
4
Posts: 1,445
Reputation: 4,665

Post » Tue May 12, 2009 5:33 am

There shouldn't be a discrepancy between + 1 and TimeDelta * 60 if your refresh rate is 60Hz. You should probably post your CAP so someone can take a look at it.

Anyways, here's a CAP I rigged up where there are two separate objects being moved using the two methods. There's only a slight discrepancy due to rounding errors.

TimeDelta example
Moderator
B
98
S
36
G
19
Posts: 1,584
Reputation: 17,717

Post » Tue May 12, 2009 6:01 am

Madster,
and if we assume it's V-sync?

What I'm trying to get is a game that runs the same speed no matter what the refresh rate is.

Arima has been helping push me towards this, and your run down has some nice formatting in it i will take on board, namely

Always-- object Set Y to object +(5*60*Timedelta)

Now, this would have the game running at 60 ticks per second no matter what the frame rate(well, if it was higher) or refresh rate was correct?


Linkman2004- Hey, cheers for the look in, I'm not on my computer at the moment, so i can't try it out just yet.

Would there be a discrepancy between + 1 and TimeDelta * 60 If my refresh rate wasn't 60? Because the whole reason behind this question is to take the refresh rate out of the equation.
B
9
S
2
G
4
Posts: 346
Reputation: 2,726

Post » Tue May 12, 2009 6:27 am

If your refresh rate was something like 75, like my main computer, then there would be a difference between + 1 and 60 * TimeDelta. To achieve the equivalent of + 1, it just becomes (your refresh rate) * TimeDelta.

So basically, if you want your game to have the equivalent of what + 1 is on your computer, but still run the same on everybody's computers regardless of refresh rate, you'd just use (your refresh rate) * TimeDelta
Moderator
B
98
S
36
G
19
Posts: 1,584
Reputation: 17,717

Post » Tue May 12, 2009 10:34 am

You don't need to change your monitor's refresh rate to test different framerates - change the framerate to 'Fixed' at 10fps in application properties, or set it uo 'unlimited' (which should reach a few hundred frames per second at least), and the difference will become obvious.

The problem is framerate dependent code (like set X to .X + 1) doesn't have a fixed speed (it depends on the framerate), and TimeDelta code does have a fixed speed. So how can you possibly convert between them? TimeDelta always means one speed, and without TimeDelta it could be any speed, depending on your framerate. So to convert to or from framerate-dependent, you have to pick a framerate as your reference, and 60fps is a common one so a good choice.

My advice, though, is to completely ditch framerate-dependent code. Forget it. Just always use TimeDelta, and get everything moving at the right speed using that. Don't bother converting between them, it's totally unecessary if you start making your game right off the bat with TimeDelta. Also, don't forget the built in behaviors are already framerate-independent, so you don't need to worry about them.

I hope that's clear, it can be a tricky area to explain! By the way, you should avoid the 'Every X ticks' event in games, because as the name suggests, it runs depending on the framerate (every 10 ticks is once a second at 10fps, but ten times a second at 100fps!). You always want to use 'real world' times, so use 'every X milliseconds'. TimeDelta is another way of using real world times, so with the two, your game should run at the same speed everywhere! :)
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,518

Next

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 3 guests