LGDX 2D Level Editor - ALPHA 1 - Stress test

Any community contributions to libgdx go here! Some may get included in the core API when permission is granted.

Re: LGDX 2D Level Editor - ALPHA 1 - Stress test

Postby Saurav » Thu Mar 31, 2011 12:17 am

Awesome, thanks so much. I figured it had something to do with getting the collision mask information from the shape, but I had no idea how it all tied together.

I'll probably be back with more questions once I've played with this for a bit. I've been procrastinating on figuring out how to do multiple levels for my game, but I've more or less run out of other things to tinker with, so this will be next :)
Saurav
 
Posts: 623
Joined: Sun Feb 27, 2011 7:59 pm

Re: LGDX 2D Level Editor - ALPHA 1 - Stress test

Postby Obli » Thu Mar 31, 2011 7:35 am

It's just that I need to provide a better API for box2D bodies than the current one which is....nothing.
Actually, to help you with collision masks that are indeed described as vertex indices, there are several methods in the "ShapeModelHelper" class.
Code: Select all
   /**
    * Converts point indices into points.
    */
   public static Vector2[] getPointsFromIndices(ShapeModel shape, int[] indices);


But this is a minimalistic api. I'll bring you a full API that will get you a complete Box2D body from a ShapeModel. Actually, what I imagine is that each polygon you lasso will be a fixture, and for each ShapeModel, a unique box2d Body will be generated, composed by as many fixtures as needed.

I think that I got my auto-lasso algorithm working, so no more trouble guys. I will also bring that API up and running, and finally make the source code available for anyone to contribute (I'll provide a quick "the main classes of the app" since there are 41 files and I don't want you to get a headache from figuring how the app is running...).
Obli
 
Posts: 616
Joined: Mon Jan 10, 2011 6:18 pm
Location: Bordeaux, France

Re: LGDX 2D Level Editor - ALPHA 1 - Stress test

Postby Saurav » Thu Mar 31, 2011 2:17 pm

OK, more newbie questions. I managed to create a level. The first time I tried to load it, the app crashed with 'class not found'. I figured that out, downloaded the Kryo library and added that to my project. That got the level loaded up (I think). Now on to the rendering. Note that so far I've been using Actors and Sprites and BitmapFonts, I don't know any openGL. My first attempt was just the code from Obli's original post:

Code: Select all
   public void render(float delta) {
      for (int i=0; i<level.getShapeCount(); i++) {
            ShapeModel shape = level.getShape(i);

            shape.fillTexDef.getTexture().bind();
            shape.getFillMesh().render(GL10.GL_TRIANGLES);
            shape.borderTexDef.getTexture().bind();
            shape.getBorderMesh().render(GL10.GL_TRIANGLES);
         }
   }


That didn't do anything, the screen didn't even change from the previous screen. So I poked around in GDX Invaders, and came up with this:

Code: Select all
   public void render(float delta) {
      GL10 gl = Gdx.app.getGraphics().getGL10();
      gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
      gl.glViewport(0, 0, Gdx.app.getGraphics().getWidth(), Gdx.app.getGraphics().getHeight());
      
      for (int i=0; i<level.getShapeCount(); i++) {
            ShapeModel shape = level.getShape(i);

            shape.fillTexDef.getTexture().bind();
            shape.getFillMesh().render(GL10.GL_TRIANGLES);
            shape.borderTexDef.getTexture().bind();
            shape.getBorderMesh().render(GL10.GL_TRIANGLES);
         }
   }


With this, I get a black screen, with a few white dots in the bottom left corner. It may be that this is simply a matter of scale? I see a 'pixels per meter' setting in the tool, and I left that at the default 75. But I don't see anything that tells me how many meters my drawn objects are. Is that the problem, that I've drawn things that are too small? Or do I need to do more in terms of openGL setup before this will work? The previous screen was a menu (Stage with some Actors), so openGL has been left in whatever state that would leave it in.

I'll bring you a full API that will get you a complete Box2D body from a ShapeModel.
That would be very helpful :)
Saurav
 
Posts: 623
Joined: Sun Feb 27, 2011 7:59 pm

Re: LGDX 2D Level Editor - ALPHA 1 - Stress test

Postby Obli » Thu Mar 31, 2011 3:01 pm

Yes, they are too small :D

The glViewPort is the faulty line. With this line, you're telling opengl that the rendering area width 800 meters (or "units") if your screen is 800 px wide. In the level editor, I'm not dealing with pixel units, but with meter units, which are arbitrary units. For instance, if you remove the glViewPort line, opengl will use the default viewport which is 2 meter wide and 2 meter tall (i.e. bottom left is at -1,-1, and top right is at 1,1).

