1
\$\begingroup\$

I'm new to libgdx and especially to using OrthographicCameras. I'm trying to understand how useful they are.

The question is, suppose I have a game map 500 units wide. I have an OrtographicCamera vith a viewport of 50x50. Now If I use SpriteBatch to draw texture of my game map as 500 units (I set projection matrix to cameras combined matrix) and move the camera around as the character moves, is this a right practice to do? If that map is 20000 pixels wide (an entire level for a platformer for example), is this really the way to go? Or should I make my camera and render logic myself (render only part of the map according to current coordinates of the character)? If I need to do this myself, what's moving the camera useful for?

\$\endgroup\$
  • \$\begingroup\$ "Or should I make my camera and render logic myself" - well, is drawing your game using the built-in camera logic as you described before working for you? Does it get the right game behaviour, with good performance on your target hardware, and readable code for working with? If so, then it sounds like you don't need to reinvent the wheel for this particular case. If that method isn't working for you though, you should describe the specific problem you're having with it so we can suggest ways to solve it. \$\endgroup\$ – DMGregory Jun 3 '18 at 20:02
  • \$\begingroup\$ dont worry about float precision until it becomes a problem \$\endgroup\$ – Khlorghaal Dec 24 '18 at 15:26
0
\$\begingroup\$

First of all, the camera provides the projection for the rendering pipeline. Without a camera you can't define how wide the view is.

Secondly, moving every part of the terrain separately is inconvenient compared to simply moving the camera.

\$\endgroup\$
  • \$\begingroup\$ This means I should use 20000 pixels wide map in above example? What about OpenGL if i do that? \$\endgroup\$ – Tom Milberg Jan 6 '18 at 22:10
  • \$\begingroup\$ @TomMilberg I didn't mention that anywhere. You should definitely break it up into parts and load them dynamically. \$\endgroup\$ – Bálint Jan 6 '18 at 22:30
  • \$\begingroup\$ Does libgdx camera do that or should i do that breaking up manually? \$\endgroup\$ – Tom Milberg Jan 6 '18 at 22:32
  • \$\begingroup\$ @TomMilberg manually \$\endgroup\$ – Bálint Jan 6 '18 at 23:33
0
\$\begingroup\$

Seems like for your use case , you should definitely use a camera. Not only will it save you the trouble of writing it yourself. The libgdx camera projection matrix is passed to the sprite batch and this "moves" your view.

Just render your map and player: move the player around and have the camera follow the player. Then when you are rendering simple call batch.setProjectionMatrix(yourcams_projection_matrix) before you call batch.begin().

Also dont forget that when you use a viewport you should update it whenever your game is rezied via the resize function provided in the Application interface/Adapter.

A last note: when you change your cameras position you must call cam.update() in order for the changes to take effect.

\$\endgroup\$
  • \$\begingroup\$ But what about rendering 20000 pixels wide map at once? What does OpenGL think about this? Does libgdx split it by itself? \$\endgroup\$ – Tom Milberg Jan 6 '18 at 23:05
  • 1
    \$\begingroup\$ you would have to cull the map. You could use libgdx TiledMap implementation which automatically does this. Implementing some fort of culling is not very difficult though, just calculate if an object is withing the camera and if its not then dont render it. \$\endgroup\$ – Jonathan Camarena Jan 7 '18 at 1:06
  • \$\begingroup\$ Well then, if it needs this manual work even with a camera, why i don't calculate if an object should be in current screen (using coordinates) instead of working with camera at all? \$\endgroup\$ – Tom Milberg Jan 7 '18 at 1:09
  • \$\begingroup\$ because without a camera you would constantly have to keep moving changing an objects position - even static ones. With a camera doing all the moving around all you have to do is check an object is in frame. Camera provides utility methods to do this. I hope you can see the use in it. \$\endgroup\$ – Jonathan Camarena Jan 7 '18 at 2:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.