Groovy Wonderland

Our guest blog post today comes to us from Douglas Finnigan of the School of IIT at Temasek Polytechnic in Singapore. In addition to his superb introductory tutorials (Wonderland Development, Basic Concepts and Wonderland Development, Intro to Modules), Douglas has been working on ways to simplify Wonderland programming using Groovy. In this article, he also describes a simple particle system that can be used for weather simulation.

Snow in WonderlandFor the past six months, I’ve been part of Sun’s Co-Space project in Singapore, using Wonderland to develop a framework to run and visualise simulations inside a virtual world. An early piece of work was to read the Yahoo! weather RSS feed and visualise the local weather conditions. I was interested in getting dynamics to work in Wonderland, and had already experimented with cloth, so decided to use particle systems for the weather. All we needed was to show basic weather, such as rain and snow (not that it ever snows in Singapore!), so I decided to create a few simple particle systems which could be swapped in and out depending on the weather condition-code number. My first thought was to hardcode each particle system in Java, but this kind of went against the grain. I wanted a way for someone to add, update and load a particle system without having to dig in and modify the module’s source code.

Which is where Groovy came to the rescue.

Groovy has what are called Builder classes, for the (relatively) easy building of hierarchical data models, with built in support for XML, Ant build files, Swing development and more. You can also create your own builders for customised model building. So, after some trial and error in getting Groovy to work with Wonderland (via Netbeans and a fair bit of Ant hacking), most of the module’s code is now written in Groovy. Groovy classes are compiled to byte code, and as long as everything is built in the correct order, Groovy-Java integration is problem free.

The way the Groovy weather module works is that particle systems are defined in a simple, declarative-style markup script. For JMonkeyEngine particles, some attributes are simple primitive values, while others, such as the various influence types, are objects, which must also be defined in the script. So, the basic script structure is:

define Particle System object (String : id, Map : constructor attributes, if required) {
set primitive attributes
set object attributes {
define object (String : id, Map : constructor attributes) {
set primitive attributes
set object attributes {
define object ( ... ) {
...
}
}
}
}
set more object attributes, as required ...
}

A simple example is given at the end.

The classes for reading a script, parsing the data, generating an object tree, making sure the correct methods are called with the right arguments, and then putting this all together to create a particle system object, are all written in Groovy. Scripts are stored on the server, and loaded via url whenever a new weather code arrives from Yahoo!. Scripts can be modified and re-loaded without having to stop and start the Wonderland server. Actually, to speed things up slightly, scripts are pre-processed into a hashmap of particle system objects – this can be improved, but the project was just a simple demo and is now happily retired. (Although since this basic framework can be used to define and create other objects, not just particle systems, it may get resurrected at some point.)

The question is probably: why Groovy? My answer is: speed. Not runtime speed, but development speed. Groovy has an incredible amount of support for IO, list and String processing, meta-programming and reflection (a crucial part of this project), and the removal of great swathes of boilerplate code. For example, reading and extracting data from the Yahoo! weather feed took only five lines of code. In another project, the use of Groovy closures has been invaluable. It would now be very painful to go back to Plain Old Java. The only real drawback is the lack of compile-time error checking, which I can live with. (Scala, which is type-safe, might be even better than Groovy, though I haven’t looked at this yet.)

Rather than extend this entry beyond its allotted shelf-space, I hope to add a tutorial on integrating Groovy with Wonderland soon. (Note, this isn’t about using Groovy as a scripting language for Wonderland, but as part of module development.) I’m constantly amazed at how well designed Wonderland is, especially for extensibility, and the integration of Groovy is yet another example of this. Many thanks to the Wonderland team for their great work, and also for the opportunity to share this blog entry with everyone!

Appendix

Simple example:

Node(prefix: "com.jme.scene", args: "rootNode"){
attachChild {
ParticleMesh (id: "pSystem", args: ["snow", 1500]) {
localTranslation ([0.0, 8.0, 0.0])
startSize 0.075
endSize 0.055
startColor ([1.0, 1.0, 1.0, 0.5])
endColor([1.0, 1.0, 1.0, 0.0])
Texture ("snow.png")
renderState {
BlendState {
blendEnabled true
enabled true
}
}
geometry {
Ring {
outerRadius 10.0
}
}
emitterTransform {
TransformMatrix (args: "quat") {
Quaternion (id: "quat", args: [0.7071f, 0.0f, 0.0f, 0.7071f])
}
}
addController {
ParticleController (id: "contr", args: "pSystem") {
repeatType Controller.RT_CYCLE
controlFlow true
speed 0.10
addInfluence {
WanderInfluence {
wanderDistance 0.75
wanderRadius 0.01
wanderJitter 0.01
}
}
addInfluence {
BasicGravity {
gravityForce ([0.0, -0.01, 0.0])
rotateWithScene true
}
}
}
}
}
}
About these ads

8 Responses to Groovy Wonderland

  1. Jagwire says:

    Well done. I can’t wait to read the tutorial :)

  2. Andy Burnett says:

    Me too! I would really love to see how more dynamic languages can be integrated with Wonderland. If we can get to the stage of experimenting with something like jRuby, that would be fantastic.

  3. Douglas says:

    Actually, integrating Groovy isn’t so difficult. The main things to figure out are how to build the Groovy classes without modifying the structure of the current Wonderland build files, and how to compile circular dependencies between Groovy and Java classes.
    Thanks for the kind words, and I’ll try to get a tutorial written within the next couple of weeks, perhaps based on the particle system demo just to show what Groovy can do :)

  4. Douglas says:

    OFF TOPIC:
    Has the Sun blog site been hit by a virus? Everything is REDEYE OWNZ … !
    Any idea if it can be fixed?

  5. Nicole Yankelovich says:

    The blog seems okay to me, but the forum and wiki were both attacked by hackers. The java.net staff is working to get our site restored. Thanks for everyone’s patience while the situation is getting resolved.

  6. Douglas says:

    Sorry, Nicole, I meant the forum … didn’t mean to worry you!
    But just check – the forums are still hacked … stupid hackers, what’s the point?! Good luck with the recovery.
    I hope all is well with you :)

  7. I’ve just heard from our java.net manager and she says the whole site has been taken down and the service provider is restoring the site from a backup. Let’s hope they’ve done a good job backing up our data!

  8. [...] room, as you can see demonstrated in the Xland video trailer. A previous WonderBlog article about a way to implement a snow effect inspired us; Douglas Finnigan, the author of that guest blog post, developed his snowflake [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 54 other followers

%d bloggers like this: