Box2D Java

ewjordan wrote:
For now, you might just wrap each of your query calls in a try/catch, you should be able to safely ignore any exceptions there without causing much trouble (the side effects of those calls should not do anything important to the simulation), at least until I track this down.

Yep, I've already done that. Also, I changed the assert in query() to an if, so it will just print out the values of i and s. Anything else I should log (lowerQuery, ...)?
Yeah, if you're spitting stuff to a log file and it's not happening too often, it would be great if you could log some stuff here. Suppose you have some sort of doLog(String) function, then if you can insert the following code in place of that assertion, any results you see would likely help track this down:
Code:
if (i < 0) {
                 doLog("i = "+i);
                  doLog("s = "+s);
                  doLog("lowerValue = "+lowerValue);
                  doLog("upperValue = "+upperValue);
                  doLog("lowerQuery = "+lowerQuery);
                  doLog("upperQuery = "+upperQuery);
                  for (int j=0; j<boundCount; ++j) {
                     doLog("bounds["+j+"]: "+bounds[j]);
                 }
                  doLog("axis: "+axis);
}
I'm sorry (in a way) to tell you that the problem did not occur anymore during the last games, so no log.
The multi-touch table had to be deconstructed today, so I won't be able to do any test for some time... sorry.

Furthermore, I came across another exception two times yesterday. However, they might be related to my code. I'll go into further details next week, as I'm away for the weekend (and I have to pack my bags now... 😉 )
Ah well, thanks for trying, ElkMonster - I suppose since the bug is so rare that means it's probably not a huge priority to worry about, though I'll definitely keep my eyes open for it, as it's pretty bad if something crashes unexpectedly during a long simulation...I'll probably try fuzzing an input to the query function to see if I can cause it to happen again, maybe that will turn up something that I can track down (I've got to assume this is some sort of edge case, where you're querying right up against the boundary or something odd like that).

In any case, glad that the project turned out okay in the end. I'm curious, do you have any videos to show or anything like that? I've been interested in getting into the multi-touch stuff for a while, I'd love to see it in action with Box2d.
ewjordan wrote:
Ah well, thanks for trying, ElkMonster - I suppose since the bug is so rare that means it's probably not a huge priority to worry about, though I'll definitely keep my eyes open for it, as it's pretty bad if something crashes unexpectedly during a long simulation...I'll probably try fuzzing an input to the query function to see if I can cause it to happen again, maybe that will turn up something that I can track down (I've got to assume this is some sort of edge case, where you're querying right up against the boundary or something odd like that).

Yeah, that's what I think, too. Considering all the testbed examples and other apps that have been run by quite a number of people without the bug occurring, it is really not that important right now.
One more hint: It could be that the bug occurred when two bodies were overlapping. In my game, you can "glue" boxes by putting them next to each other (while touching each with two fingers). As this is done with great force (your hands 😀 ), boxes often happened to overlap for some time, i.e., more than one frame.

Here's the other exception I mentioned:
Code:
java.lang.ArrayIndexOutOfBoundsException: 15
   at org.jbox2d.dynamics.Island.add(Island.java:81)
   at org.jbox2d.dynamics.World.solve(World.java:598)
   at org.jbox2d.dynamics.World.step(World.java:483)

This occurred when you did the "glueing" mentioned above with many boxes (more than eight, maybe; all glued to the same "complex" figure). Glueing is implemented through revolute joints. I hope you can imagine it somehow from my explanations...
The exception happened three times, but was not really reproducible. Most of the time, everything would work fine. I used to have some trouble with double-destroying joints accidentally, and as the glueing (and "unglueing", which you can also do) involves fiddling with joints a lot, I'm not sure if it could be my fault, being related to joint destroying (but note that destroying was not done during the simulation step).
I'm sorry I couldn't go deeper into it, but it happened after one of the last matches and I didn't have the time then to experiment more. 🙁

Quote:
In any case, glad that the project turned out okay in the end. I'm curious, do you have any videos to show or anything like that? I've been interested in getting into the multi-touch stuff for a while, I'd love to see it in action with Box2d.

