Friday, 19 April 2013

Spawn points and map data

So lately I've had a bit of a conundrum, how do I save level data so that when I load the scene I can get things like player spawn points, patrol areas etc. My options as I saw them were:

  • Find a way to place the data directly onto the map
  • Save it into another file i.e. <level_name>data.xml and create a map editor that synchronises the two.
Naturally the second one isn't the best option, there is always the chance the two could become out of sync and the level won't be correctly loaded the first is far better. And before I go on a brief introduction should be given on Spatials vs Nodes vs Geometries:

  • A Spatial is an abstract class that contains all the data and base methods for nodes and geometries. You can store things such as position and rotation in a spatial.
  • A Geometry is an extension of a spatial except it represents a visible object in the scene graph
  • A Node extends Spatial is a handle for other Spatials, it can be used to group objects in the scene graph
So initially I wanted to add a nodes to the graph to represent data, there would be a parent node called something like spawns and all it's children would have a local translation set. However in the sceneComposer in the jmonkey SDK there isn't an option to add a node to the scene. But there is an easy workaround (credit does go to Normen from the JMonkey community for the inspiration behind this):

In the models folder in assets save a new jmonkey scene (.j3o file), however don't create a model for it leave it blank. The empty space for the model will have an origin, and we can place this empty model in the scene and use it to store information. If you don't understand double click on the file and see the picture of the axes. Well when we place this empty model the origin of these axes is our local translation. We can place these blank models around the scene and get their location and this is how information is being saved into the level. I won't go into how to add these models to the scene this is covered adeptly on the jmonkey website.

These models will be added as a child to a node added to the scene graph. I called this node "Spawn points" and loaded the list of spawn points with the following code:

Node spawnList=(Node)((Node)level).getChild("Spawn points");
for(Spatial s:spawnList.getChildren())
{
    ArrayList<Vector3f> spawnPoints.add(s.getLocalTranslation().add(0,4,0);
}

The add(0,4,0) is there to prevent spawning a character at a point were it can fall through the map. There's no need to make additional blank models of course, you can infer the purpose of the spatial by the name of its parent node.

Of course this doesn't solve the patrolling issue but I have a number of ideas:
  • Using none blank models the patrol zone being inside them
  • Points defining the area, the patrol area is a concave polygon encompassing all the points
  • Other more inventive method.
As I see it now the point based method has short comings based on the difficulty of specifying a awkwardly shaped patrol zone, and multiple patrol zones. Also the model method has problems, storing a large amount of different shapes that aren't even loaded adds an overhead to the project. I'm not attempting to prematurely optimise but while this isn't a pressing concern there's no harm in mulling it over until an elegant enough solution presents itself.

I've also attempted and successfully created and animated a human figure in blender + makehuman I'm going to work on some more pressing essentials before I start figuring out the best way to export/import these assets and control the animations. These are likely going to be timely operations given my inexperience with graphics and I have other things to focus on.

No comments:

Post a Comment