Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411507 Posts in 69374 Topics- by 58429 Members - Latest Member: Alternalo

April 26, 2024, 12:53:20 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Timestep in platformer game?
Pages: 1 [2]
Print
Author Topic: Timestep in platformer game?  (Read 3716 times)
Netsu
Level 10
*****


proficient at just chillin'


View Profile WWW
« Reply #20 on: December 09, 2014, 02:47:24 PM »

I see this is an old thread and the OP might not see it, but I'm really surprised that so many people posted here and no one (as far as I see) noticed it.

Code:
if (SPACEBAR is pressed)
    yVelocity  = yVelocity + (JUMP_SPEED * Time.deltaTime)

Why do you use deltaTime here at all? Judging from the rest of the code, you only want to apply a jump impulse when the player hits the space bar. It's an instantaneous force impulse that is constant and independent of any time factors, so it shouldn't take deltaTime into account.

Deltas should be used when you have an effect that applies for a period of time, like the force of gravity, or accelerating while moving sideways.
Logged

Polly
Level 6
*



View Profile
« Reply #21 on: December 09, 2014, 07:21:38 PM »

The problem at hand is that your delta step is dynamic. This is a basic problem that a lot of gamedevs encounter because most tutorials will simply teach them to use dynamic delta times. However, it is fundamentally flawed for anything physics and/or logic related.

No .. in that case you're simply doing it wrong / using incorrect math. Here's a graph showing the y-position over time ( x-axis ) of the jump behavior in my game at different frame rates / delta time intervals ( rendered using OpenGL ).



As you can see the coordinates converge at the exact same point ( when the frame rates converge as well ).
Logged
ThemsAllTook
Administrator
Level 10
******



View Profile WWW
« Reply #22 on: December 09, 2014, 08:27:51 PM »

No .. in that case you're simply doing it wrong / using incorrect math.

The problem is that there's no such thing as correct math for this on computers with finite numeric precision. No matter what you do, if you have delta time in your calculations somewhere, your game will be nondeterministic. Take a look at your superimposed graph and imagine there's a collidable object intersecting the red line but not the blue one. Sure, you could complicate your collision code with calculations for the path as though it were curved, but that just pushes the problem deeper rather than actually solving it...rounding errors will still cause nondeterministic behavior between different time deltas.

You can hypothetically get things to behave well enough that it's impossible for a player to notice the difference if you're not doing anything that requires deterministic behavior, but that's not a sacrifice I'd take lightly. Determinism is awesome. With a deterministic fixed timestep simulation, it's trivial to implement a system that records inputs during gameplay, replays the session just from that data, and gets identical results. Apart from being a cool novelty, this is useful for losslessly exporting game footage (replay an input session and write each drawn frame to disk as a PNG or whatever), creating a 100% reliable test case to reproduce any bug that's ever encountered, and various other things.
Logged

Polly
Level 6
*



View Profile
« Reply #23 on: December 10, 2014, 04:01:25 AM »

With a deterministic fixed timestep simulation, it's trivial to implement a system that records inputs during gameplay, replays the session just from that data, and gets identical results.

Wouldn't consider it trivial .. but you can simply record the delta time along with the input.
« Last Edit: December 10, 2014, 04:17:33 AM by Polly » Logged
Netsu
Level 10
*****


proficient at just chillin'


View Profile WWW
« Reply #24 on: December 10, 2014, 04:03:38 AM »

I don't get what you're saying, it's not trivial but it's simple?
Logged

Polly
Level 6
*



View Profile
« Reply #25 on: December 10, 2014, 04:18:10 AM »

I don't get what you're saying, it's not trivial but it's simple?

There should have been a "but" in that sentence Smiley
Logged
ThemsAllTook
Administrator
Level 10
******



View Profile WWW
« Reply #26 on: December 10, 2014, 11:51:09 AM »

With a deterministic fixed timestep simulation, it's trivial to implement a system that records inputs during gameplay, replays the session just from that data, and gets identical results.

Wouldn't consider it trivial .. but you can simply record the delta time along with the input.

True enough, you could do that and at least get the bug reproduction benefits. It wouldn't be as ideal for exporting gameplay footage, since the performance of the particular session you were recording would be irreversibly encoded in your playback data, but I suppose that's up to most people's standards.

I feel like there are a lot of cool things you could do to make networked multiplayer gameplay work better if you know everyone will be perfectly in sync given the same inputs, but I can't speak from experience on that yet...need to get around to trying it.
Logged

J-Snake
Level 10
*****


A fool with a tool is still a fool.


