Sunday 21 July 2013

Adding tests to a JBox2D Testbed

In order to speed up the learning process and really get the most out of the JBox2D physics engine, I'm going to utilise the Testbed framework. There is some online documentation for the Testbed, however, I found that it is more of a companion guide for the jbox2d-testbed module's codebase; almost psuedocode, pointing you towards paramount parts of the code. Whilst this is useful once you are up and running, what I really needed to get me started was a complete and simple guide on how to go from a brand new project to launching the example test.

What I'm going to outline here are the steps which need to be performed in order to launch a test of some JBox2D modelled, physics behaviour. The full code for which can be found on GitHub.

As highlighted in the Adding Tests/Settings section of the documentation, we need an entry point to perform the following, high-level, tasks:
  • Instantiate an instance of the Testbed
  • Add our test(s)
  • Launch the Testbed's GUI
The first thing that wasn't immediately clear from the online documentation, but that was cleared up by the codebase is that our entry point (we'll call it TestbedMain) is not a JUnit test, but a Java class with a Main method. The online documentation/example seems to be a bit out of date - below is the complete code for my TestbedMain class:  

The MJWTest2 class (seen referenced in the code above) is the test which we want to run, and the code for which is almost copied verbatim from the A Simple Test section of the documentation, with the exclusion of the following line which does not compile:
It's also likely at this point that your TestbedMain class does not compile because of the missing libraries/dependencies. I used Maven to manage these dependencies, and I think that this is the easiest way - the following dependency needs to be added to your pom.xml file:

You could, however, just add the dependencies' JAR files to the project by hand if you don't want to use Maven - here's a view of the dependencies needed:


You should then be able to execute the Main method of the TestbedMain class, and you will see the sample test started in the Testbed's GUI:


















The full code for this simple project can be found on GitHub.

Prev (Getting started with JBox2D)    Next (Modelling simple physics behaviour)

No comments:

Post a Comment