So, what are the dimensions of your level ? Actually, the XY axis drawn in the editor is one meter wide and one meter tall. Do the conversion ;-) (that's why there is a "PIXELS_PER_METER" constant to define).

But do not mess with the viewport by yourself, go see the OrthogonalCamera implemention of libgdx ;-)
Obli
 
Posts: 616
Joined: Mon Jan 10, 2011 6:18 pm
Location: Bordeaux, France

Re: LGDX 2D Level Editor - ALPHA 1 - Stress test

Postby Saurav » Thu Mar 31, 2011 3:21 pm

I wondered about that. So the red and green axes in the middle of the screen are 1m? Also, I'm assuming the center of the screen (where the axes are located) is the origin? That makes more sense now.

I've used the ortho camera with my other levels, but like I said, my understanding of this is very thin :) I'll mess around some more and see if I can make it work. So far all my attempts with the camera or the openGL calls result in white dots in the corner. I also realized that my test level screen is derived from my screen base class, which sets up the camera for some of the other screens, so there's probably some interaction going on there as well.

Edit: OK, making some progress. I see the shapes, but no textures yet. But that's a step forward :)
Saurav
 
Posts: 623
Joined: Sun Feb 27, 2011 7:59 pm

Re: LGDX 2D Level Editor - ALPHA 1 - Stress test

Postby Obli » Thu Mar 31, 2011 3:31 pm

Here's a gift. This is the class from which I derive when I want to draw 2D stuff. You just need to implement the "getInitialScreenWidthInMeters" method by returning a float representing your desired screen width in meter units, and it will handle everything (camera initialization, etc) for you. From this, you can move the camera with camera.position.set(x, y, 0), or zoom by changing camera.zoom. You can convert from world position (in meter units) to pixel position (in pixel units) or vice-versa with the provided methods.

Code: Select all
package aurelienribon.libgdx;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;

public abstract class Base2DRenderer {
   public final OrthographicCamera camera = new OrthographicCamera(0, 0);

   public final Vector2 screenSizePixels = new Vector2();
   public final Vector2 screenSizeMeters = new Vector2();
   public final Vector2 pixelsPerMeterRatio = new Vector2();
   public final Vector2 metersPerPixelRatio = new Vector2();

   private final Vector2 tmpVec1 = new Vector2();
   private final Vector2 tmpVec2 = new Vector2();
   private final Vector2 tmpVec3 = new Vector2();
   private final Vector2 tmpVec4 = new Vector2();

   // -------------------------------------------------------------------------
   // Stuff to implement
   // -------------------------------------------------------------------------

   /**
    * Returns the desired screen size in meters when no camera scale is
    * applied.
    */
   protected abstract float getInitialScreenWidthInMeters(float ratio);

   // -------------------------------------------------------------------------
   // Factory
   // -------------------------------------------------------------------------
   
   /**
    * Builds the renderer and its attributes.
    */
   public final void create() {
      screenSizePixels.x = Gdx.graphics.getWidth();
      screenSizePixels.y = Gdx.graphics.getHeight();
      float ratio = screenSizePixels.x / screenSizePixels.y;
      float sw = getInitialScreenWidthInMeters(ratio);
      screenSizeMeters.set(sw, sw / ratio);
      pixelsPerMeterRatio.x = screenSizePixels.x / screenSizeMeters.x;
      pixelsPerMeterRatio.y = screenSizePixels.y / screenSizeMeters.y;
      metersPerPixelRatio.x = screenSizeMeters.x / screenSizePixels.x;
      metersPerPixelRatio.y = screenSizeMeters.y / screenSizePixels.y;
      
      camera.viewportWidth = screenSizeMeters.x;
      camera.viewportHeight = screenSizeMeters.y;
      camera.position.set(0, 0, 0);
      camera.zoom = 1;
      camera.update();
   }

   // -------------------------------------------------------------------------
   // Screen / World conversion
   // -------------------------------------------------------------------------

   /**
    * Screen distance to world distance
    */
   public final Vector2 sd2w(float dx, float dy) {
      tmpVec1.x = dx * metersPerPixelRatio.x * camera.zoom;
      tmpVec1.y = dy * metersPerPixelRatio.y * camera.zoom;

      return tmpVec1;
   }

