Box2D + Processing

This tutorial is being expanded into a full chapter of my upcoming Nature of Code book. The library has been updated for JBox2D 2.1.2, however, the page below documents the code for the previous version of JBox2D. Numerous things have changed, most notably the addition of Fixtures. This page will be updated soon to reflect all of the changes.

The Nature of Box2D

Up until now, we’ve been following a fairly linear narrative.

  1. Learn about a concept from the world of physics — What is a vector? What is a force? What is a wave? Etc.
  2. Understand the math and algorithms behind such concept.
  3. Implement the algorithms in Processing with an object-oriented approach.

This has yielded a set of motion simulation examples, allowing us to creatively define the physics of the worlds we build (whether realistic or fantastical). Of course, we aren’t the first to try this. The world of computer graphics and programming is full of source code dedicated to simulation. Just try googling “open source physics engine” and spend the rest of your day pouring through rich and complex code. While many of the libraries out there give us physics (and super awesome advanced physics at that) for free, it’s good that we started with learning the fundamentals from scratch before diving into libraries. For one, without an understanding of vectors, forces, and trigonometry, we’d be completely lost even just reading the documentation of a library. And two, as wonderful a physics engine can be, if you look deep down into your hearts, it’s likely that you seek to create worlds and visualizations that stretch the limits of imagination. A library is great, but it is a limited set of features. It’s important to know both when to live within the limitations in the pursuit of a Processing project and when those limits prove to be confining.

This tutorial serves as a basic introduction to using the Box2D physics engine in Processing.

What is Box2D?

Box2D is an open source C++ physics engines (with many ports to other environments). To learn more, here are a list of links:

If you take a look at the JBox2D page, you’ll notice a collection of terrific demos with a happy little line of text below: “All demos use Processing for graphics rendering.” Awesome! JBox2D must work with Processing! And so we could just stop here. I could point you to that link and say “have fun!” Nevertheless, while the demos are excellent, if you download the source, you’ll notice that it’s a large collection of many many java files. It’s not terribly accessible for the Processing programmer. My goal here is to create a series of simple examples that, although at the moment not as comprehensive as what you will find on jbox2d.org, will help make getting started with Box2D a bit easier.

Is this a Processing Box2D library or a set of Processing examples that use JBox2D?

I have to admit, I’m not entirely sure what I’m doing here. I know I want to make using Box2D easier in Processing, but I also don’t want to hide too much about Box2D. Users need to both take beginner steps as well as dive into the deep expanse of features that is Box2D. As of this version, I’ve got something of a compromise. There is a Processing library (called PBox2D) which is just a little helper class. It sets up your Box2D world for you and provides some functions that you generally have to call over and over again. But to do the real work in your Processing code, you still have to refer to the JBox2D reference and make JBox2D objects.

Three Primary Box2D Concepts

1. World

Manual page: World (note this is the C++ manual).

You Box2D World manages everything inside the world of your simulation. It stores lists of all the bodies present in the world as well as any joints connecting those bodies. When you create a world, you need 3 parameters:

  • AABB or the “axis-aligned bounding box” — This is really just a fancy term for the box that defines the edges of your universe. It’s important because Box2D will ignore anything that exists outside this bounding box.
  • Gravity — When you make a world, you must define a gravity vector, i.e. how strong is gravity and what is gravity’s direction. Gravity doesn’t have to be fixed; you can adjust the gravity vector only the fly while your program is running.
  • doSleep — This is a boolean argument you must pass when creating a world. If you set it to true, it tells the world to allow bodies to sleep when they come to rest. It’s pretty standard to leave this on, but you do have to watch out and sometimes need to “wake up” your objects.

Knowing the above 3 concepts, we can create a Box2D world in our code like so:

AABB worldAABB = new AABB();
worldAABB.lowerBound.set(-100,-100);
worldAABB.upperBound.set(100,100);
Vec2 gravity = new Vec2(0.0, -10.0);
boolean doSleep = true;
World world = new World(worldAABB, gravity, doSleep);