I'll show a video, but it might take some time. I've got to get a number of things done (writing the bachelor thesis being the most important one... 😉 ), so please be patient.
Anyway, my game is not the greatest example of Box2D in action... after all, it's just using Box2D to get some reasonable, somewhat realistic feeling into the game, but I had to apply a few "hacks" here and there in order to get the game play right (like modifying linear velocity in some collision cases, etc.).
Hmm, that other exception is quite odd, too, I can't recall ever seeing that. The island is supposed to be big enough to fit every body in the world, so I'm a little confused as to how it could be getting messed up there...am I correct that in your version line 81 of Island.java corresponds to the following?
Code:
    void add(Body body) {
        assert m_bodyCount < m_bodyCapacity;
        m_bodies[m_bodyCount++] = body;
    }


A couple things spring to mind that could have caused that:

- Is your app multi-threaded by any chance? I'm wondering if maybe the UI thread is altering some of the data concurrent with the simulation or something like that. None of this is thread safe, so that could definitely cause some problems.
- Are you creating any objects dynamically, say maybe in response to a collision callback? This is disallowed via an assertion (because it could cause exceptions like the one you've seen and just generally throw things off), but Java by default turns off assertions, so it probably wouldn't have crashed (which reminds me, I really need to turn some of the more important assertions into exceptions!) Creating joints in response to events could do it, too, though the exception would come from a different place, most likely.
ewjordan wrote:
Hmm, that other exception is quite odd, too, I can't recall ever seeing that. The island is supposed to be big enough to fit every body in the world, so I'm a little confused as to how it could be getting messed up there...am I correct that in your version line 81 of Island.java corresponds to the following?
Code:
    void add(Body body) {
        assert m_bodyCount < m_bodyCapacity;
        m_bodies[m_bodyCount++] = body;
    }


Looking at SVN history, I'm pretty sure that's right, because the only recent version where the line numbers in the exception match is revision 134 of World.java, where in line 598 the call to add(Body) is made. (I'm glad I found it out, as the computer I was using had been borrowed and I had to remove everything I installed on it - so there's no chance to look it up in the original setup anymore...)

Quote:
A couple things spring to mind that could have caused that:

- Is your app multi-threaded by any chance? I'm wondering if maybe the UI thread is altering some of the data concurrent with the simulation or something like that. None of this is thread safe, so that could definitely cause some problems.
- Are you creating any objects dynamically, say maybe in response to a collision callback? This is disallowed via an assertion (because it could cause exceptions like the one you've seen and just generally throw things off), but Java by default turns off assertions, so it probably wouldn't have crashed (which reminds me, I really need to turn some of the more important assertions into exceptions!) Creating joints in response to events could do it, too, though the exception would come from a different place, most likely.

The app is single-threaded (at least all the parts that touch JBox2D), and any changes to the world are made outside step() (except linear velocity modifications as already said before). On collision events, the app only "remembers" the collision, but does not create anything and also does not keep references to anything coming from JBox2D. I checked that more than twice in order to be sure. 😉
Could (J)Box2D somehow get "confused" if you're adding and removing a lot of joints? What would happen if you double-remove a joint (crash probably, but where)? I'm really not sure that I'm doing everything right in my code, it was hacked together in a short time and makes use of some awkward constructions, but on the other hand, it always worked when you glued only a few boxes (which was done sooo many times), so I don't see any reason why it should not work with more than a few boxes as well...

I really think it's a good idea to turn those important assertions into exceptions. There's no difference to the user if the app crashes because of a null pointer or other invalid values, or because of a "manual" exception, but the latter can provide useful messages that make it a lot easier for you. 🙂
Hi guys,

I recently came across JBox2d and saw the super cool demos. I loved it, made me want to make games just so I can use the engine! Haha.

In any case, I downloaded the code from svn, but I noticed that there are no build tools used to build the application.

So I decided to put Maven2 (http://maven.apache.org) as the building tool. I reorganized some of the folders and broke the "gui" parts out into their own sub-modules and gave the "engine" its own module.

The work is pretty sloppy for now but it's a start. I have added some Eclipse files as well so that people can import the project easily into the Eclipse IDE.

I'm hoping that this will reduce the entry barrier and making it easy for people interested to help develop the software a starting point.

Due to the gui dependencies on Proccessing and Slick, which both are unavailable on any public Maven servers (not that I'm aware of anyway), I had to add a script to "install" them to the Maven local repository, so that the jar files can be found when we need to compile the gui modules. Again, this will not be how we'd want to manage it, but it's a start. Another thing is that Slick has many dependencies on external jar files. I haven't added them in so the Slick module compiles but doesn't run.

If the current JBox2d developers are curious or would like to give this a try download the zip from http://makas.tzavellas.googlepages.com/trunk.zip and follow these steps to build the application.

1) Download the latest Maven from http://maven.apache.org and follow the instructions to install it.
2) Go to the root directory where the zip file was extracted to.
3) Type mvninstall. This runs a local script that will install the core.jar and slick.jar dependency to Maven's local repo.
4) Type mvn install. This will run the actual Maven build that will compile, package and copy the jar files to Maven local repo, which will then make it possible for others to create their own projects using the jars as Maven dependencies.
5) Well, you're done for now. This will build the jbox2d core engine jar file (residing in target folder in it's module) as well as the jar files for both Processing and Slick.

