0
votes

I'm making a 2D side scroller game with Scala & libGDX. My problem is that I want to have a fixed room size, say 4000px * 720px.

I want the window size to be 1280px * 720px and I need to have a orthographic camera that I can move and display the correct part of the "room"/"map". The problem is how I can set the room size to be that specific size and then use orthographic camera to display a part of it?

Currently I'm trying to use this tutorial: https://github.com/libgdx/libgdx/wiki/Orthographic-camera

I can set the window size when I launch the program like this:

object DesktopLauncher {
    def main(args: Array[String]) {
        var config: LwjglApplicationConfiguration = new LwjglApplicationConfiguration
        config.foregroundFPS = 60
        config.width = 1280
        config.height = 720
     new LwjglApplication(new Controller, config)
    }
}

And I can set the camera size like this:

val h: Float = Gdx.graphics.getHeight()
val w: Float = Gdx.graphics.getWidth()

cam = new OrthographicCamera(100, 100 * (h / w))
cam.position.set(cam.viewportWidth / 2f, cam.viewportHeight / 2f, 0)

But how can I specify the camera and room size to be the size I want in pixels?

1

1 Answers

0
votes

If I understand your question correctly, you want your camera to stop displaying black bars when you get to the edge of the room (Stop scrolling at the edge of the room). I assume you are using a Viewport. (You should be) Here's how I'd do it:

You'd need to use Viewport#unproject() to project screen coordinates to world coordinates. You'd project your window width to world coordinates, let's call this variable camWorldX. Before moving the camera you'd check if camWorldX + 1 < roomWidth. If it is true move the camera, else, don't. For negative directions, unproject 0 and check if camWorldX - 1 < 0. You can apply the same logic to the Y axis.

To read more about LibGDX coordinate systems, click here. To read more about Viewport#unproject(), click here.