We’re not actually going to include the above code in our sketches, it’s taken care of by the PBox2D helper class. But by examining how a world is created, we’ve opened up two important points regarding working with Box2D.

  1. The Box2D coordinate system is not your pixel coordinate system!! The engine is not tuned for large numbers so if you have a full screen 1024×768 window, everything is going to have to be translated and scaled to Box2D coordinates so that the physics work properly and then back again when it comes time to actually draw something on the screen. Fortunately, the PBox2D helper library takes care of converting between pixel space and Box2D space for you, as we’ll demonstrate in a moment.
  2. We can’t use PVector. Box2D comes with its own vector class: Vec2. It’s right there in the above demo code: Vec2 gravity = new Vec2(0,-10). The good news is that conceptually it’s still just a vector and we’re going to use it in all the same ways as we used PVector. The bad news is some of the syntax will be different, which we’ll see when we look at example code.

Let’s learn how we can convert coordinates from Box2D (“world”) to Processing (“screen”) and vice-versa. But before we do this,let’s quickly return to creating the Box2D world using the PBox2D helper class.

// Create a Box2D object
PBox2D box2d = new PBox2D(this);
// Create a "default" world (we can do more here, but for now this will do)
box2d.createWorld();

Once we have our world, we can take Processing coordinates and turn them into Box2D world vectors and vice versa.

Convert mouseX,mouseY to coordinate in Box2D world:

Vec2 mouseWorld = box2d.coordPixelsToWorld(mouseX,mouseY);

Convert a Box2D world position to Processing screen space:

// Let's assume we have a worldPosition vector, i.e.:
Vec2 worldPosition = new Vec2(-10,25);
Vec2 screenPosition = box2d.coordWorldToPixels(worldPosition);
// We need the "screen position" if we are going to draw something on the screen!
ellipse(screenPosition.x,screenPosition.y,16,16);

Incidentally, Box2D uses real-world measurements in its calculations (meters, kilograms, seconds, etc.) Box2D knows nothing about computer graphics and rendering, it is truly a pure physics engine that just deals with numbers. You give it numbers and you get numbers back. For example, if you say: box2D.setGravity(0,-9.8), you are using real world gravity!

2. Body

Manual page: Bodies

A Box2D body is the primary element in the Box2D world. It has a location. It has a velocity. Sound familiar? The Body is essentially the class we’ve been writing on our own in our vectors and forces examples. A Box2D body can also experience forces. It can also be static (meaning fixed and not moving). It’s important to note, however, that a Body is not a Shape. Bodies carry Shapes around with them (this way a Body can be a single rectangle or a rectangle attached to a circle, etc.) We’ll look at Shapes in a moment, for now let’s see how we make a Body.

The first thing we have to do is create a “Body Definition.” This will let us define the properties about the Body we intend to make.

BodyDef bd = new BodyDef();

Next we can set properties. For example, we might want to position the Body in the center of the Processing window. Remember, if we are going to pass a coordinate to the Body Definition, it must be a Box2D world coordinate, not a screen one!

Vec2 center = box2d.coordPixelsToWorld(width/2,height/2));
bd.position.set(center);

Once we’re done with the definition (BodyDef), we can create the Body itself:

// Create the body!
Body body = box2d.createBody(bd);

And once we have the body, we can set some more properties, such as initial velocity:

// Give it some initial random velocity
body.setLinearVelocity(new Vec2(random(-5,5),random(2,5)));
body.setAngularVelocity(random(-5,5));
3. Shapes

Manual page: Shapes

A Body on its own isn’t anything that physically exists in the world. It’s like a soul with no human form to inhabit. For a Body to have mass, we have to define a Shape and attach that Shape to the Body. Shapes keep track of all the necessary collision geometry attached to a Body. And by specifying a Shape’s density, your Body has mass. Finally, Shapes also have friction and restitution (“bounciness”). One of the nice things about separating the concepts of Bodies and Shapes is that you can attach multiple shapes to a single Body in order to create more complex forms.

Much like with a Body, to create a Shape, we need to first create a ShapeDef instance. For most non-circular shapes, a PolygonDef will work just fine. For example, let’s look at how we define a rectangle.

// Define the shape -- a polygon (this is what we use for a rectangle)
PolygonDef sd = new PolygonDef();

Next up, we have to define the width and height of the rectangle. Let’s say we want our rectangle to be 150×100 pixels. Remember, pixel units are no good for Box2D shapes! So we have to use our helper functions to convert them first.

// Scale to Box2D coordinates
float w = box2d.scalarPixelsToWorld(150);
float h = box2d.scalarPixelsToWorld(100);
// Define the Shape as "Box" (rectangle)
sd.setAsBox(w, h);

