-1

I have recently asked a question here about programming a game so it will be efficient and won't lag.

I have made a game that lags like hell. I suspect that the way that I programmed it's game-loop, could be the source of the problem.

I want to describe to you in general the situation in my game, and then present my way of programming it's game loop.

It's a game for two players. Each player controls a tank. Each tank can shoot missiles. Each tank can collect 'gifts' from the field.

  • Each missile of tank1 can collide with tank2, and each missile of tank2 can collide with tank1.
  • Each missile can collide with a boundary of the screen.
  • Each tank can collide with the other tank.
  • Each tank can collide with 'gifts'.
  • All missiles constantly move.
  • Tanks move when specific buttons on the keyboard are pushed.

My game loop (Each stage is inside a method called from the game loop):

  1. Loop through all the missiles on the screen, and update their location (move them).
  2. Loop through all of tank1's missiles, and for every one check if collides with tank2.
  3. Loop through all of tank2's missiles, and for every one check if collide with tank1.
  4. Loop through all the missiles on the screen, and check if collide with screen boundaries.
  5. Check if specific keys are pressed on the keyboard. If so, move tanks.
  6. Check if the two tanks collide.
  7. Loop through all of the 4 'gifts' on the screen, and check if they touch a tank.

Questions:

  1. This game-loop is probably inefficient. How inefficient is it? A little, or a lot?
  2. How can I improve the game loop and/or the way it's methods work? How can I achieve the same thing, more efficiently?
  3. How likely is it for a game-loop to be the main cause for poor performance level? How common is it in games for this to be the source of the problem?

Appreciate your help.

user3150201
  • 1,901
  • 5
  • 26
  • 29
  • Show code. And yes your game loop can definitely be a bottleneck. Refactoring your code can give a surprising performance benefit. For me, it was something as simple as changing all `for in` loops to `for i = 0` loops in C#. –  Jan 25 '14 at 01:59
  • it would be nice to have this tagged with the language in question – user3125280 Jan 25 '14 at 02:16
  • @user3125280 I'll tag it, but is it relevant? I think this question isn't really language specific. – user3150201 Jan 25 '14 at 02:18
  • @user3150201 probably not, but it is specific to the framework, etc. Maybe you are using some library, etc or a language not suited to this kind of thing. – user3125280 Jan 25 '14 at 02:19
  • I'm using Java, it should be suitable. I don't want to tag it as Java because I don't want only Java programmers to look here. – user3150201 Jan 25 '14 at 13:41
  • @remyabel Probably a silly question, but what do you mean by 'refactoring'? And what is a `for in` loop? Do you mean loops that scan an entire array or list? – user3150201 Jan 25 '14 at 16:46

1 Answers1

0
  1. I wouldn't necessarily say it's inefficient since you're just giving us the general idea of what might be done in several other games. It sounds like all you're doing is moving objects and checking for collisions, which I'm assuming uses AABB collision detecting. Nothing really expensive here. Of course it has room for improvement. See #2.

    • You're probably getting input more than once, so what you could do is get the input at the beginning of each frame and store it in some way. I don't know what form of input you're using, but if you're using a keyboard for example, you'd get and store the state of the WASD keys and check those states when handling tank input.

    • The most notable optimization you could make is you loop through every missile 3 times. The first time is to move them, the second to check their collisions, and the third time to check if they are outside the screen. In one loop, you can move them, check if they're outside of the screen, and then check if they collide with the opposing tank. I recommend doing it in this order, because if the bullet does happen to move outside the screen you can avoid doing an unnecessary collision check.

    • Finally, you only have to check if the two tanks collide when they move, and you only have to do it once. If you need it so something happens for every frame the two tanks are on top of each other, you can store the state of the collision and you only need to update it again when one tank moves.

  2. It really depends. You're probably locking the frame rate by sleeping, so you could be sleeping for too long. Try to remove your method of frame rate limiting and seeing what happens.

John
  • 16
  • 2