Say I have a
GameObject class that is being extended by all of the objects in the game that are moving around. (This class provides basic stuff like a sprite, draw method, update method with application of move vector and so on...) I also have a class
Asteroid that like I said extends
Then there are two versions of the game:
Version 1: Somewhere in the game there is a Collection that holds all of the
Asteroid-objects. The update Method is something like this:
for(Asteroid a : asteroids) a.update(...)
Draw method goes equivalent.
Version 2: I decided to put all of my
GameObjects in a single collection, so that all moving stuff is being handled in one single loop like this:
for(GameObject o : gameObjects) o.update(...)
Again, draw method goes equivalent.
The problem I am facing with version 2 is that the game loop basically takes twice as long. (The update method as well as the draw method.) I am astonished at the draw method, since in either version there is the exact same amount of object being drawn. So my question is: Does this abstraction in version 2 affect the game's performance in any way? (I can imagine that version 2 requires the JVM to alway cast the
GameObject implicitly to it's specific type which it does not have to do when the type is already given like in version one, but I am not sure about that.) (I am using Java with LibGDX if it matters)