• Create Account

## Speeding up a game question

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1MarkusH82  Members

102
Like
0Likes
Like

Posted 21 January 2014 - 05:32 AM

Hi,

I am currently developing a simple Tower Defense game and would like to implement the functionality for the player to speed up the game.

First, I was thinking it is easy to solve by speeding up the the enemies and towers, but a simple equation showed me it is not that easy.

For example my enemy is standing at x=0m its speed is 10m/s and the time per frame is 1s (to make the calculation easier).

So after one frame my enemy is at x=10m   (pos = speed * time)

To double the game speed I would double the enemy speed, and would get the enemy position at x=20m after one frame.

Now, if I have a tower at x=10m with a range of 5m in both directions, then the enemy in normal speed would be in range. The enemy with double speed would not be in range. So, this shows me that just increasing the speed is not the solution for a time based game.

Does anybody has experience with that and could help me?

Best regards,

Markus

Edited by MarkusH82, 21 January 2014 - 05:34 AM.

### #2SimonForsman  Members

7586
Like
3Likes
Like

Posted 21 January 2014 - 05:34 AM

http://gafferongames.com/game-physics/fix-your-timestep/

if you use a fixed timestep speeding things up is simply a matter of increasing the tick rate.

I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

### #3MarkusH82  Members

102
Like
0Likes
Like

Posted 21 January 2014 - 05:40 AM

I was also thinking about making the game framerate based, but is there not also a solution for time based games? Is a fixed timestep not a problem if one computer runs faster then another?

### #4SimonForsman  Members

7586
Like
1Likes
Like

Posted 21 January 2014 - 05:45 AM

I was also thinking about making the game framerate based, but is there not also a solution for time based games?

not a good one since it won't be deterministic. you can multiply the elapsed time by x to speed everything up but it will change the simulation. (Fixed timestep does not mean that the framerate is fixed, only that you update the game state at a fixed rate)

I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

### #5richardurich  Members

1187
Like
1Likes
Like

Posted 21 January 2014 - 06:01 AM

The solution to this varies greatly depending on your implementation details. If you're using a grid, you can just force units through every square/hex of the grid even if they're moving 2 squares in one unit of time. This assumes you trigger from that in some way, shape, or form. If you're using geometric shapes and effectively no grid, you'd check for intersection between the line segment of the unit's movement and range of the tower. How long the tower fires (how much damage it does) needs to be calculated, but your existing code for that should require minimal changes to handle whatever solution you choose.

I have to ask, are you really writing a tower defense game where units move so fast that they pass an entire tower's range within 1 frame (~16ms at 60fps)? I cannot fathom a game where I'm expected to see action that fast. Usually, you cap the speedup a player can use well, well, well below that point.

### #6MarkusH82  Members

102
Like
0Likes
Like

Posted 21 January 2014 - 06:14 AM

@SimonForsman: Thanks for that article, just finished reading it. I will try the last solution with a fixed time step.

@richardurich: Of course no enemy will be fast enough to skip a tower range. My calculation was really just an extreme example, but it shows that even if the time per frame is small enough the gameresults can variate with different speed values. This is maybe just a small difference, but I would like to avoid it.

Edited by MarkusH82, 21 January 2014 - 06:17 AM.

### #7emcconnell  Members

940
Like
0Likes
Like

Posted 21 January 2014 - 01:03 PM

If you could grab the delta time between frames and multiply that by 2, it would give you a 2x speed. I guess it does depend on how your code structure is setup though

### #8L. Spiro  Members

24837
Like
5Likes
Like

Posted 21 January 2014 - 01:36 PM

POPULAR

I think you have done a poor job of describing your problem.  Most likely speeding up the game is irrelevant but you threw those words in there thinking of that as the only solution to whatever problem it really is that you are trying to solve.  This has led to you getting very…not-related-to-whatever-you-are-really-trying-to-do advice.

#1: I assume your example of a unit being in range of a tower based on how quickly it can run away to be the true indicator of whatever your real goal is.
If you speed up the whole game, nothing has changed.  The same units are still in range.  If you double game speed, you double the speeds of everything in the game.  That means the bullets, too.
Using your example, if you double game speed, the enemy may be able to cover twice the distance in the same amount of time, but the cannon ball will hit in only half the time (it too will be moving twice as fast), so the enemy still only has time to run 10 meters (assuming a cannon ball takes 1 second to hit normally, sped up to 0.5 seconds).