Finally, we can set the Shape’s properties:

// Parameters that affect physics
sd.density = 1.0;     // A density of 0 will create a fixed Body that doesn't move
sd.friction = 0.3;    // How much friction
sd.restitution = 0.5; // How bouncy is this Shape?

Once the Shape is defined, all we have left to do is attach the Shape to the Body:

// Attach that shape to our body!
body.createShape(sd);
body.setMassFromShapes();

Note the final function called is setMassFromShapes(). This tells Box2D I’m finished making the Body and you can figure out its mass from all of the attached Shapes (in this case just one). You aren’t limited to calling this function just once. Since you can create or destroy shapes on a body on the fly, you can always call setMassFromShapes() again to adjust the mass accordingly. Also, if you don’t want Box2D to calculate the mass for you (based on the Shape’s density), you can use setMass() instead.

Oh, and in case you were wondering, if your Shape is a circle, you can use a CircleDef:

// Make the body's shape a circle
CircleDef cd = new CircleDef();
// Don't forget to scale the radius!
cd.radius = box2d.scalarPixelsToWorld(10); 
cd.density = 1.0;
cd.friction = 0.01;
cd.restitution = 0.3; 
body.createShape(cd);
body.setMassFromShapes();

Putting the Body all together

Let’s review all the steps we took to construct a Body.

  1. Define a Body using BodyDef (properties such as location can be set)
  2. Create the Body from the Body Definition.
  3. Define a Shape using PolygonDef, CircleDef, or any other Shape definition class (properties such as friction, density, and restitution can be set.
  4. Attach the Shape to the Body
  5. Update the Body’s mass

And here’s the code:

// STEP 1: DEFINE BODY
BodyDef bd = new BodyDef();
bd.position.set(box2d.coordPixelsToWorld(width/2,height/2));
 
// STEP 2: CREATE BODY
Body body = box2d.createBody(bd);
 
// STEP 3: DEFINE SHAPE
PolygonDef sd = new PolygonDef();
float w = box2d.scalarPixelsToWorld(150);
float h = box2d.scalarPixelsToWorld(100);
sd.setAsBox(w, h);
// Parameters that affect physics
sd.density = 1.0;
sd.friction = 0.3;
sd.restitution = 0.5;
 
// STEP 4: ATTACH SHAPE TO BODY
body.createShape(sd);
 
// STEP 5: UPDATE BODY'S MASS
body.setMassFromShapes();

Keeping Track of it all

Once a body is made it lives in the Box2D physics world. Box2D will always know it’s there, check it for collisions, move it appropriately according the forces, etc. It’ll do all that stuff for you without you having to lift a finger! The thing that it won’t do, however, is display the Body for you. This is a good thing. This is your time to shine. When working with Box2D what we’re essentially saying is “I want to be the designer of my world, and I want you, Box2D, to compute all the physics.”

Now, Box2D will keep track of a list of all the Bodies that exist in the world for you. This can be accessed by calling the World object’s getBodyList() function. Nevertheless, what I’m going to demonstrate here is a technique for keeping your own Body lists. Yes, this may be a bit redundant and we perhaps sacrifice a bit of efficiency. But we more than make up for that with ease of use. This methodology will allow us to program like we’re used to in Processing and easily keep track of which Bodies are which and render them appropriately. Let’s consider the structure of the following Processing sketch:

This looks like any ol’ Processing sketch. We have a main tab called “Boxes” and a “Boundary” and “Box” tab. Let’s think about the Box tab for a moment. There, we write a class to describe a Box object, a simple class with a Constructor and a function called display() that draws the rectangle.

// A rectangular box
class Box  {
 
  float x,y;
  float w,h;
 
  // Constructor
  Box(float x_, float y_) {
    x = x_;
    y = y_;
    w = random(4,16);
    h = random(4,16);
  }
 
  // Drawing the box
  void display() {
    fill(175);
    stroke(0);
    rect(x,y,w,h);
  }
}

Our main tab might have a simple program that creates Box objects when the mouse is pressed and draws them on the screen.

// A list for all of our rectangles
ArrayList<Box> boxes;
 
void setup() {
  size(400,300);
  smooth();
  // Create ArrayLists	
  boxes = new ArrayList<Box>();
}
 
void draw() {
  background(255);
 
  // When the mouse is clicked, add a new Box object
  if (mousePressed) {
    Box p = new Box(mouseX,mouseY);
    boxes.add(p);
  }
 
  // Display all the boxes
  for (Box b: boxes) {
    b.display();
  }
}

Now, here’s our assignment. Take the above example verbatim, but instead of simply drawing fixed boxes on the screen, draw boxes that experience physics (via Box2D) as soon as they appear. We’ll need two major steps to accomplish our goal.

1. Add Box2D to our main program (i.e. setup() and draw())

This part is not too tough. We can use the PBox2D helper class and create a PBox2D object, and in setup() initialize it.

// A reference to our box2d world
PBox2d box2d;
 
void setup() {
  // Initialize box2d physics and create the world
  box2d = new PBox2d(this);
  box2d.createWorld();
  // We are setting a custom gravity
  box2d.setGravity(0, -20);
}

Then in draw(), we need to make sure we call one very important function: step(). Without this function, nothing would ever happen! step() takes the box2d world one step further through time. Internally, the engine sweeps through and looks at all of the bodies and figures out what to do with them. Just calling step() on its own moves the Box2D world forward with default settings, however, it is customizable (check PBox2D source documentation for more).

void draw() {
  // We must always step through time!
  box2d.step();
}

2. Store a reference to a Box2D Body object inside of our Box class

Before, our Box class just included fields for location and width and height. Now, for every Box object we create, we also want to create a Body object for the Box2D world, i.e.

// A rectangular box
class Box  {
 
  // We need to keep track of a Body and a width and height
  Body body;
 
  float w;
  float h;

We don’t need (x,y) anymore since, as we’ll see, the Body itself will keep track of its location. Then in our constructor, in addition to initializing width and height, we go ahead and include all of the Body and Shape code we learned above!

  // Constructor
  Box(float x, float y) {
 
    w = random(4,16);
    h = random(4,16);
 
    // Define a polygon (this is what we use for a rectangle)
    PolygonDef sd = new PolygonDef();
    float box2dW = box2d.scalarPixelsToWorld(w/2);
    float box2dH = box2d.scalarPixelsToWorld(h/2);
    sd.setAsBox(box2dW, box2dH);
 
    // Parameters that affect physics
    sd.density = 1.0;
    sd.friction = 0.3;
    sd.restitution = 0.5;
 
    // Define the body and make it from the shape
    BodyDef bd = new BodyDef();
    Vec2 center = new Vec2(x,y);
    bd.position.set(box2d.coordPixelsToWorld(center));
 
    body = box2d.createBody(bd);
    body.createShape(sd);
    body.setMassFromShapes();
 
    // Give it some initial random velocity
    body.setLinearVelocity(new Vec2(random(-5,5),random(2,5)));
    body.setAngularVelocity(random(-5,5));
 }

Ok, we’re almost there. Before we introduce Box2D, it was easy to display an object. The object’s location was stored in variables x and y and we called rect().

  // Drawing the box
  void display() {
    fill(175);
    stroke(0);
    rect(x,y,w,h);
  }

But now Box2D manages the object’s motion. So we can no longer use our own variables to display the shape. But not to fear! Our Box object has a reference to the Box2D Body associated with it. So all we need to do is ask the Body for its location. Since this is a task we’ll need to do quite often, it is taken care of for us by the PBox2D helper:

Vec2 pos = box2d.getBodyPixelCoord(body);

However, let’s take a moment to see how we would do it longhand (without the helper function).

XForm xf = body.getXForm();
Vec2 pos = box2d.coordWorldToPixels(xf.position);

An XForm object describes the transformation state (i.e. location and rotation) of a Body. We can pull out information from it and then using our worldToScreen() function, translate its Box2D location to screen location.

Finally, we can ask the body for its angle of rotation:

// Get its angle of rotation
float a = body.getAngle();

Once we have the location and angle, it’s easy to display the object using translate and rotate (note rotation is in the opposite direction in box2d from Processing so we need to multiply the angle by -1)!

  // Drawing the box
  void display() {
    // We look at each body and get its screen position
    Vec2 pos = box2d.getBodyPixelCoord(body);
    // Get its angle of rotation
    float a = body.getAngle();
 
    rectMode(CENTER);
    pushMatrix();
    translate(pos.x,pos.y);
    rotate(-a);
    fill(175);
    stroke(0);
    rect(0,0,w,h);
    popMatrix();
  }

In case we want to have objects that can leave the physics, it’s often useful to also include a function to destroy a Body, i.e.

  // This function removes the particle from the box2d world
  void killBody() {
    box2d.destroyBody(body);
  }

Coming Next (check out examples on github)

  • Demonstrate how to apply forces to objects
  • Demonstrate how to interact with the mouse
  • Show some advanced structures — chains, bridges, blobs, curvy edges, compound shapes
  • Detecting collisions with ContactListener
  • multiple shapes attached to one body
  • http://profiles.google.com/damarusama geoffroy tremblay

    Thanks for all the good code!!

    as soon as I have ArrayList particles; in my code processing stop and give me an “unexpected token void”, of course if I comment out ArrayList particles; it spits out an error later on in the code (because the arraylist is not initialized)

    Would it be my version of processing or java that is not happy ?

    thanks!

  • http://twitter.com/damarusama gef

    just had the wrong processing version :S

  • http://profiles.google.com/jaisbell James Isbell

    Hi, First of all THANKS!! i feel like I’ve stumbled on a gold mine, I have a few questions though.

    Just wondering what happened to the google code repository?
    Also all these examples seem to show gravity going in the -y direction but nothing for as if your looking down from above. for example a pool table game looking down on the table does not need downwards gravity but still needs the physic’s collision, velocity, friction etc

    I would have thought it would be posible but me being a noob at all this its kinda hard to work it out. Got any tips?

    Many Thanks

  • http://www.facebook.com/shiffman Daniel Shiffman

    Google code has been replaced by github. See: https://github.com/shiffman/PBox2D

    You can turn off the default gravity by saying:

    box2d.setGravity(0,0);

  • http://twitter.com/Ernaem Nikita Rokotyan

    Thanks a lot for all the tutorials and books you wrote!

    I’ve one little question about pbox2d. Is it possible to create arbitrary dynamic shape from vertices? I had tried everything, but i can’t do it in Processing. EdgeChainDef is static. PolygonDef gives NullPointerException when creating shape.

    Here is sample code:

    ArrayList mVerticesList;
    mVerticesList.add( new Vec2( x, y ) );

    PolygonDef pd = new PolygonDef();
    //pd.vertexCount = mVerticesList.size(); <—— There is no such field
    pd.vertices = mVerticesList;

    pd.density = 1.0f;
    pd.friction = 0.3f;
    pd.restitution = 0.6f;

    BodyDef bd = new BodyDef();
    bd.position.set( Vec2( 0, 0 ) );

    body = box2d.createBody(bd);
    body.createShape(pd); <—— Error here (NullPointerException)
    body.setMassFromShapes();

    Thx!

  • http://twitter.com/Ernaem Nikita Rokotyan

    Try to add following line of code after ” ArrayList particles”:
    particles = new ArrayList();

  • http://twitter.com/shiffman Daniel Shiffman
  • http://twitter.com/Ernaem Nikita Rokotyan

    Many thanks!

  • http://www.facebook.com/allan.james.berry Allan Berry

    Hi Daniel,

    Do you think these arbitrary shapes can be generated by SVG files? Is there some way to iterate over vertices in an SVG, and set the myVerticesList to those vertices?

    I have a directory of some 200 SVG files, and want to draw them on the screen at runtime.

    Many thanks, btw… your tutorials have helped me many times before now, and I’m grateful. I hope sometime soon I know enough about Processing to help others in similar fashion.

  • http://twitter.com/shiffman Daniel Shiffman

    Take a look at this thread on the Processing forum.

    http://forum.processing.org/topic/parse-svg-to-processing-shape-code

  • http://www.facebook.com/allan.james.berry Allan Berry

    tremendous. I think that will do the trick.

  • http://twitter.com/wind_up_toy Wind Up Toy

    Daniel, thank you so much for PBox2D, it’s super easy to use.

    I’ve been making some really complicated Box2D shapes and it would take quite a bit of time to write the code to have Processing actually draw them. How do I use the debugdraw features of JBox2D in my Processing sketch?

    Thanks!

  • Anonymous

    If you download the jbox2d source there are many examples that use debugdraw. I’d suggest starting there. Good luck!

  • http://www.facebook.com/allan.james.berry Allan Berry

    is it possible to define a shape, via an SVG file perhaps, with curved lines?  The thread you point to deals with polygons… what about curvy shapes besides circles?

  • Anonymous

    My strategy is always to tell Box2D it’s a polygon and then draw it with curvy lines.  If your polygon is close the physics will really look just fine.  I’m pretty sure stuff like Angry Birds, etc operates on this principle. 

  • Christopher Coleman

    Dan, big fan, and backer for the new book.  I am trying to link up a series of boxes with revolute joints in order to make a tree, but for the life of me I cannot see how to properly structure the chain of joints and boxes.  I looked at the windmill example, but need just one more joint/box combo to understand how to get everything relative to each other as it doesnt make sense to me how to properly define the anchor points since i want to be able to pinpoint them relative to each box, not the world.

  • Anonymous

    This is a great idea for yet another example!  I will try to remember to add one.  For now, my suggestion would be to just try expanding the windmill object to have three bodies, and two joints, i.e.

    Body box1
    Body box2
    Body box3

    RevoluteJointDef rjd1;
    RevoluteJointDef rjd2;

    rjd1.body1 = box1.body;
    rjd1.body2 = box2.body;

    rjd2.body1 = box2.body;rjd2.body2 = box3.body;Hope this helps!

  • Mario

    Hi,

    Thanks to your article I’m now using processing and you PBox2d to simulate my scene.
    But I have a small problem. I can’t get correct simulation for my polygon.
    Polygon is anti-clockwise less or equal 8 vertex. Coords of my polygon are coming from screen, I’m drawing polygon by clicking mouse.

    How to properly create PolyDef to use polygon ?

    I’m doing something like this:

    centerF = pArea.getCenterOfMass();
                center = new Vec2(centerF.x, centerF.y);
                for (int i = 0; i < pArea.getPoints().length; ++i) {
                    polyDef.addVertex(box2d.coordPixelsToWorld(new Vec2(pArea.getPoints()[i].x, pArea.getPoints()[i].y)));
                }

    where pArea.getPoints()[i] is vector with screen coords.

    I'm trying to display this, but it's still don't work :(

    My display method:

            Vec2 pos = box2d.getBodyPixelCoord(body);
           
            float angle = body.getAngle();
           
            pApplet.rectMode(pApplet.CENTER);
            pApplet.pushMatrix();

            pApplet.translate(pos.x, pos.y);
            pApplet.rotate(-angle);
            pApplet.fill(175);
            pApplet.stroke(1);
           
            if (image!=null){
                //pApplet.image(image, -image.width/2, -image.height/2);
            }
            pApplet.fill(113);
            pApplet.beginShape();
            PolygonShape shape = (PolygonShape)body.getShapeList();
            for (int i=0; i < shape.getVertexCount(); i++){
                 pApplet.vertex(pArea.getPoints()[i].x, pArea.getPoints()[i].y);
            }
            pApplet.endShape();       
                   
            pApplet.popMatrix();

    Could you give me some advices ?

  • Shiny

    Hi,

    I am trying to incorporate some PBox2d stuff with the xbox kinect. I am trying to use the CollisionsAndControl example with your hand being the mouse. Anyways, that’s not important.. In that example, I added the Boundary class so the particles will stay on the screen. But now, the addContact method won’t work properly when either a particle or the box touches any boundary. Is there any way around this?

    I made it so the addContact method is this now:

    if (c1.contains(“Box”) && c2.contains(“Particle”)) {
        Particle p = (Particle) o2;
        p.change();
      }
      // If object 2 is a Box, then object 1 must be a particle
      else if (c2.contains(“Box”) && c1.contains(“Particle”)) {
        Particle p = (Particle) o1;
        p.change();
      }

    so the boundaries do not get mistaken as ‘touches’. I want the color of the particles to only change when they hit the box. Is there something in the Boundary class that makes the library not recognize it as an object with a shape?

    If anybody could help with this, it would be much appreciated. Thanks.

  • Anonymous

    As long as you add userData() the same way to the Boundary class (see how Particle and Box do it), the function will still work.  You can also check to see if the user data is null in the addContact() method.

  • matias benedetto

    Hi Daniel. Thanks for the tutorial;

    Sometimes the function body.setMassFromShapes(); throws a assertionError. Any idea about that?

  • Anonymous

    Hmm, I’m not sure about that.  I am sometime this summer going to release a brand new version all with Box2D 2.0 though so hopefully any bugs here will go away with the next version (and there will be new fun bugs as well I’m sure)

  • Omid

    Hi Daniel,
    Thanks for the work and tutorial. This should really help in quick physics prototyping in my apps.

    I face a problem with importing pbox2d (and jbox2d) into processing. I tried adding the pbox2d whole folder, as well as the the jar files separately into a new created folder called ‘libraries’ in my sketchbook folder. Processing cant seem to locate them. 
    I am assuming lot of processing newbies would be facing this issue. Could you please advice what might be wrong?

  • Omid

    fixed. i think it was the case mismatch in the library and the source, though i have no idea if it matters.

  • Nasirus1983

    I see that my last post was ignored – that’s pitty, but I resolved my problem.
    Maybe this time I’ll get answer for my next question.

    What is difference beetwen something like this:

    Vec2 positionBody = new Vec2(
                    (shape.getX() -centerFW.x)/ box2d.scaleFactor,
                    (-shape.getY() -centerFW.y)/ box2d.scaleFactor);

    and
           
    Vec2 positionBody = box2d.coordPixelsToWorld(new Vec2(center.x, center.y));

    and in display:

    Vec2 pos = box2d.getBodyPixelCoord(body); // wspó?rz?dne processing

    and

    Vec2 pos = new Vec2((body.getWorldCenter().x+centerFW.x),
                            (body.getWorldCenter().y+centerFW.y));

    Both working but on different body positions ?
    I wolud like to export those positions for example to andengine or Flash box2d or jbox2d but they don’t work if I use coordPixelsToWorld and getBodyPixelsCoord, but works with dividing with scalefactor ?

    Has processing using some different coordinates ?

  • Anonymous

    You can check the source to see exactly how the coordinates are being transformed.   It’s essentially identical to what you are doing, but if you want total control over doing it manually isn’t such a bad idea.

  • Nasirus1983

    Thanks for answer. Yes, I was looking into source, and those calculations are a little bit complicated for me. I’ve noticed that, if I do this manualy I can’t get testpoint for shape working correctly.

    Maybe you will knows this. But coordPixelsToWorld nad vice-versa isn’t working for actionscript box2d. I’ve noticed that in box2d actionscript, body position is set to screen x and y divided by physics scale.

    I don’t understand this, because processing and actionscript both have (0,0) screen point in top left corner but in processing you are making complicated calculations for box2d and in actionscript people just divide screen position by physics scale.

    Do you have any idea ?

    p.s.
    Thanks for answering for previous question.

  • Anonymous

    In box2d the world origin is 0,0 and up is positive (as opposed to a Processing graphics window where the world origin is 0,0 and down is positive.) i assume the same is true in actionscript but haven’t worked in that environment with box2d.  good luck!

  • Nasirus1983

    If it was true for actionscript, then I should make calculations as you do in PBox2d. But in actionscript it’s enought to take screen coords and divide them by physics scale( Besides in actionscript every example has gravity with positive value for y axis.) But I can’t get anywhere confirmation of that. Anyway thanks for answer.

  • Ethan Hughes

    You keep referencing “physics scale” I need to know this scale because I am in fact using Box2D-js, which is basically a port from Box2D actionscript, so I would grately appreciate if you could give me an exact ratio for what to use so I can use the same…?

  • Dekamayro

    I’m probably ignoring something obvious, but is there a specific way to stop an object from rotating? I can’t seem to find any function to do so that works with PBox2D.

  • Anonymous

    you can try setting an object’s angular velocity to 0?  

      body.setAngularVelocity(0);

  • Dekamayro

    *facepalm* I should’ve thought of that :(

    Thank you so much!

  • Dekamayro

    Actually, that doesn’t work for some reason…The object doesn’t spin but it can still rotate. Is there some sort of fixedRotation(boolean) function? Sorry I’m asking a lot of questions, but I just jumped into PBox2D.

  • Anonymous

    You can always choose not to rotate the object when you draw it, and additionally you can set the object’s angle (via the body) directly in box2d.

  • Dekamayro

    I tried both of your suggestions: the first merely makes a non-rotating shape while the box2d object does rotate; for the second one I can’t find any method for the desired effect. I looked in the box2d manual, but the fixedRotation(boolean) function doesn’t exist. Neither does a setAngle(var) or body.angle.set(var). Could you point me to the specific method to do this or, even better, to a list of PBox2D methods that might’ve eluded me?

    P.S. Thanks for responding (most people I know wouldn’t) and sorry for the question overload, I’m just that much of a newb :( .

  • Anonymous

    Oh, found one! Try:

    BodyDef bd = new BodyDef();       
    //etc.   
    bd.fixedRotation = true;

    as long as you don’t give the body any angular velocity it should work.

  • Dekamayro

    Sir, 

    my eternal gratitude 
    is thine. :D
    Happy holidays and thank you so much.

  • Olavo Peres

    Hi Shiffman,
    thanks for your great work!!!
    I have a question about the MouseJoint with multiples objects in Box2D, I try to move one at a time with the mouse.
    I use your Mouse exemple (MouseJoint) but with a ArrayList to make many boxes, 
    it’s compile but when I click it crash.
    My changes in spring class (ArrayList in void bind) don’t work too…
    Can you help me?
    thanx!

  • Dstavy

    i have got an assertion error when trying to add few hundred of falling circles
    any idea?
    Exception in thread “Animation Thread” java.lang.AssertionError at org.jbox2d.collision.BroadPhase.createProxy(BroadPhase.java:226) at org.jbox2d.collision.shapes.Shape.createProxy(Shape.java:337) at org.jbox2d.dynamics.Body.createShape(Body.java:308) at critical_mass$Particle.makeBody(critical_mass.java:615) at critical_mass$Particle.(critical_mass.java:562) at critical_mass.draw(critical_mass.java:257) at processing.core.PApplet.handleDraw(Unknown Source) at processing.core.PApplet.run(Unknown Source) at java.lang.Thread.run(Thread.java:722)

  • http://ignivome.com/ Ignivome

    Thanks a lot Shiffman to always make everything easier ;)

    FYI
    I notice a small bug on your CollisionsAndControl lib example.
    Particle class/ done method: you wrote
        // Is it off the bottom of the screen?
        if (pos.y > height+r*2) {,

    instead, i’d write
        if (body.isFrozen())

    because, if the particle goes outsite by the left, top or right side, killBody() wont be called and arrayList will keep unused particles.
    Little memory leak there ;)

    thanks again

  • Anonymous

    Thanks for the tip!

  • Siarfnopmop

    Hi!

    Sorry programming is a brand new thing for me, I know processing since a few month only and I would like to do something with box2d

    I have a specific question, is it possible to edit the density of an object the fly with pbox2d ?

    I found this thread http://www.box2d.org/forum/viewtopic.php?f=19&t=6089
    but actually i’m totally lost uhuh

    sorry for my dumb question :D

  • Anonymous

    If you store a reference to the Shape object you can reset its density and then update its mass accordingly.  The forum post is for Box2D 2.0 (my library still uses the old Box2D).  I’ll be updating it very soon!

  • Siarfnopmop

    youhouh ty so much :) i’m waiting for the final version
    thx again! ?

  • Dekamayro

    Hi (it’s me again)! Do you have any suggestions as to how to make a box2d world sidescroll? I’ve been trying to make a room bigger than one screen (my size is (800, 600)) and only display things a certain distance from a character, but that doesn’t work. Also, when I try to make a wall way out of the size – like at 1990, 300 – I get an assertion error. Does that have to do with PBox2D’s reliance on the size(float x, float y) to determine the size of its world? 

    Thanks.

  • Anonymous

    Sadly, my answer to this again is if you wait for the next  version of the library it won’t have this limitation!  Working on it now to release in the next few weeks. (you can go and use JBox2D directly if you don’t want to wait: http://code.google.com/p/jbox2d/)

  • Dekamayro

    All right, thank you. :)

  • Nick Inzucchi

    Hi Daniel!  Thanks for the great wrapper. Any way to set a body’s angle directly? Looks like body.setTransform(Vec2 position, float angle) is not implemented?

  • Anonymous

    setTransform() is exactly what you are looking for.

    Vec2 position = body.getPosition();
    body.setTransform(position, someangle);