What is the difference between "PBox2D library"and "box2d_processing library"?

i am confused about the “PBox2d” in book The Nature of Code.

when i tried run the example ,
the main tab

import pbox2d.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;

// A list for all of our rectangles
ArrayList<Box> boxes;

PBox2D box2d;		

void setup() {
  size(800, 200);
  smooth();
  // Initialize and create the Box2D world
  box2d = new PBox2D(this);	
  box2d.createWorld();

  // Create ArrayLists
  boxes = new ArrayList<Box>();
}

void draw() {
  background(255);

  // We must always step through time!
  box2d.step();

  // When the mouse is clicked, add a new Box object
  Box p = new Box(mouseX, mouseY);
  boxes.add(p);

  // Display all the boxes
  for (Box b: boxes) {
    b.display();
  }
}

the box tab:

// A rectangular box
class Box {
  //  Instead of any of the usual variables, we will store a reference to a Box2D Body
  Body body;      

  float w,h;

  Box(float x, float y) {
    w = 16;
    h = 16;

    // Build Body
    BodyDef bd = new BodyDef();			
    bd.type = BodyType.DYNAMIC;
    bd.position.set(box2d.coordPixelsToWorld(x,y));
    body = box2d.createBody(bd);


    // Define a polygon (this is what we use for a rectangle)
    PolygonShape ps = new PolygonShape();
    float box2dW = box2d.scalarPixelsToWorld(w/2);
    float box2dH = box2d.scalarPixelsToWorld(h/2);	// Box2D considers the width and height of a
    ps.setAsBox(box2dW, box2dH);		        // rectangle to be the distance from the
                 					// center to the edge (so half of what we
                					// normally think of as width or height.) 
    // Define a fixture
    FixtureDef fd = new FixtureDef();
    fd.shape = ps;
    // Parameters that affect physics
    fd.density = 1;
    fd.friction = 0.3;
    fd.restitution = 0.5;

    // Attach Fixture to Body						   
    body.createFixture(fd);
  }

  void display() {
    // We need the Body’s location and angle
    Vec2 pos = box2d.getBodyPixelCoord(body);		
    float a = body.getAngle();

    pushMatrix();
    translate(pos.x,pos.y);		// Using the Vec2 position and float angle to
    rotate(-a);			        // translate and rotate the rectangle
    fill(127);
    stroke(0);
    strokeWeight(2);
    rectMode(CENTER);
    rect(0,0,w,h);
    popMatrix();
  }

}

the example could’t be run successfully.

when i copy the url which the The Nature of Code provides to download PBox2D:


it looks this:

This is the python version of the code I tried to rewrite, it could be run successfully, but I don’t know if it is the same as the sample run result provided.

add_library('box2d_processing')


from org.jbox2d.collision.shapes import *
from org.jbox2d.common import *
from org.jbox2d.dynamics import *



class Box(object):
    def __init__(self,x_, y_):
        self.x = x_
        self.y = y_
        self.w =16
        self.h =16
        
        #Step 1: 定义Body
        self.bd = BodyDef()
        self.bd.type = BodyType.DYNAMIC
        self.bd.position.set(box2d.coordPixelsToWorld(self.x,self.y))
        
        #Step 2:创建Body
        self.body = box2d.createBody(self.bd)
        
        #Step 3:创建Shape
        ps = PolygonShape()
        box2DW =  box2d.scalarPixelsToWorld(self.w/2)
        box2DH =  box2d.scalarPixelsToWorld(self.h/2)
        ps.setAsBox(box2DW,box2DH)
        
        #Step 4:创建Fixture
        fd = FixtureDef()
        fd.shape = ps
        #影响physics的参数
        fd.density = 1
        fd.friction = 0.3
        fd.restitution = 0.5
        
        #Step 5:Attach Shape to Body with Fixture
        self.body.createFixture(fd)
        
        
    def display(self):
        pos = box2d.getBodyPixelCoord(self.body)
        a = self.body.getAngle()    
        pushMatrix()
        translate(pos.x,pos.y)
        rotate(-a)
        fill(127)
        stroke(0)
        strokeWeight(2)
        rectMode(CENTER)
        rect(0,0,self.w,self.h)
        popMatrix()

 
def setup():
    global boxes,box2d
    size(640,360)
    
    box2d = Box2DProcessing(this)
    box2d.createWorld()
    boxes = []   

    
def draw():
    global boxes,box2d
    
    box2d.step()
    background(255)
    
    if mousePressed:
        p = Box(mouseX,mouseY)
        boxes.append(p)
        
    for b in boxes:
        b.display()

i need help:
1.Is PBox2D library the same as box2d_processing library?(name changes?)
2.What should I do to run the code which use pbox2d in java mode successfully?(I want to know the results of the run of the java mode code, in order to detect whether the python code I rewritten is correct.)

1 Like

It is as you suspected a renaming of library see https://github.com/shiffman/Box2D-for-Processing/commit/d3fead5bfd42ea57af213e1762bd93d4c69ab977

2 Likes

thank you very much :heartpulse:

1 Like