Hex Tile based games in JME

Can’t get GeometryBatch working (see http://www.jmonkeyengine.com/jmeforum/index.php?topic=4552.0). What do I make wrong?



[edit] I took the wrong constructor (the one without arguments). It is annoying that you need an initial mesh (which can’t be translated as you can do in addGeometry). However, GeometryBatch works fine, and I would say I have a speedup by factor 1.5 … 2. Cooool!

No argument constructors are there for serialization / savable sake.  I thought most of them were javadoc'd as "do not use".

Couldn't we make them private so people don't make that mistake?  If the serialization code uses reflection it can make it accessible for its use still…

If they were private, the importer/exporter classes would not be able to use them to instantiate the class would they?

I have a new version of GeometryBatch that seems to work, it was not an issue with the "name" not being set - though that constructor should never have been used as we have learned

Just an update - impromptu testing of the GeometryBatch method of making the Hex board shows framerates approaching 600 FPS with boards as large as 100x130 hexes.  The larger problem with boards of that size is that it takes a looong time to load.  It is creating 13,000 hexes afterall.  My guess is that there's room for improvement there, but regardless this really seems like the way to go.



I suppose it's time to start moving forward with this solution and begin looking into things like picking and translating that back to a "hex" location on the board.



Does anyone have suggestions about moving bits and pieces of this wisdom over to the Wiki?  I would be glad to provide sample code and textures for the examples (at least for a very simple hex based board) and as things go along maybe we can add to it with knowledge about picking, etc so that anyone else interested in this kind of thing with jME has a one-stop shop for tips and tricks.  Any thoughts?

renanse said:

If they were private, the importer/exporter classes would not be able to use them to instantiate the class would they?


Proof of concept:

package test;

import java.lang.reflect.*;

public class TestReflection {
   public static void main(String[] args) throws Exception {
      Constructor c = MyBean.class.getDeclaredConstructor(new Class[0]);
      c.setAccessible(true);
      MyBean bean = (MyBean)c.newInstance(new Object[0]);
      bean.test();
   }
}



package test;

public class MyBean {
   private MyBean() {
   }
   
   public void test() {
      System.out.println("Testing!");
   }
}



You can't just call MyBean.class.newInstance() since that will throw an exception.  You have to explicitly reference the no arg constructor and then use setAccessible(true) on it and then you can instantiate it without problems as referenced above.

It's one of those "cheats" in Java that once you realize what reflection can really do it starts to scare you what you can actually get away with.
darkfrog said:

renanse said:

If they were private, the importer/exporter classes would not be able to use them to instantiate the class would they?


Proof of concept:

package test;

import java.lang.reflect.*;

public class TestReflection {
   public static void main(String[] args) throws Exception {
      Constructor c = MyBean.class.getDeclaredConstructor(new Class[0]);
      c.setAccessible(true);
      MyBean bean = (MyBean)c.newInstance(new Object[0]);
      bean.test();
   }
}



package test;

public class MyBean {
   private MyBean() {
   }
   
   public void test() {
      System.out.println("Testing!");
   }
}



You can't just call MyBean.class.newInstance() since that will throw an exception.

Leave it as-is for now.  I'm not sure we're ready to go through making all those constructors private yet.

Sounds good.  DarkFrog, what do you think about my question for the Wiki?  What key bits and pieces of this thread so go in there?  I have a decent amount of sample code I can post (hopefully landei can also assist with that) and where should this topic go?



Also, should I be updating the existing GeometryBatch class post on the Wiki?  Or is it too soon to do that.  At the very least I can fix the "bug" with the ColorBuffer in the no argument constructor so that as it is, it won't throw an exception if someone happens to use that.

Use your own judgment for the wiki, if anyone complains we can always move it around later. :wink:



Don't worry about "fixing" the no-arg constructor as I have a feeling they will all be going away within the next few days anyway. :wink:

I wrote the original GeometryBatch on the Wiki. Feel free to update the class source on the Wiki. I'm really glad someone finds it useful!  :smiley:

stodge said:

I wrote the original GeometryBatch on the Wiki. Feel free to update the class source on the Wiki. I'm really glad someone finds it useful!

"Fixing" the no arg constructor in that fashion bugs the heck out of me for some reason.  Anyone else want to chime in as to why this would be a good way to go?

I can't imagine why that would bother you?  You're already using reflection to instantiate Savables', right?  You don't want the no-arg constructor to be visible for use do you?  Seems like a perfect use of this feature of reflection.

Because changing the visiblilty of the constructor seems like one of those things that could fail due to a security manager issue or something similar.  It's something that should at least be examined a bit first before a knee jerk "let's change all of the Savable classes" action.  Anyway, relax, it's definitely worth a discussion at least.

I'm fine with the discussion and research into it…I was just taking issue with the "bugs the heck out of me" part. :wink:


renanse said:
Anyway, relax, it's definitely worth a discussion at least.


I would think you'd know me well enough by now to know that I'm ALWAYS relaxed. ;)

Oh?  Did someone change the meaning of the word relaxed recently or something??  :stuck_out_tongue:

As this thread is now more about GeometryBatch as about the Hex Tiled Game, may I ask if someone knows

  • how to write a constructor GeometryBatch(String name)
  • how to add rotations to the addGeometry() method



    BTW: Thank you stodge for that great work!
Landei said:

As this thread is now more about GeometryBatch as about the Hex Tiled Game, may I ask if someone knows
- how to write a constructor GeometryBatch(String name)
- how to add rotations to the addGeometry() method

BTW: Thank you stodge for that great work!


Landei, I already have the constructor, just add this:


public GeometryBatch(String name) {
        super(name);
       
        FloatBuffer vertices = BufferUtils.createFloatBuffer(0);
        FloatBuffer normal = BufferUtils.createFloatBuffer(0);
        FloatBuffer color = null;
        FloatBuffer texture = BufferUtils.createFloatBuffer(0);
        IntBuffer indices = BufferUtils.createIntBuffer(0);

        reconstruct(vertices, normal, color, texture, indices);
 
        this.setModelBound(new BoundingBox());
        updateModelBound();
}



As for the rotation, I can look into it.  My time during the week is short though, so if you can work on it as well maybe we'll get ahead of the game (pun intended).