Displaying world/accessing loaded from b2djson in Processing

Discuss issues specific the Java port of Box2D
Serendiptasaurus
Posts: 1
Joined: Thu Jan 22, 2015 3:55 am

Displaying world/accessing loaded from b2djson in Processing

Postby Serendiptasaurus » Thu Jan 22, 2015 4:29 am

Hi, I've just started using b2djson to load a world designed in RUBE and saved to json format.

Newbie question, how to display the world object?

Also, how to access objects in the world?

Thanks in advance!

Code: Select all

[code][/code]


Code: Select all


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

// import the iforce2d libraries
import org.iforce2d.*;

Box2DProcessing box2d;

void setup() {
  size(640, 360);
  smooth();

  box2d = new Box2DProcessing(this);
  box2d.createWorld();
  org.iforce2d.Jb2dJson json = new org.iforce2d.Jb2dJson() ;

  StringBuilder errorMsg = new StringBuilder();
  World world = json.readFromFile("bike.json  ", errorMsg);

  println(world);
  println(errorMsg);
}

void draw() {
  background(255);

  box2d.step();
}

irresistible force
Posts: 1991
Joined: Tue Jun 24, 2008 8:25 pm
Location: Tokyo
Contact:

Re: Displaying world/accessing loaded from b2djson in Proces

Postby irresistible force » Mon Feb 02, 2015 4:36 am

This seems more like a processing question than a Box2D question. Do you know how to draw a box, or some lines etc, in processing?
The easiest way to get started would probably be to make a 'debug draw' implementation for processing. You can read about how debug draw works in the Box2D manual http://www.box2d.org/manual.html and also here: http://www.iforce2d.net/b2dtut/debug-draw

To access objects in the world you could loop over all bodies as shown in the Box2D manual (search for "Exploring the World")

To get specific bodies that you named in your RUBE scene, you can use the getBodyByName() or getBodiesByName() functions as explained on the main b2dJson page: http://www.iforce2d.net/b2djson/
Same goes for fixtures, joints, and images.
It might help to have a look at the source for b2dJson as well as there are some functions that were added after writing the main page that are not mentioned on it. https://github.com/iforce2d/b2dJson/blo ... dJson.java


Return to “Java”



Who is online

Users browsing this forum: Google [Bot] and 1 guest