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\$
2
  • \$\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\$ Commented Jun 3, 2018 at 20:02
  • \$\begingroup\$ dont worry about float precision until it becomes a problem \$\endgroup\$ Commented Dec 24, 2018 at 15:26

2 Answers 2

1
\$\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\$
4
  • \$\begingroup\$ This means I should use 20000 pixels wide map in above example? What about OpenGL if i do that? \$\endgroup\$ Commented Jan 6, 2018 at 22:10
  • 1
    \$\begingroup\$ @TomMilberg I didn't mention that anywhere. You should definitely break it up into parts and load them dynamically. \$\endgroup\$ Commented Jan 6, 2018 at 22:30
  • \$\begingroup\$ Does libgdx camera do that or should i do that breaking up manually? \$\endgroup\$ Commented Jan 6, 2018 at 22:32
  • \$\begingroup\$ @TomMilberg manually \$\endgroup\$ Commented Jan 6, 2018 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\$
4
  • \$\begingroup\$ But what about rendering 20000 pixels wide map at once? What does OpenGL think about this? Does libgdx split it by itself? \$\endgroup\$ Commented Jan 6, 2018 at 23:05
  • 2
    \$\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\$ Commented Jan 7, 2018 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\$ Commented Jan 7, 2018 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\$ Commented Jan 7, 2018 at 2:13

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.