Question

Posted earlier about something along the lines of this. This is a follow up. I have a previously existing java game in progress. Its about 95% finished. If I install libGdx, can I use previously existing java code with LibGDX's orthographic camera to create an android app? I plan on just using the camera to adjust to size of the screen so it fits on a phone. I am not going to use anything else like spritebatch etc. Anyone have experience with this?

Was it helpful?

Solution

That will work. The methods you care about don't rely on GL context from the libgdx engine, so you can instantiate an OrthographicCamera and use it normally from within your own game code.

Instantiate an OrthographicCamera with the empty constructor, then call setToOrtho on it with the width and height you want for the screen's viewport. This will depend on what units you are using for the scale of your sprites, but you can do something like this to set up your viewport:

private static final float MIN_SCENE_HEIGHT_IN_GAME_UNITS= 480f; //FOR EXAMPLE
private static final float MIN_SCENE_WIDTH_IN_GAME_UNITS= 760f;

   if (((float)screenWidthInPixels/(float)screenHeightInPixels) > (MIN_SCENE_WIDTH_IN_GAME_UNITS/MIN_SCENE_HEIGHT_IN_GAME_UNITS)){
       orthoCam.setToOrtho(false, MIN_SCENE_HEIGHT_IN_GAME_UNITS*(float)screenWidthInPixels/(float)screenHeightInPixels, MIN_SCENE_HEIGHT_IN_GAME_UNITS);
   } else {
       orthoCam.setToOrtho(false, MIN_SCENE_WIDTH, MIN_SCENE_WIDTH_IN_GAME_UNITS*(float)screenHeightInPixels/(float)screenWidthInPixels);
   }

The falses above are so y is pointing up, so you can flip that if you want. If you need to move the camera to 0,0, call orthocam.position.set(x,y,z) and then orthocam.update().

Then you can grab the matrix you need from orthographicCamera.combined.

If you're using GL ES 1.0, you can set the matrix like this:

gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadMatrixf(orthographicCamera.combined.val, 0);

Otherwise, you can just directly use orthographicCamera.combined in your shader

You could also clone the OrthographicCamera class and its superclass Camera into your project and remove the methods you won't be using to remove dependencies on other parts of the engine, and switch it to use your own versions of Vector3 and Matrix4.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top