Random XML mesh generation

Hi all,

I’m fairly new to JME (I went through all of the beginner tutorials this past weekend) - but there’s nothing like trying to dive in at the deep-end! From what I’ve seen so far I’m very impressed and currently toying around with different ideas trying to make some cool stuff.

I’m currently experimenting with genetic algorithms and using JME to render the results.

Essentially I want to start with a population of, say, 100 randomly generated mesh XML objects. The idea being that in order to create these random meshes, you would be able to pass in some constraining parameters (e.g.: objects can have a maximum size of x, and no more than y faces, etc.) - currently I’m looking at writing some kind of XML generator class which outputs some Ogre XML defining a “random” object mesh - but this seems an incredibly manual process and with a lot of potential pit-falls, such as how would you ensure all defined faces in the submesh form a solid whole, etc. It just feels like the wrong approach as there would surely be a huge number of checks and validations I’d have to perform across the randomly generated values before I could say “Yep, that’s a valid object - now load and render it”.

I was hoping to see if anyone knows of an easier way to generate random valid object meshes, or can point me in the direction of a generally “better” approach. My idea is that eventually I will write a fitness function to judge the generated objects based on how they perform in the environment at a given task - enabling me to take the XML of the best solutions and combine parts of their XML strings to generate the next generation of meshes, etc.

Many thanks in advance!

what kind of meshes are you trying to make? random regular polygons? terrains? malformed blobs?

generating just a random mesh would be easy, you could just use the built in meshes in jmonkey like Box, Cylinder, Sphere, etc and randomly populate their parameters, (then save to a j3o- the native jmonkey format).

I was ideally looking to start with some malformed blobs and go from there, allowing the artificial selection process to morph the shapes in as unconstrained way as possible - and see what kind of interesting mesh objects turn up as result.

Using the built in meshes would be a good starting point but I think it would put too many limitations on what can evolve, as I can’t change a box into anything other than another type of box - unless there exists some jmonkey (or external) library that can manipulate/morph/distort meshes in some way?

EDIT: To give a bit of context, one example of what I might do is to generate a terrain, place my 100 random mesh objects at the top of a hill, apply physics/gravity and see which ones travel to the bottom of the hill fastest. Take the 20 quickest meshes and blend them together to create another 100 meshes which should hopefuly be slightly faster. Rinse and repeat to find the ultimate shape for “rolling down a hill”.

Still… is there a reason you must use XML or can you just generate meshes directly?

there’s not really an easy way to do what you want. my advice would be to start with this page: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:custom_meshes and then try to come up with a system to randomly generate vertices/faces while still maintaining the rules of a mesh and also being “randomized” in accordance to some pattern.

afaik theres currently no tools that exist for manipulating jme meshes as most models in jme games are imported from blender. (though something like this would be a really awesome plugin contribution!)

@pspeed said: Still... is there a reason you must use XML or can you just generate meshes directly?
Not particularly - I picked XML as a starting point following on from the Hello Animation tutorial as it seemed closest to what I wanted to achieve, and I have some experience with SimpleXML so the marshalling/demarshalling side of things would be relatively easy. None of the beginner tutorials really cover direct manipulation of vertices so I wasn't aware that sort of thing was even possible, but that may indeed be the way to go.
@icamefromspace said: there's not really an easy way to do what you want. my advice would be to start with this page: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:custom_meshes and then try to come up with a system to randomly generate vertices/faces while still maintaining the rules of a mesh and also being "randomized" in accordance to some pattern.

afaik theres currently no tools that exist for manipulating jme meshes as most models in jme games are imported from blender. (though something like this would be a really awesome plugin contribution!)


Thanks! That’s a very useful page and might just help get me going in the right direction. I’ll have a play around and see what I can come up with. It might take me a while to get my head around the maths of it all but maybe I can work towards coming up with methods that perform blender-like functions, e.g.: a method that creates a depression (concave/convex) on a given object face. No promise that it’ll actually lead to anything but I’ll post any progress on here. Thanks again :slight_smile:

EDIT: So based on that link I had some ideas this morning which might be one way to go about this…

  1. Generate 3 random vertices - these form your initial triangle in the polygon
  2. For (1…N) generate 1 additional random vertices, and join it to 2 randomly chosen existing vertices to form the next triangle of the polygon
  3. Vertices and Triangles would be stored in some kind of collection like an ArrayList
  4. The end result could be anything - I would think that the initial population will largely consist of twisted contorted splines which don’t really resemble anything more than a scribble or a scrunched up ball of paper!

Various parameters can be passed into the method for generating a polygon to determine things like:

  • The maximum size of the polygon (the range in which randomly generated vertices are bound)
  • The maximum number of triangles allowed in the polygon (to ensure a finite size and reduce overheads in rendering later on)

When it comes to combining solutions to artificially select for the 2nd, 3rd, 4th, etc… generation, I am thinking of taking the ArrayLists which represent each solution, and splicing them/crossing them over at a random point. The vertices/triangles “on the edge” of the splice would have to be carefully adjusted to ensure they successfully “glue” both halves of the resulting shapes together. As with all genetic algorithms there will be mutation, allowing for the chance to vertices/triangles to randomly change between generations. Another idea is that to show the inheritance and trace lineage, you could base the colour or texture of the polygon on that of its “parents”.

After many generations you MIGHT (and that’s a big might…) end up with polygons approximating all kinds of interesting shapes - depending on however I decide to evaluate their fitness.

Not got anything concrete in mind yet but I’m also thinking how I might be able to work randomly generated animation into the picture to give these polygons the ability to seemingly “wriggle” over the landscape. This may give rise to a polygon which learns to move in such a way that it can climb hills, for example. I’m not getting ahead of myself though, that’s a long way off at this point! This is all just a collection of thoughts and probably won’t amount to anything, but could be a fun learning experience!