5

I have working on libGDX and use Box2D for simple 2D physic interactions inside my game. However, recently I have found the existence of a library called JBox2D.

Is there any difference between this two libraries? What the advantages of one from another?

I know they both are based on the Box2D library for c++ because of this:

The Box2D implementation in libgdx is a thin Java wrapper around the C++ engine.


JBox2d is a Java port of the C++ physics engines LiquidFun and Box2d.

But, what do they mean by port and wrapping? Is JBox2D more complete than Box2D? which one is faster?

I ask this because I want to know if change my project from one to another (in this case from Box2D to JBox2D) can affect or optimize the performance of my game.

Alex S. Diaz
  • 2,637
  • 2
  • 23
  • 35
  • 2
    Wrapping would mean that their is a thin (essentially non-functional) API which makes it easier to call the underlying API from Java. It basically acts as a proxy between Java and (presumably) the native implementation. A Port is where the actual code has been translated from C++ to Java, so the ported version becomes (presumably) pure Java and doesn't rely on any external native libraries – MadProgrammer Aug 12 '15 at 00:13
  • Do this make the port one slowly? – Alex S. Diaz Aug 12 '15 at 00:15
  • 1
    You'd have to bench mark, some times, yes, some times no. The port would be easier to get running on multiple platforms as you won't need to worry about ensuring that the correct libraries are installed, for example, also, it won't suffer from issues such as updated libraries on the platform (OS) which might cause issues – MadProgrammer Aug 12 '15 at 00:19

1 Answers1

9

As MadProgrammer said, the libgdx box2d extension (gdx-box2d) is a JNI wrapper around the native box2d library. While the JBox2D library is completely written port in Java and doesn't rely on the native library. However, you might find it interesting to know that the gdx-box2d extension uses JBox2d behind the scenes when it is not possible to wrap the native library. Which is only the case for the GWT backend.

In most cases (specifically Android) it is faster to use the JNI wrapper instead of JBox2d (it is probably also faster on iOS because RoboVM needs to translate the JBox2D bytecode). Which is why the extension uses that on the those backends. Of course this depends on that actual scenario, so you should benchmark and compare it know the actual difference.

I haven't bench marked gdx-box2d vs JBox2d myself. But I can say from experience that the (3D physics) gdx-bullet JNI wrapper is much faster compared to the JBullet Java port.

Please note that the gdx-box2d includes the required libraries. It does not depend on any libraries on the platform that needs to be updated. You also don't need to worry about ensuring that the correct libraries are installed.

Xoppa
  • 7,983
  • 1
  • 23
  • 34
  • I'll have to learn how to benchmark then (it's a new word for me), do just porting my game from one library to another and then measure time/performance for both count as bench marking? or there are standard on how to benchmark ? – Alex S. Diaz Aug 12 '15 at 21:21
  • 4
    Unless there's an actual (performance) problem you're trying to solve *and* you actually found that box2d is causing that problem *and* you verified that it is not a problem with your own code (e.g. misusing the API), you shouldn't even think about this. First identify the problem, then try to solve. Don't try to solve problems that might or might not exist. – Xoppa Aug 12 '15 at 22:42