View Profile WWW
« Reply #27 on: December 10, 2014, 12:20:54 PM »

Use visual interpolation and it will look smooth. Google what it is, the idea is also explained in that gafferon article.
EDIT: I see you are using Unity. In that case you are out of luck with my suggestion, I guess.

Sorry to reply this late and but I think this statement is wrong that Unity can't do interpolation talked in gafferon article.
I've only tried Unity so I'm no expert but I think you can do this in Unity if you are not using Unity's physics.


Just add script and make it's priority highest so that it's Update is called before any other script.
When it's Update is called create your own fixed update loop and accumulator using delta time.
Make this script call your own fixed update, let's say called FixedUpdate2 in all scripts you have registered to be called by this script (maybe create script base class that registers and unregisters script automatically), many times as needed.
Then use your own accumulator to do the interpolation in LateUpdate.

Problem solved.

Have you checked out how reliable and practical that is. 

 
Logged

Independent game developer with an elaborate focus on interesting gameplay, rewarding depth of play and technical quality.<br /><br />Trap Them: http://store.steampowered.com/app/375930
Netsu
Level 10
*****


proficient at just chillin'


View Profile WWW
« Reply #28 on: December 10, 2014, 12:48:04 PM »

I feel like there are a lot of cool things you could do to make networked multiplayer gameplay work better if you know everyone will be perfectly in sync given the same inputs, but I can't speak from experience on that yet...need to get around to trying it.

I can tell from experience that I can't even begin to imagine how painful it would be to make a networked real-time game with variable timesteps...
Fixed timestep is pretty much a must, with 100% deterministic movement you only need to send time stamped inputs between the players and the server.
Logged

wanton
Level 0
*


View Profile
« Reply #29 on: December 10, 2014, 02:26:44 PM »

Use visual interpolation and it will look smooth. Google what it is, the idea is also explained in that gafferon article.
EDIT: I see you are using Unity. In that case you are out of luck with my suggestion, I guess.

Sorry to reply this late and but I think this statement is wrong that Unity can't do interpolation talked in gafferon article.
I've only tried Unity so I'm no expert but I think you can do this in Unity if you are not using Unity's physics.


Just add script and make it's priority highest so that it's Update is called before any other script.
When it's Update is called create your own fixed update loop and accumulator using delta time.
Make this script call your own fixed update, let's say called FixedUpdate2 in all scripts you have registered to be called by this script (maybe create script base class that registers and unregisters script automatically), many times as needed.
Then use your own accumulator to do the interpolation in LateUpdate.

Problem solved.

Have you checked out how reliable and practical that is.  

I did my own fixed update loop with interpolation like this when I was trying out Unity, it worked fine.

Just add delta time to your accumulator and call your custom fixed update in your registered scripts as many times there is more or equal time left than your fixed timestep.
You need to expose your accumulator or already calculated interpolation factor for your registered scripts to do interpolation.

Also Time.deltaTime will have Update's delta time and not your fixed timestep delta time so you need to take that into a account.
And make sure your custom fixed update caller script's priority is highest so it's Update is called first, so that your custom fixed update is called before any other script's Update is called.

Minus is that this won't work with internal physics because physics are updated after every FixedUpdate and your custom accumulator won't be in sync with Unity's own fixed update accumulator.

If you don't use internal physics you can do your own fixed update with interpolation this way.

Just create two scripts something like this

InterpolatedFixedUpdateCaller.cs
- Set priority to highest
- Exposes list of InterpolatedFixedUpdateBase classes for registeration or Register and Unregister methods.
- Exposes interpolation factor to lerp stuff like movement and rotation.
- Calls your custom fixed update in all InterpolatedFixedUpdateBase classes.

InterpolatedFixedUpdateBase.cs
- Base class for scripts you want have custom fixed update for.
- Also use this class to auto register script for calling by InterpolatedFixedUpdateCaller class.
- Can't remember which Unity events I used to do the registeration and unregisteration, I think they where "OnEnable" to register and "OnDisable" to unregister.
« Last Edit: December 10, 2014, 03:07:30 PM by wanton » Logged
J-Snake
Level 10
*****


A fool with a tool is still a fool.


View Profile WWW
« Reply #30 on: December 11, 2014, 05:50:34 AM »

Ok, cool if Unity allows you to do that. Might help out those who are doomed to use it.
Logged

Independent game developer with an elaborate focus on interesting gameplay, rewarding depth of play and technical quality.<br /><br />Trap Them: http://store.steampowered.com/app/375930
Pages: 1 [2]
Print
Jump to:  

Theme orange-lt created by panic