Search code examples
libgdx

Which is the more efficient way to position the camera on the sprite? Move the camera along on input or give it the sprite's position on update?


In libGDX, which is more efficient to position the camera on the sprite? Move the camera for the same amount on the input that moves the sprite or set the camera's position equal to the sprite's position on update?

I can't really tell if the input handler is more consuming than a regular get/set position.


Solution

  • Some trivial math that you do only once a frame is not worth thinking about. Moving a camera to follow a sprite only happens once a frame, so you should only be concerned with code clarity.

    If you have a large object (like SpriteBatch or a big array) to instantiate, you want to avoid doing that in the render loop because that is slow. You can instantiate it when creating something instead.

    It's good practice to focus on code clarity, not premature optimization. Only optimize if you actually have a frame rate problem. If that happens, the issue is usually something that happens in a big loop, like if you're cycling through 200 enemies to update them.