JBox2D a minimal example

Hi,

I'm trying to implement JBox2D with Processing. I know there is a wrapper for processing<->box2d, but
I want to know why my code, which is very minimal isn't working as I expect it should work. I create these
bodies:

1) big plain, like a ground floor
2) a small box (I use setMassFromShapes)
3) a big vertical box. (I use setMassFromShapes)

Body 2 and 3 fall down onto the plain. Though the boxes "overlap" they do not collide with eachother. Like
in this image:
Image

I've been trying to change the x/y position when rendering the body to a rectangle, though no
possible solution works. Does someone has an example w/o the debugdraw methods that are used
in the examples?

Code:
----------------------------------------------
package jboxtest;
import processing.core.PApplet;
import processing.core.PFont;

public class Main extends PApplet {
   private Physics m_physics;
   private Sprite m_box;
   private Sprite m_boxb;
   private Sprite m_plain;
   private PFont m_font;

    public static void main(String[] args) {
       PApplet.main( new String[]{"jboxtest.Main"} );
    }

   @Override
   public void setup() {
      m_physics = new Physics(600,600);
      m_plain = new Sprite(this, m_physics, -1,30,30.0f,0.05f);
      m_box = new Sprite(this, m_physics, 0,-10,0.5f,0.3f);
      m_box.setMass();
      m_boxb = new Sprite(this, m_physics, 0,-141,0.3f,1.53f);
      m_boxb.setMass();
      frameRate(30);
      size(600,600);
      m_font = loadFont("Andalus-48.vlw");
      textFont(m_font, 48);
   }

   @Override
   public void draw() {
      background(155);
      m_physics.update();
      m_plain.draw();
      m_box.draw();
      m_boxb.draw();
   }
}

----------------------------------------------
package jboxtest;

import org.jbox2d.collision.AABB;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.World;

public class Physics {
   private AABB m_aabb;
   private Vec2 m_gravity;
   private Boolean m_do_sleep;
   private World m_world;
   private Camera m_camera;

   public Physics(float fScreenW, float fScreenH) {
      m_aabb = new AABB();
      m_aabb.lowerBound = new Vec2(-(fScreenW/2), -(fScreenH/2));
      m_aabb.upperBound = new Vec2((fScreenW/2), (fScreenH/2));

      m_do_sleep = true;
      m_gravity = new Vec2(0.0f, 10.0f);
      m_world = new World(m_aabb, m_gravity, m_do_sleep);
      m_world.setContinuousPhysics(true);
      m_camera = new Camera(this, fScreenW, fScreenH);
   }

   public World getWorld() {
      return m_world;
   }

   public void update() {
      m_world.step(1.0f/20, 10);
   }
}

----------------------------------------------
package jboxtest;

import org.jbox2d.collision.PolygonDef;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.World;
import processing.core.PApplet;

public class Sprite {
   private float m_width = 0;
   private float m_height = 0;
   private Physics m_physics;
   private World m_world;
   private PApplet m_app;
   private float m_scale = 100;
   private PolygonDef m_poly_def;
   private BodyDef m_body_def;
   private Body m_body;
   private Vec2 m_position; // screen position
   private float m_x; // screen x
   private float m_y; // screen y
   private Vec2 m_world_size;
   private Vec2 m_screen_size;

   private static int m_num_objects = 0;
   private int m_id = 0;

   public Sprite(PApplet oApp, Physics oPhys, float fX, float fY, float fWidth, float fHeight) {
      m_num_objects++;
      m_app = oApp;
      m_physics = oPhys;
      m_world = oPhys.getWorld();
      m_width = fWidth;
      m_height = fHeight;
      m_world_size = new Vec2(m_width/2, m_height/2); // size is half width.
      m_screen_size = new Vec2(m_world_size.x * m_scale, m_world_size.y * m_scale);
      m_y = fY;
      m_x = fX;


      m_poly_def = new PolygonDef();
      m_poly_def.density = 5.0f;
      m_poly_def.friction = 6.0f;
      //m_poly_def.restitution = 1.3f;
      m_poly_def.setAsBox(m_world_size.x, m_world_size.y);
      System.out.println("World size: " +m_world_size.x +", " +m_world_size.y +", members w/h: " +m_width +", " +m_height);
      m_body_def  = new BodyDef();
      m_body_def.position = new Vec2(fX, fY);

      m_id = m_num_objects-1;
      createBody();
   }

   public Body createBody() {
      m_body = m_world.createBody(m_body_def);
      m_body.createShape(m_poly_def);
      return m_body;
   }

