JBox2D wierdness (?!)

Hi,

I'm trying to write a simple wrapper to create Bodies to be used with Processing. (I'm aware of boxwrap 2d, though I want to know what I'm doing wrong here). I started
with a simple Sprite class that I will use to create 'box' like objects. The Sprite class contains the world-unit to screen-unit conversion methods. Though something goes wrong as you can see in this image:

Image

Here is my code. Does someone knows what I'm doing wrong?
Code:
package jboxtest;
import processing.core.PApplet;

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

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

   @Override
   public void setup() {
      m_physics = new Physics(600,600);

      m_box = new Sprite(this, m_physics, 0,0,10,0.03f);
      m_plain = new Sprite(this, m_physics, 0,30,10,0.5f);
      m_box.setMass();

      m_boxb = new Sprite(this, m_physics, 0,-40,1.5f,2.0f);
      m_boxb.setMass();
      frameRate(30);
      size(600,600);
   }

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

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 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, m_height);
      m_screen_size = new Vec2(m_width * 20, m_height * 20);
      m_y = fY;
      m_x = fX;


      m_poly_def = new PolygonDef();
      m_poly_def.density = 5.0f;
      m_poly_def.friction = 4.0f;
      m_poly_def.setAsBox(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/2;
      pos.y += 300 - m_screen_size.y/2;
      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();
   }
}





roxlu wrote:
Hi,

I'm trying to write a simple wrapper to create Bodies to be used with Processing. (I'm aware of boxwrap 2d, though I want to know what I'm doing wrong here). I started
with a simple Sprite class that I will use to create 'box' like objects. The Sprite class contains the world-unit to screen-unit conversion methods. Though something goes wrong as you can see in this image:

Image

Here is my code. Does someone knows what I'm doing wrong?



You might try explaining what you think is wrong here. I can't tell what you're trying to do, and as far as I know I see a picture of 2 boxes. Which look fine...

Bill
One problem is here (in your Sprite::draw() function):
Code:
m_app.rect(pos.x, pos.y, m_screen_size.x,m_screen_size.y);

The problem is that Processing's rect function takes as its first two parameters the corner of the rectangle, whereas the position vector you get from JBox2d is (at least for a rectangle) the center of the rectangle. You'll need to adjust accordingly.
Hi,

Yeah I've been trying to use something like this: rect(pos.x + width/2, pos.y + height/2, ..,..);
though that didnt work either. I'm wondering if someone here maybe has an example where jbox2d
is used together with processing and NOT the debugdraw methods of the jbox library? As I haven't
seen an working example without it.

Roxlu

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