B4A Library JBox2D - A physics engine for advanced users

Hello,

Here's my adaptation of JBox2D, the popular Java physics engine (and probably one of the best available). This is not a wrapper around the library but an adaptation to B4A. Pro: no impact on performance. Con: the object initialization is a bit loose (some objects are initialized with a simple Dim, some need a call to the Initialize function; and IsInitialized is not implemented).
The source code is available below.

The documentation can be found on the net:
Box2D manual
The JBox2D site

If you want to know what JBox2D can do for you, download the testbed or try the twelve examples provided with the library.

Beta 2:
I fixed the issue with Next. Next for bodies is renamed NextBody, and Next for fixtures is renamed NextFixture.

Beta 3:
I fixed the remaining issues with Next. Next for contacts is renamed NextContact, and Next for joints is renamed NextJoint.
I added four events: BeginContact, EndContact, PreSolve, PostSolve. To take them into account, use the jbWorld function: setContactListener.

1.0:
I updated the code with the latest version available (2.2.1.2). There are many changes and bugfixes since the beta version.
I ported also ten "official" examples.

1.1:
I added three classes: jbQueryCallback, jbTreeCallback and jbTreeRayCastCallback;
I added a class, jbBodyEditorLoader, that allows to load JSON files created by Physics Body Editor v2;
I added two new examples (Web and Physics Body Editor Loader).

About the examples:
All examples provided with this library may run very slowly on your device because:
1) These examples use a drawing method (drawDebugData) that is convenient but very slow;
2) The interval between each step is set to a high value.
I tried to keep the original settings, so you'll get the same result as in the testbed, but these settings are too demanding for our pocket devices.

You need the Accelerated Surface library to run the examples.
 

Attachments

  • JBox2D_11.zip
    345.5 KB · Views: 893
  • src_JBox2D_11.zip
    294.7 KB · Views: 671
Last edited:

NFOBoy

Active Member
Licensed User
Longtime User
obfuscation

Frederic,

updated the bouncing ball demo to include sound (and the right smiley!)

Of note, if compiled obfuscated, the listeners are not getting called.

I know you are having to re-do... so wanted you to note that.



(tap on the screen to start/add another ball at that position, double tap to zoom out and reset)
Ross
 

Attachments

  • jBox2dDemos.zip
    93.8 KB · Views: 296

NFOBoy

Active Member
Licensed User
Longtime User
Frederic, I know you mentioned you were busy, and this might be at your lower end of your priority loop, but any chance the compile with obfuscation can be quickly addressed for the listeners? (that brings me to a dead halt with trying to make something for the market... probably irrational fear on my part :) )
 

Informatix

Expert
Licensed User
Longtime User
Frederic, I know you mentioned you were busy, and this might be at your lower end of your priority loop, but any chance the compile with obfuscation can be quickly addressed for the listeners? (that brings me to a dead halt with trying to make something for the market... probably irrational fear on my part :) )

I'm not at home for the week, so I cannot do anything now, but I looked at this problem lately and ... didn't find it. If you want to run your code with the obfuscation mode, use a valid event name (with a _ somewhere in its name, World_BeginContact for example). That's all. I did nothing wrong in my code.
 

NFOBoy

Active Member
Licensed User
Longtime User
Sounds good, I will give that a try! Hope you are at least having a good time where you are at away from home!
 

ilan

Expert
Licensed User
Longtime User
realy great,

i am trying to use in the physic engine images instead od drawing, but its not working
is it possible to use images? like cubes that fall down,...

thanx
 

ilan

Expert
Licensed User
Longtime User

Informatix

Expert
Licensed User
Longtime User

ilan

Expert
Licensed User
Longtime User
hi @Informatix

i managed to create a b4j example with your lib JBox2D (https://www.b4x.com/android/forum/threads/box2d-for-b4j.75112/)

although i could not use the DebugDraw option.
the reason is the DebugDraw option is using ints for colors since android is using its but b4j needs fx.colors

so we need to convert the ints to fx.color. i dont know if there are other issues with the debugdrawing option but if you could update the lib to output also fx.colors when compile in b4j i could try the rest.

for now its seem to work pretty well. i am drawing with canvas the whole bodies but having the debugdraw option is very important to ensure that the images are really in the right position.

thank you, ilan
 
Last edited:

Informatix

Expert
Licensed User
Longtime User
hi @Informatix

i managed to create a b4j example with your lib JBox2D (https://www.b4x.com/android/forum/threads/box2d-for-b4j.75112/)

although i could not use the DebugDraw option.
the reason is the DebugDraw option is using ints for colors since android is using its but b4j needs fx.colors

so we need to convert the ints to fx.color. i dont know if there are other issues with the debugdrawing option put if you could update the lib to output also fx.colors when compile in b4j i could try the rest.

for now its seem to work pretty well. i am drawing with canvas the whole bodies but having the debugdraw option is very important to ensure that the images are really in the right position.

thank you, ilan
Good news.
jBox2D is now an abandoned project and I won't improve the code or add new things, but I will try to provide a solution for the color issue.
 

ilan

Expert
Licensed User
Longtime User
hi again :)

i am playing a little bit with jBox2d and B4J (https://www.b4x.com/android/forum/threads/tank-attack-box2d.75233/#post-477576)

i would like to catch the contacts between the bodies so i add an event

B4X:
Sub world_BeginContact(Contact As jbContact)
    Dim contact1 As typecontact
    contact1.body1 = Contact.FixtureA.Body
    contact1.body2 = Contact.FixtureB.Body

    If contact1.body1.UserData Is bodyinf And contact1.body2.UserData Is bodyinf Then
        Dim bodyinf1 As bodyinf = contact1.body1.UserData
        Dim bodyinf2 As bodyinf = contact1.body2.UserData
        Log(bodyinf1.id & " hit " & bodyinf2.id)
    End If
End Sub

my problem is that when i intialize "world" i have no option to add an event name. the only thing that i can put is the gravity and i could not find where to set the event name for the world events.

B4X:
world.Initialize(vGravity)

am i missing something?

thank you, ilan
 

Informatix

Expert
Licensed User
Longtime User
hi again :)

i am playing a little bit with jBox2d and B4J (https://www.b4x.com/android/forum/threads/tank-attack-box2d.75233/#post-477576)

i would like to catch the contacts between the bodies so i add an event

B4X:
Sub world_BeginContact(Contact As jbContact)
    Dim contact1 As typecontact
    contact1.body1 = Contact.FixtureA.Body
    contact1.body2 = Contact.FixtureB.Body

    If contact1.body1.UserData Is bodyinf And contact1.body2.UserData Is bodyinf Then
        Dim bodyinf1 As bodyinf = contact1.body1.UserData
        Dim bodyinf2 As bodyinf = contact1.body2.UserData
        Log(bodyinf1.id & " hit " & bodyinf2.id)
    End If
End Sub

my problem is that when i intialize "world" i have no option to add an event name. the only thing that i can put is the gravity and i could not find where to set the event name for the world events.

B4X:
world.Initialize(vGravity)

am i missing something?

thank you, ilan
You should look at the B4A example "CollisionProcessing" or at the list of functions in the IDE. You should find SetContactListener.
 

ilan

Expert
Licensed User
Longtime User
You should look at the B4A example "CollisionProcessing" or at the list of functions in the IDE. You should find SetContactListener.


:oops::oops::oops: thank you, i dont know how i missed that command. i looked for something like this but could not find and now i see it.

thanx a lot :)
 
Top