-3

I'm learning basic frame independent movement, but I have a necessary need in the implementation.

Right now, I'm searching for C++ code, but I need a complete example inside a GameLoop that allows an increase in speed. Not a gradual increase, but just the ability to move my sprite faster. I have to translate the C++ code to vb .net since there are not many examples. So let me be clear about this ->

I have a simple GameLoop. In the GameLoop, I have two calls to functions

Update() Render()

I understand that for the Update() function I should put a deltaTime parameter. So it should be

Update(deltaTime As double)

Since this is how most examples on the web are shown, I just copied the idea but I don't have an actual frame independent setup yet.

How can I set this up and call the Render function.

Please keep in mind, that if it works, that's fine - I cut and paste a different example like that before, but there was no way to increase the speed of the moving sprite. I don't even know where to begin with this?

Please note if you only have C++ code, I will do my best to translate it, so for me it's acceptable.

2 Answers2

0

That deltaTime idea is to allow your game to run in a way where the perceived speed of the gameplay is constant no matter how fast or slow the machine and environment are that are running the game.

You might notice if you run some poorly coded games designed for old hardware on hardware much newer that the gameplay goes way too fast and becomes unplayable on those fast machines without deliberately slowing things down. There are actual slowdown utilities just to deal with that problem, like Mo'Slow and Cpukiller. This is because these old games were coded in a way where things were being changed by a constant amount every frame instead of a variable amount based on the time that has passed.

So to avoid this problem, you move all your sprites each frame by numbers that are multiples of deltaTime. That'll give you the sense of consistent speed regardless of the speed of the machine/environment.

Now if you want an adjustable game speed on top of that where you want to allow users to deliberately make the game faster, then you introduce a second variable on top of deltaTime that you also multiply by, like gameSpeed. So your sprites should move in Update by a multiple of both deltaTime and gameSpeed.

0

I wont give you code, cause I'm positive that you will be able to fix this with a proper description, but I will gladly explain how to move your sprite without frame rate dependency.

As you said, you already pass a delta-time in to the function.
The delta time is the time that it took between now and the last time the function was called (a frame).
When you move your sprite, you probably move it by a specific number of pixels, instead of doing this every frame, you should multiply the pixel range with the delta time.

This will make into a quite simple formula:
DetlaTime * TheNumberOfPixelsToMoveInASecond.
If you mul it with 10, then the sprite will move 10 pixels in one second. Mul it with 100, it will move 100 pixels in one second.

Jite
  • 5,761
  • 2
  • 23
  • 37
  • Here is my code, but the sprite just slowly speeds up, plus there is a slight stutter and I don't know how to adjust the speed. – compchip May 03 '15 at 15:38
  • `x += x * (2.0F * ft.AsSeconds)` Each frame, you increase the x value with `x * (2.0f * ft.AsSeconds)` This will speed up the sprite a whole lot. Instead try: `x += (2.0F * ft.AsSeconds)`, i.e., only add the value it should move (I would rather use milliseconds instead of whole seconds too). – Jite May 03 '15 at 15:46