JBox2d
From Box2D Wiki
JBox2d is a port of Box2d v2.0.1 to Java, maintained by quixote_arg and ewjordan.
Contents |
External Links
- http://jbox2d.org/ - Website/demos
- http://www.sf.net/projects/jbox2d - Sourceforge project page
- http://sourceforge.net/projects/jbox2d/files/jbox2d/2.0.1/JBox2d-2.0.1.zip/download - Version 2.0.1 snapshot download (latest stable version)
- Subversion repository trunk: https://jbox2d.svn.sourceforge.net/svnroot/jbox2d/trunk/ - the newest version of the JBox2d code can always be checked out from here. It is often unsupported, and is not even guaranteed to compile, but will always be much closer to the newest version of the C++ code.
We recommend either using the Subclipse Eclipse plugin (cross-platform) or the TortoiseSVN Windows Explorer add-on (Windows only) to access the repository without having to learn how to use Subversion from the command line.
Compilation guide for Eclipse
Using Eclipse is the easiest way to get started using JBox2d. These instructions will explain how to get the testbed running.
- Download the zip file from http://sourceforge.net/projects/jbox2d/files/jbox2d/2.0.1/JBox2d-2.0.1.zip/download
- Unzip the file. You might want to rename the resulting JBox2D 2005-08-2008 directory to JBox2d and copy/move that directory into your Eclipse workspace directory - whatever name you choose here will be the name of the project in Eclipse, so choose wisely.
- Open up Eclipse
- In Eclipse, select File -> New -> Java Project
- Under the Contents heading, select Create projects from existing source and select the JBox2d directory (if you renamed it as suggested above) (newer versions of Eclipse might not have that option - just type the name from the previous step)
- Name the project JBox2d (this must match the name of the directory the project is in, so if you renamed it differently in step 2, make the name match here)
- Click Next and then Finish
- Now there should be a JBox2d project in your workspace. In the Package Explorer, open that project and navigate to src -> org.jbox2d.testbed
- (The current download comes with an integration with Slick which is missing some files. If you are not interested in Slick: Go to Project|Properties|Java Build Path|Projects and remove "SlickBox2D". You will also need to delete the org.jbox2d.integrations directory and subdirectories from Package Explorer.)
- Right click the TestbedMain class, and go to Run As -> Java Application. You can also choose Run As -> Java Applet if you wish.
- You should see the test running. You're done!
To use JBox2d in a project of your own, the simplest way is probably to directly import the source into your project. You can also use one of the .jar files included in the distribution.
Graphics with JBox2d
JBox2d uses the open source Processing library in the testbed. Processing is generally used with its own IDE to write simplified Java code that is then exported as an applet or application using Java 1.4, but JBox2d uses features from Java 1.5 so may not be used (at least as-is) within the Processing IDE.
Note: the version of Processing included with JBox2d does not run very well when using a Java 1.6 virtual machine, and may cause frame-skipping. This is a Processing/Java issue, not a JBox2d one - try switching to 1.5 if it bothers you.
The Processing library (core.jar in the JBox2d distribution) is very easy to use itself, so you can probably get pretty far using it with JBox2d from within Eclipse. The tests.PTest class inherits from PApplet, so if you've used Processing before, you can use any Processing methods within PTest.
However, if you are not familiar with or choose not to use Processing, you will need to provide your own graphics, input, and game loop handling. Exactly how to do this is up to you (you might look into Java game libraries like Slick or JMonkeyEngine for fast OpenGL graphics and some neat effects). You still need to know how to get at the right data in JBox2d, so you should look at the ProcessingDebugDraw class to see an example how to do this.
It is strongly recommended that you write your own transformation functions (or use the ones built in to your game engine) and handle scaling and translations. Using pixel-units is discouraged, as JBox2d (like Box2d) functions best with objects ranging in size from about .1 to 10 units (meters, usually). Pixel scale objects will be much larger, and the engine is not tuned for them.
Where are the docs?
Javadocs are now included with JBox2d as of version 2.0. Most functions that you need to use should be covered.
Differences Between JBox2d and Box2d
- Class names generally lack the b2 prefix in JBox2d. For instance:
| [C++] | [Java] |
|---|---|
b2Shape b2Vec2 |
Shape
Vec2 |
- Method names are camel-cased in JBox2d:
myb2Vec2.Dot(otherb2Vec2) ThisIsAMethod(parameter) |
myVec2.dot(otherVec2) thisIsAMethod(parameter) |
- Built-in types don't have bit-length modifiers:
float32 int32 uint8 |
float int int /*nothing is gained by using smaller data types in Java*/ |
- Generally speaking, pointers turn into references in JBox2d. As a rule of thumb, you can usually start by just deleting any *s and &s that you see in the C++ code, and resolve any problems as they arise:
m_vehicle->CreateShape(&poly1); |
m_vehicle.createShape(poly1); |
- Polygon definitions use a List<Vec2> instead of a preallocated array. This also means that you do not need to set the vertex count yourself - if you need to get the vertexCount, use the vertexCount() function.
b2PolygonDef sd; sd.vertexCount = 3; sd.vertices[0].Set(-0.5f, 0.0f); sd.vertices[1].Set(0.5f, 0.0f); sd.vertices[2].Set(0.0f, 1.5f); |
PolygonDef sd = new PolygonDef(); sd.vertices.add(new Vec2(-0.5f, 0.0f)); sd.vertices.add(new Vec2(0.5f, 0.0f)); sd.vertices.add(new Vec2(0.0f, 1.5f)); |
- Global methods in the C++ version had to be put into classes for the Java one. The choices as to where to put these were largely arbitrary, though they usually correspond loosely to whatever .cpp/.h file they were declared within in the C++ version. These functions are mostly for internal use only, so it shouldn't affect you too much.
- No operator overloading for Vec2/Mat22 methods:
myb2VecA = vecb + vecc |
myVecA = vecb.add(vecc); |
- Math-related b2* methods are added to the relevant classes, unless they are already present in Math:
b2Dot(veca, vecb) b2Mul(matrixa, vecb) b2Max(floata, floatb) |
Vec2.dot(veca, vecb) Mat22.mul(matrixa, vecb) Math.max(floata, floatb) |
Issues
- Performance: We're aware that JBox2d does not achieve the same impressive performance as Box2d. Some of this is an unavoidable limitation of Java, some of it is due to lack of Java-specific optimizations. This should improve a bit as we further optimize, but we're not expecting miracles. At the moment JBox2d (run without drawing) is roughly half as fast as the C++ version, which still means that the bottleneck will usually be graphics, not physics.
- Lack of Java-ness: This is a difficult issue for us. We would love to make all the fields private, and refactor everything like mad until it actually looks like Java code. But we are trying to balance Java-ness, maintainability, and performance, and until the C++ version stabilizes a bit more we need to keep very close to its organizational structure, which means public fields all over the place. We're aware of this issue, and we take it seriously, but for now it will have to wait if we want to have any chance of keeping up to date. To ensure that your code does not have to change very much in the future, best practice is always to use getters and setters where available instead of accessing member fields - using Eclipse it is very easy to browse the available methods, we suggest you do so.
- Dependencies in testbed: Right now the testbed code depends on the Processing core library (http://www.processing.org) for drawing. A lot of people have asked for a plain old AWT version of the tests, which we will do if we have time. We also plan to add Slick and/or JMonkeyEngine demos. Ultimately, though, it's up to the end user to figure out their own drawing code - it's really not that tough if you look at the way we do it with Processing, the methods are pretty transparent (drawLine(x0,y0,x1,y1) is easy to translate to any engine, no?). We would be more than happy to accept examples that show how to use JBox2d with other libraries, so please let us know in the forums if you have something you'd be willing to release under a zlib license that other people could build off of.
- Other bugs: JBox2d should produce roughly the same output as Box2d. The exact numerical values may be off by a little bit due to different floating point handling, but we're really shooting for identical large-scale behavior. If you see any problems, please tell us about them!