#2: But let’s assume I am wrong; you really do want to speed up game time (like in Sim City—let time pass more quickly for players) and your example was just a misunderstanding on your part of a hypothetical situation.
If you do want to speed up time, put a multiplier on your timer.  Change nothing else.  It will automatically cascade into having logical ticks called more often on fixed time steps and you can make it like Perfect Dark where the game speed is smoothly adjustable.  Increasing manually the tick rate is not the way to go for 2 reasons: #1, even if you use a fixed time step, passing a constant time delta leads to accumulated drift; you still have to calculate how much time has passed so that occasionally 1 extra microsecond gets added to your delta to account for truncated fractions of a microsecond that accumulate.  #2, if a fixed time step is implemented with a constant delta time, smoothly speeding up and slowing down your game is a bit of a pain and a hack.  If not, changing the rate of ticks will either just not work or it will cause instabilities in the physics and game simulations that fixed time steps are specifically meant to address.

Based on your wording it is really hard to know what you are really asking.
If you really want something like Perfect Dark smart slow-motion to smoothly speed up and slow down the game, use a multiplier on your virtual timer (the timer used to implement pausing.  If you don’t know what that is, search the site for “virtual timer Spiro”).  And then you don’t have to worry about the hypothetical range situation; the game logic and simulation will work exactly the same no matter how faster or slow you run your game.  All the math ends up exactly the same.

If your range scenario is the real problem you are trying to solve, and for some reason units can be sped up but tower bullets can’t, the solution to finding out of something is in range is still the same math.
If it takes 1 second for the cannon to go 20 meters:
inRange = ((unitSpeed * 1.0f) + unitStartingDistFromTower) <= 20.0f; // Uses speed (not velocity) and assumes the unit will run directly away from the tower as soon as the cannon is fired.
Otherwise if you expect the unit to continue its course (no matter what):
inRange = (((unitVelocity * 1.0) + unitPos) - towerPos).Len() <= 20.0f; // Assumes the unit keeps going straight while the cannon is in the air.

L. Spiro

### #9MarkusH82  Members

102
Like
0Likes
Like

Posted 23 January 2014 - 02:17 AM

@emcconnell: it's still the same problem. Doubling the delta time is the same effect then doubling the speed.

@Spiro: I don't think I did a poor job in describing my problem. It is simple math. And yes you are wrong. If I double the speed and the enemy skips the tower range within one frame then even doubling the bullet speed doesn't help, if there is no collision.

Anyway, I reached my goal with the article that Simon recommented.

### #10L. Spiro  Members

24837
Like
0Likes
Like

Posted 23 January 2014 - 02:19 PM

Anyway, I reached my goal with the article that Simon recommented.

It is impossible to say that I was wrong and that you reached your goal with that article.  These are mutually exclusive statements.

If you believe you have reached your goal with that article, you have a fundamental misunderstanding of the problem and the math behind it.  You just confirmed that by stating that doubling enemy speed has anything to do with frame counts.  What does a “frame” mean to a game?  What length of time is that?

16 milliseconds?  2 minutes?  32.3 days?

Of course you need a fixed time-step.  That’s true for anything you make.  That link alone is not the answer to your problem in itself.  It’s about how you speed up and slow down the game and how you manipulate the fixed time-step.  Read my reply again until it makes sense.

L. Spiro

### #11Trienco  Members

2555
Like
0Likes
Like

Posted 24 January 2014 - 11:36 PM

His original description sounds simple. Doubling delta time will effectively just double the speed, will double the distance traveled in one frame and will fail horribly if things are determined on a "snapshot" base. If enemy x starts the movement outside a towers range and ends the movement behind its range, the tower won't fire at it in the first place. Bullet speed never comes into play.

Keeping the simple approach can work, as long as all speeds and sizes/ranges are carefully designed and in combination with fixed time steps. It will still be inconsistent. Assuming the time step allows an enemy to move half a tower radius, it might start shooting anywhere from the enemy just barely having entered and the enemy already being halfway through. Not even fixing time steps can help with this (though aligning everything in a grid of carefully selected size could).

Personally I'd prefer to go with the extra math: you don't just check "is the enemy in range now", but "did it move through my range this update, at which point did it enter, when did the tower start shooting, where exactly did bullets hit the enemy during its movement, etc.". I like to believe that treating movement as stuff actually moving, rather than just teleporting around is somewhat important to have "proper" physics.

Of course, sometimes "simple and kind of good enough" is all you got time for...

f@dzhttp://festini.device-zero.de

### #12Strewya  Members

2604
Like
0Likes
Like

Posted 27 January 2014 - 08:21 AM

I'd say there's something fundamentally wrong and game breaking if units are fast enough to pass right next to the tower and avoid it's firing range in one frame. Either the time step is way too large, or the metrics for unit speeds/tower ranges are designed wrong.

Having a 16ms timestep, units should move a fraction of a meter, and not 40 meters (assuming a tower has a range of 20 meters). Maybe the design should be changed a bit so units don't move fast enough to skip towers?

Edited by Strewya, 27 January 2014 - 08:22 AM.

devstropo.blogspot.com - Random stuff about my gamedev hobby

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.