   /**
    * Screen position to world position
    */
   public final Vector2 sp2w(float x, float y) {
      tmpVec2.x = x * metersPerPixelRatio.x * camera.zoom;
      tmpVec2.y = y * metersPerPixelRatio.y * camera.zoom;

      Vector3 p = camera.position;
      tmpVec2.x += p.x - screenSizeMeters.x / 2 * camera.zoom;
      tmpVec2.y += p.y - screenSizeMeters.y / 2 * camera.zoom;

      return tmpVec2;
   }

   /**
    * Screen touch to world position
    */
   public final Vector2 st2w(float x, float y) {
      tmpVec3.x = x * metersPerPixelRatio.x * camera.zoom;
      tmpVec3.y = (Gdx.graphics.getHeight() - y) * metersPerPixelRatio.y * camera.zoom;

      Vector3 p = camera.position;
      tmpVec3.x += p.x - screenSizeMeters.x / 2 * camera.zoom;
      tmpVec3.y += p.y - screenSizeMeters.y / 2 * camera.zoom;

      return tmpVec3;
   }

   /**
    * World distance to screen distance
    */
   public final Vector2 wd2s(float dx, float dy) {
      tmpVec4.x = dx * pixelsPerMeterRatio.x / camera.zoom;
      tmpVec4.y = dy * pixelsPerMeterRatio.y / camera.zoom;
      
      return tmpVec4;
   }
}

Obli
 
Posts: 616
Joined: Mon Jan 10, 2011 6:18 pm
Location: Bordeaux, France

Re: LGDX 2D Level Editor - ALPHA 1 - Stress test

Postby Saurav » Thu Mar 31, 2011 3:52 pm

Thanks! Off to work now, but I'll play with this stuff tonight. So far I have a hacked-together model and renderer. I threw in pixelsPerMeter here and there, multiplied / divided until I got things drawing right, without really understanding what I was doing or why I was doing it :) Time to revisit that and clean it up.
Saurav
 
Posts: 623
Joined: Sun Feb 27, 2011 7:59 pm

Re: LGDX 2D Level Editor - ALPHA 1 - Stress test

Postby jbrin » Thu Mar 31, 2011 5:42 pm

Obli wrote:Actually, to help you with collision masks that are indeed described as vertex indices, there are several methods in the "ShapeModelHelper" class.
Code: Select all
   /**
    * Converts point indices into points.
    */
   public static Vector2[] getPointsFromIndices(ShapeModel shape, int[] indices);



FFfffFfffuuuuuuuu... wish I had known about that sooner.

Must ... go ... rewrite levEL LOADING CODE WITH CAPS LOCK ON TO EXPRESS MY RAGE...
-Jason
jbrin
 
Posts: 71
Joined: Thu Dec 30, 2010 9:16 pm

Re: LGDX 2D Level Editor - ALPHA 1 - Stress test

Postby broken-e » Thu Apr 28, 2011 7:24 am

Obli wrote:Make sure to call the rebuild() method of the level with the same assets folder you used in the editor. By this I mean that the folder can be different but MUST contain the texture and border files required (since they are saved as relative pathes to the resource directory).

Hey Obli, thanks for the sweet tool! I know it's still alpha and you have a lot of other things you're doing too. But I have a question about clarification with the texture paths.

I'm thinking this is where I'm going wrong, because I have gotten my level to load without crashing, and I can see the shapes, but they have no texture. Just white.

I tried a couple scenarios and a couple different png files. At first I had the files in their own folders, but to troubleshoot I have tried putting the level file and the texture file in the same folder with the LevelEditor.jar file, saving the level file with that texture, and then putting the .level and .png in my "assets/data" folder in eclipse. Then I load the level file with "data/test.level" and the texture path for rebuild is "data". Still no texture.

Is there any obvious problem with what I've tried so far, that would be causing the issue?

thanks again for your contribution to us mooching devs! :mrgreen:
my latest release: SnowPaper
broken-e
 
Posts: 700
Joined: Sun Feb 06, 2011 7:20 am

Re: LGDX 2D Level Editor - ALPHA 1 - Stress test

Postby jbrin » Wed May 04, 2011 11:11 am

Hmm, I think it'll error out right away if you try to launch your game with the assets in the wrong place... I'll need to check on that.

Have you enabled 2d textures?

Gdx.gl10.glEnable(GL10.GL_TEXTURE_2D);

Did you bind your textures?

yourShape.fillTexDef.getTexture().bind();



Those are the only things I can think of ... I can't really think right now, (still angry... fuuuu..)
-Jason
jbrin
 
Posts: 71
Joined: Thu Dec 30, 2010 9:16 pm

PreviousNext

Return to Libgdx Contributions

Who is online

Users browsing this forum: No registered users and 1 guest