Some things are not done from this build yet, like generating javadocs and generating a separate source jar (perhaps more?). But Maven supports that out of the box and we just need to set it in the project files.

As a note, don't take the jar files name created as the "final" name, the final jar name can easily be changed in the Maven build scripts.

I will improve on it if people are interested to use Maven as the building tool.

Makas
Yup, a good build process would be nice - I don't have much experience with this stuff (apart from dealing with Eclipse and a little Ant), so I've put it off for a while. When I get a bit of time I'll take a look at Maven and the setup you did, it definitely sounds like it would help things. It's pretty easy for me within Eclipse to export stuff to .jar, but I would definitely prefer to give people building from source an easier way to do so. Thanks for taking some time on this, it sounds like it will be very useful!

(Edit:) btw, the Slick dependency will likely be trimmed out of the next versions of JBox2d (an update is coming soon); its looking at the moment like I just won't have the time to maintain or create any substantial amount of Slick demos (it doesn't help that I'm not very good with Slick, as I've never used it in a project), and getting everything building and running with Slick is a bit more complex than with Processing due to the OpenGL stuff (whereas Processing just requires dropping a .jar in), so for the moment at least it makes sense to remove that stuff from the trunk. Actually, the whole integrations package will likely be removed, as I'm probably not going to have the time to put together any other integrations, and I'm feeling lately that these are better done as projects specific to the game engine rather than as part of JBox2d (for instance, I am working on a PulpCore + JBox2d integration, but we're keeping that separate from the JBox2d source in order to keep things cleaner).

For that matter, I'd be happier without the Processing dependency, too, to keep things as pure as possible, but it would be a bit of work to handle everything that it's doing in the testbed right now, so that will have to wait for another day.
ewjordan wrote:
Yup, a good build process would be nice - I don't have much experience with this stuff (apart from dealing with Eclipse and a little Ant), so I've put it off for a while. When I get a bit of time I'll take a look at Maven and the setup you did, it definitely sounds like it would help things. It's pretty easy for me within Eclipse to export stuff to .jar, but I would definitely prefer to give people building from source an easier way to do so. Thanks for taking some time on this, it sounds like it will be very useful!



No problem, I was hoping that the all the hard work you are putting will have a lot more people interested and of course more games built using the engine. With that in mind, hopefully the workload can be distributed and so that the engine performance and API can be improved (to be more object oriented) with more people actively working on the engine (3 cheers for open source!!).

I'm still studying the code and looking at the example so that I can get the "Hello world" example from Box2d to work using JBox2d and Slick. I have no physics background so there are lot for me to learn first, sigh... My hope is to combine JBox2d into several popular 2d game engine (starting with Slick) so that more people can use it to develop games with ease. I know that SlickSet exist which uses phys2d engine, but I think JBox2d is a lot better so integrating them will provide game developers a better alternative.

Thanks for putting in the effort writing JBox2d, it's a great port and will definitely benefit the game developers which at the end give the gamers better gaming experience 😀

Original thread: https://box2d.org/forum/viewtopic.php?t=36