In contrast to the comments saying that this is a good way to do it, I don't think so.
It is not intuitive to move every entity in your world. When you run around through a forest in real life, the trees also don't move around you, but only you move.
Use an OrthographicCamera
and change its position. When rendering via a SpriteBatch
, draw all bodies exactly on the position where they are (probably with some METER_TO_PIXEL scaling factor) and use spriteBatch.setProjectionMatrix(camera.combined)
on your SpriteBatch
. This way only the visible bodies will be drawn automatically, since the camera transformation will get rid of all non-visible bodies.
And another hint: Try to avoid Body.setTransform()
. Instead try setting linear velocity or apply forces/torque on the bodies. Using setTransform
causes non-physical behaviour since it's basically like teleporting and might lead to weird effects and in my case sometimes even bugs.