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:

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?
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:

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();
}
}
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();
}
}
