how to use box2d ?

missmiss's icon

dose anyone have tutorials or some suggestions about how to use box2d ? like make user's own shape?

this is the download address: http://charles.bascou.free.fr/box2d/

Rob Ramirez's icon

i would recommend using the jit.phys externals for this. search for the phys.world.2D example patch for an example of using jit.phys.world in 2D mode. also check here: https://cycling74.com/tutorials/introduction-to-jit-phys-part-1

Roland Sproll's icon

I am looking for a way to use custom 2d polygon meshes in jit.phys.world, such as the gear in this example: https://libgdxinfo.wordpress.com/box2d-importing-complex-bodies/

Rob Ramirez's icon

Here's a basic example using the starfish.obj as your gear. The key for 2D is the remove_plane message to a phys.world

Max Patch
Copy patch and select New From Clipboard in Max.


Roland Sproll's icon

That has already helped me a lot, thank you very much.

Unfortunately, I am now stuck with the problem of creating different polygons in any number that behave physically. It seems logical to me that only the last mesh data sent to jit.phys.body are handled, although all others are rendered.

What is the best way to insert different shapes of any number into the physical 2D world?

Max Patch
Copy patch and select New From Clipboard in Max.

Rob Ramirez's icon

you'll need a jit.phys.body for each shape you want in the world. you can use a poly~ abstraction if you want that dynamic, or better yet some simple javascript gives you so much flexibility.

Max Patch
Copy patch and select New From Clipboard in Max.

Roland Sproll's icon

Thanks Rob, that's exactly what I needed.

Hopefully my last question: is there an example of how to subdivide (concave) polygons to fill them properly?

Roland Sproll's icon

In my project, the shapes are generated algorithmically. It happened that shapes were created that only consisted of 2 points. Of course this makes no sense, but it caused jit.phys to suddenly crash Max. It took me some time to localize this error, as Max does not give any hints in such a case. Maybe this will help some people to recognize the error faster and hopefully it can be avoided in a newer Max version.

Max Patch
Copy patch and select New From Clipboard in Max.

If anyone has an example of how to subdivide (concave) polygons to fill them properly, I would still be very happy.

Rob Ramirez's icon

Sorry, meant to reply to this when you first sent it out. Use jit.geom operators to subdivide meshes.

I can reproduce the crashing. I'll see what can be done to catch those errors before sending to bullet.

Max Patch
Copy patch and select New From Clipboard in Max.


Rob Ramirez's icon

also, especially if making use of jit.geom ops, but even in the case of your patch, don't bang things unnecessarily. You are banging your geometry matrix out every frame, and that is causing the physics to behave poorly. You should only send a matrix out if something has changed.