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: 806
  • src_JBox2D_11.zip
    294.7 KB · Views: 597
Last edited:

Informatix

Expert
Licensed User
Longtime User
I see. So you simply shortnamed the classes to expose them into B4A.
I believe that is enough, because we could create a class to wrap the jbox2d functions and types.
This is very interesting for me, but I have to see when I get time to play with this.
Thanks :)

I did a little bit more than that because each class needs a constructor without parameter (and consequently an Initialize function). I had also to create a function with a new name for all the set() functions and clean a few things, but it was an easy work and in one day I produced the current version. Wrapping everything would be silly IMHO.
The next step, if that's needed by someone, is to implement the callbacks. That will need real programming behind because we cannot use them out of the box.
 

urikupfer

Member
Licensed User
Longtime User
Hi infomatix
I am trying to loop list of bodies :

Dim b As jbBody
b=World.BodyList
Dim i As Int=0

Do While (b<>Null)
b=b.Next <------------the error line
position = b.position
angle = b.angle

smiley(i).DestRect.Left = position.x * 60 + 20%x
smiley(i).DestRect.Top = 95%y - (position.y * 20%y)
smiley(i).DestRect.Right = smiley(i).DestRect.Left + size
smiley(i).DestRect.Bottom = smiley(i).DestRect.Top + size
smiley(i).Rotate = angle
i=i+1
Loop

But I get this strange error
"Parsing code. Error
Error parsing program.
Error description: Missing Keyword: loop
Occurred on line: 164
b=b.Next"
 

bluejay

Active Member
Licensed User
Longtime User
I have not had a chance to look at this library so based only on the code you have shown I have two suggestions:

1. you Dim b as a Body but assign it a BodyList so my guess is that NEXT is not a property of b

2. for a linked list the b=b.next would normally be in the line before loop

bluejay
 

urikupfer

Member
Licensed User
Longtime User
The world contains the bodies as a linked list (they can be created dynamically) and it have the property bodylist which point to the first body , the next property point to the next one.
 

Informatix

Expert
Licensed User
Longtime User
Hi infomatix
I am trying to loop list of bodies :

Dim b As jbBody
b=World.BodyList
Dim i As Int=0

Do While (b<>Null)
b=b.Next <------------the error line
position = b.position
angle = b.angle

smiley(i).DestRect.Left = position.x * 60 + 20%x
smiley(i).DestRect.Top = 95%y - (position.y * 20%y)
smiley(i).DestRect.Right = smiley(i).DestRect.Left + size
smiley(i).DestRect.Bottom = smiley(i).DestRect.Top + size
smiley(i).Rotate = angle
i=i+1
Loop

But I get this strange error
"Parsing code. Error
Error parsing program.
Error description: Missing Keyword: loop
Occurred on line: 164
b=b.Next"

EDIT:
I'm going to change the function name since Next is a B4A keyword and the B4A parser is unable to do the difference (its syntactic analysis does not work well here).
 
Last edited:

PaulR

Active Member
Licensed User
Longtime User
Just a note to say I am very interested in this library. I am short on time at the moment to do anything with it, but having Box2D ported to b4a is great news.

Thanks.
 

NFOBoy

Active Member
Licensed User
Longtime User
jbBody and contactList

So, trying out this library (it appears to be very rich and full of many nice features).

However, for detecting when a contact occurs, I think either the CallBack functions BeginContact() and EndContact() need to be implemented.

Or (the slower method) the world.contactList needs to be fixed

Right now, after each Step, the ContactList contains a list of jbContacts, unfortunately, I cannot instantiate a jbContact, and there is no getNext function (there is the Next function... which is same issue as you had before with Fixture and Bodies, it won't compile)

So right now, I don't see how to 'detect' collisions, so my examples I'm working on look neat... but don't know how to put in sound to match, as I can't detect the collisions.

Ross
 

NFOBoy

Active Member
Licensed User
Longtime User
Sure,
but man is my code ugly compared to yours! :)

I have put in the comments where I tried to look for contact occuring, but I am stymied by the Next command.

Listeners... woo hoo!

Thanks,

Ross
 

Attachments

  • BouncingBalls Demo.zip
    9.9 KB · Views: 249
Last edited:

NFOBoy

Active Member
Licensed User
Longtime User
Attached is a new demo involving dominos. (with old one as well)

Also included is a library for converting World Coordinates to Display Coordinates. Not fully fleshed out, but very workable, and used to make the second demo a little more interactive. (double tap to zoom and center, single tap to drop a ball, push and hold to zoom out)

I used your free Gesture Detector library for some of the interaction, but could probably do a whole lot more with the donation ware, so a donation will be headed your way shortly. (I can't go anywhere near to the right of your suggestion.. :) )

Of note, do NOT try to make objects smaller than 5 centimeters... or you may find yourself hitting your head on why the bmp's do not overlay your objects correctly... (trust me)

More demos soon
 

Attachments

  • jBox2dDemos.zip
    86.8 KB · Views: 296

NFOBoy

Active Member
Licensed User
Longtime User
Pre and Post Solve listeners

Fred,

I mentioned BeginContact and EndContact listeners as being vital, and PreSolve, PostSolve not as much, but in looking at more examples to build, I came across this:

PreSolve and PostSolve

Both PreSolve and PostSolve give you a b2Contact pointer, so we have access to the same points and normal information we just looked at for BeginContact. PreSolve gives us a chance to change the characteristics of the contact before the collision response is calculated, or even to cancel the response altogether, and from PostSolve we can find out what the collision response was.

Here are the alterations you can make to the contact in PreSolve:

1
2
3
4
5



void SetEnabled(bool flag);//non-persistent - need to set every time step

//these available from v2.2.1
void SetFriction(float32 friction);//persists for duration of contact
void SetRestitution(float32 restitution);//persists for duration of contact

Calling SetEnabled(false) will disable the contact, meaning that the collision response that normally would have been applied will be skipped. You can use this to temporarily allow objects to pass through each other. A classic example of this is the one-way wall or platform, where the player is able to pass through an otherwise solid object, depending on various criteria that can only be checked at runtime, like the position of the player and which direction direction they are heading, etc.

It's important to note that the contact will revert back to being enabled in the next time step, so if you want to disable contacts like this you'll need to call SetEnable(false) every time step.

As well as the contact pointer, PreSolve has a second parameter from which we can find out info about the collision manifold of the previous time step. If anybody has an idea about what this could be used for, let me know :D

PostSolve is called after the collision response has been calculated and applied. This also has a second parameter, in which we can find information about the impulse that was applied. A common use for this information is to check if the size of the collision response was over a given threshold, perhaps to check if an object should break, etc. See the 'sticky projectiles' topic for an example of using PostSolve to determine whether an arrow should stick into a target when it hits.

So, if you are able to add BeginContact and EndContact listeners, these would probably be good too.

Ross
 

NFOBoy

Active Member
Licensed User
Longtime User
ChainShape / EdgeShape

For my next demo, I want to make it such that the user draws on the screen, and the map of points is converted into either an Edge Shape or Chain Shape (Chain Shape seem preferable), but I do not see how to access either of those shapes. Am I missing how to access them?

Ross
 

Informatix

Expert
Licensed User
Longtime User
So, if you are able to add BeginContact and EndContact listeners, these would probably be good too.

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

I tested BeginContact and EndContact with your demo (thanks again) and the two events work fine.
The demo has a little error: I had to rename mysmiley to smiley.png.
 
Top