Implementing PBox2D in a Processing sketch

I am following tutorials to implement box2d in my sketch but having problems in implementing the box2d in processing sketch.
Am I on the rt track?
please suggest whr I am going horribly wrong?

import shiffman.box2d.*;

ArrayList<box> b;
PBox2D box2d;
void setup(){
  box2d=new PBox2D(this);
  box2d.createWorld();
  
  size(500,300);
  b=new ArrayList<box>();  
}
void draw(){
  box2d.step();
 if(mousePressed){
 
  box p=new box(mouseX,mouseY); 
  b.add(p);
  p.display(); 
 }

 }
class box{
  box b;
  Body bod;
float x,y;
  float h;
  float w;
  
  box(float px,float py ){
  x=px;
  y=px;
  h=10;
  w=15;
  BodyDef bd=new BodyDef();
  bd.type= BoyType.DYNAMIC;
  bd.position.set(box2d.coordPixelToWorld(x,y));
  bod=box2d.createBody(bd);
  PolygonShape ps=new PolygonShape();
float box2Dw =box2d.scalarPixelsToWorld(w/2);
float box2Dh =box2d.scalarPixelsToWorld(h/2);
  ps.setAsBox(box2Dw,box2Dh);
  
  FixtureDef fd=new FixtureDef();
  fd.shape(ps);
  fd.density=1;
  fd.friction=0.3;
  fd.restitution=0.5;
  bod.createFixture(fd);
  
  }
  
 void display(){
   Vec2 pos=box2d.getBodyPixelCoord(bod);
   float a=body.getAngle();
   translate(pos.x,pos.y);
   rotate(a);
   rect(x,y,h,w);
   
   
 }
  
}
1 Like

When you say “having problems” what do you mean? Are you getting error messages that you don’t understand? Are you expecting one thing to appear on the screen, but another appears.

Please be specific so that you can receive better help.

Srry I shld have been more forthcoming.
The problem is that i am gettin errors stating the classPBox2d dosen’t exist

You didn’t give us the exact error, but I’m guessing it was “class PBox2D does not exist”. When I search for that phrase, the top hit I get is on the old forum:

Box2DProcessing instead of PBox2D works for me in Processing’s current version of Box2D

it’s been Box2DProcessing for about 4 years now according to github

Seems like perhaps you were using a very old tutorial / documentation – changing to the new class name should fix your issue.

Thanks a lot i will try and post if things are running rt

Great. Where did you find the PBox2D box2d tutorial / example code? Perhaps we can ask that it be updated.

I found it in Daniel Schiffman’s tutorial Nature of Code and also the youtube tutorials by Schiffman in it
But the sad news is still after replacing PBox2d with Box2d the error persists.Now it says Body doesn’t exist
Is thr any tutorial where the updated form of box 2d is properly worked out.

import shiffman.box2d.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;

You didn’t import the bottom three in your sketch. Import them and your sketch should run fine.

1 Like

I am not very old into processing and in the bargain must be asking some questions that has already been answered.Please bear with me
now that i have extracted the JBox2d from github how to i install it in processing mannualy
should i copy the extracted folder of JBox2D into the library folder that we find inside the core folder of processing .
or what is the procedure plz xplain cause I am still getting the error the class Box2D does not exist
and also PBox2D doesn; exist

still getting error with PBox2D or Box2d that -this classPBox2D/Box2D does not exist
so wht to

import shiffman.box2d.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;

ArrayList<box> b;

Box2D box2d;
void setup(){
  box2d=new Box2D(this);
  box2d.createWorld();
  
  size(500,300);
  b=new ArrayList<box>();  
}
void draw(){
  box2d.step();
 if(mousePressed){
 
  box p=new box(mouseX,mouseY); 
  b.add(p);
  p.display(); 
 }

 }

class box{
  box b;
  Body bod;
float x,y;
  float h;
  float w;
  
  box(float px,float py ){
  x=px;
  y=px;
  h=10;
  w=15;
  BodyDef bd=new BodyDef();
  bd.type= BoyType.DYNAMIC;
  bd.position.set(box2d.coordPixelToWorld(x,y));
  bod=box2d.createBody(bd);
  PolygonShape ps=new PolygonShape();
float box2Dw =box2d.scalarPixelsToWorld(w/2);
float box2Dh =box2d.scalarPixelsToWorld(h/2);
  ps.setAsBox(box2Dw,box2Dh);
  
  FixtureDef fd=new FixtureDef();
  fd.shape(ps);
  fd.density=1;
  fd.friction=0.3;
  fd.restitution=0.5;
  bod.createFixture(fd);
  
  }
  
 void display(){
   Vec2 pos=box2d.getBodyPixelCoord(bod);
   float a=body.getAngle();
   translate(pos.x,pos.y);
   rotate(a);
   rect(x,y,h,w);
   
   
 }
  
}

Hey @maddyna!

I am actually also working through Dan’s tutorial on this as well. Since some of the class names/functions/variables are depricated, I took a look at the examples he included in his library. These examples have the updated information! I took a look at your code and was able to fix most of it (at least until it stopped giving me red marks). Here is what I did:

Added stars at the end of the import statements (makes it so it imports all in that part of the library):
import shiffman.box2d.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;

Box2D class is now called Box2DProcessing:
Box2DProcessing box2d;

Setting a shape to a fixture is slightly different:
fd.shape = ps;

The other errors were spelling mostly:
Body body;
BodyType.DYNAMIC;

After fixing these issues, your code runs, but it is still incorrect. If you have Dan’s library downloaded with Processing, you can go to File > Examples > Contributed Libraries > Box2D for Processing > box2d_exercise_solved. You should find the working code for what you want to do there, and you can compare and fix what you need to.

Hope this helps!

2 Likes