   public void draw() {
      Vec2 pos = getScreenPosition();
      m_app.rect(pos.x, pos.y, m_screen_size.x,m_screen_size.y);
   }

   public Vec2 getScreenPosition() {
      Vec2 pos = getWorldPosition();
      pos.x += 300 - m_screen_size.x ;
      pos.y += 300 - m_screen_size.y ;
      return pos;
   }

   public Vec2 getScreenSize() {
      return m_screen_size;
   }

   public Vec2 getWorldSize() {
      return m_world_size;
   }

   public void setMass() {
      m_body.setMassFromShapes();
   }

   public Vec2 getWorldPosition() {
      return m_body.getPosition();
   }
}
I noticed one more thing about this - it doesn't appear you're factoring the screen size and world size scale difference into your drawing. You account for an offset in getScreenPosition, but not the scaling, so the positions are probably incorrect. Try using PApplet's map command, I find it's pretty useful for transformations of that sort (i.e. map(inputValue, inputMin, inputMax, outputMin, outputMax) will transform the input value from the given input range to the given output range).
Hi,

You mean the world AABB lower and upperbound? I've set them to screen-width/2 and screen-height/2 so I assume I do not have to take scaling from those coordinates, right? Or do you mean something else?

-- snip --
I just found out i'm not taking care of rotation, though thats still not giving me correct results.
-- snip --
Roxlu
Hello, did you find a solution to your problem? I have the same problem with bodies not colliding:
Image
The cube started falling from the upper part of the screen and stopped down as you can see on the screenshot. It didn't collide with the 2 static boxes I defined.

Here are the declarations of the World and of 2 bodies:

World:
Code:
      m_aabb = new AABB();
      m_aabb.lowerBound.set(-500.0f,-500.0f);
      m_aabb.upperBound.set(500.0f,500.0f);
      m_do_sleep = true;
      m_gravity = new Vec2(0.0f, 60.0f);
      m_world = new World(m_aabb, m_gravity, m_do_sleep);


Static lower ground:
Code:
          m_poly_def = new PolygonDef();
          m_poly_def.density = 0;
          m_poly_def.friction = 0.3f;
          //m_poly_def.friction = 0.3f;
          m_poly_def.setAsBox(10.0f, 1.0f);   
          m_body_def  = new BodyDef();
          m_body_def.position = new Vec2(710, 460);
          m_body = m_world.createBody(m_body_def);
          m_body.createShape(m_poly_def);


Dynamic cube:
Code:
          m_poly_def = new PolygonDef();
          m_poly_def.density = 1.0;
          m_poly_def.friction = 0.3f;
          //m_poly_def.friction = 0.3f;
          m_poly_def.setAsBox(1.0f, 1.0f);   
          m_body_def  = new BodyDef();
          m_body_def.position = new Vec2(50, 0);
          m_body = m_world.createBody(m_body_def);
          m_body.createShape(m_poly_def);


I've tried different lower and upper bounds but it didn't fix the colliding problem.

I'm starting with jbox2d, I might have forgot something important....
roxlu wrote:
You mean the world AABB lower and upperbound? I've set them to screen-width/2 and screen-height/2 so I assume I do not have to take scaling from those coordinates, right? Or do you mean something else?

Whoops, sorry, I missed that post when it came up, I didn't mean to not respond.

I think I was talking about the following:

Code:
   public Vec2 getScreenPosition() {
      Vec2 pos = getWorldPosition();
      pos.x += 300 - m_screen_size.x ;
      pos.y += 300 - m_screen_size.y ;
      return pos;
   }

Suppose the position is (0,0). After transformation, the x coordinate is then 300 - m_screen_size.x = 200. Now suppose we move it one unit to the right in physics coordinates - then its coordinate is 301 - m_screen_size.x = 201, so it's moved one pixel. But one meter in the world is supposed to be 100 pixels per meter (according to m_scale), so something's wrong here. I suspect it's that you need to multiply pos.x and pos.y by m_scale before adding these values to them.

Now, for Feijah - I think your problem is different. Try setting your world bounds a LOT bigger, like:
Code:
  m_aabb.lowerBound.set(-5000.0f,-5000.0f);
  m_aabb.upperBound.set(5000.0f,5000.0f);

I think your bodies are getting frozen because they're traveling out of the world bounds. The reason for that is that you are using pixel units, which you really shouldn't do - for best results you should add a scaling factor so that you draw things the right size on the screen but the physics engine uses reasonable sizes in meters (between .1m and 10m is best). You may not need to do this, but things like stacking and collisions work much better at those sizes.

Original thread: https://box2d.org/forum/viewtopic.php?t=1960