1
\$\begingroup\$

Using Eclipse, I'm trying to write a simple hello world program in processing that simply draws a rectangle on the screen then has gravity drop it as seen in this tutorial.

The problem is that when I try to import the p5 package, it's not resolving so I can't declare my physics object. I tried two things.

  1. Download the zip, unzip it, then import the 3 jars (library, serialization, & testbed)

    a. import org.jbox2d.p5.*; doesn't resolve but the others do

    b. Physics physics; doesn't resolve

  2. Download the older standalone testbed jar then import it

    a. Physics physics; doesn't resolve;

Here is basically where I'm starting

import org.jbox2d.util.nonconvex.*;
import org.jbox2d.dynamics.contacts.*;
import org.jbox2d.testbed.*;
import org.jbox2d.collision.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.joints.*;
import org.jbox2d.p5.*;
import org.jbox2d.dynamics.*;

import processing.core.PApplet;

public class MyFirstJBox2d extends PApplet  {

    Physics physics;

    public void setup() {
      size(640,480);
      frameRate(60);
      initScene();
    }

    public void draw() {
      background(0);

      if (keyPressed) {
        //Reset everything
        physics.destroy();
        initScene();
      }

    }

    public void initScene() {
      physics = new Physics(this, width, height);
      physics.setDensity(1.0f);
      physics.createRect(300,200,340,300);
    }
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

The "Physics" and p5 package/class was an extension to the jbox2d framework the guy wrote. It seemed pretty nice but not ideal for what I'm doing. I ended up doing this:

import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;
import org.jbox2d.collision.shapes.PolygonShape;
import processing.core.PApplet;

public class MyJBoxTest extends PApplet {

    private Vec2 gravity = new Vec2(0, -10f);;
    private World world;
    private Body floor;
    private Body player;
    private float fieldBottom = 300;
    private float timeStep = 1.0f / 60.f;
    private int velocityIterations = 6;
    private int positionIterations = 2;


    public void setup() {
        size(300, (int)fieldBottom);
        frameRate(60);
        background (0);
        world = new World(gravity);
        gameInit();
        //simulateWorld();
    }

    public void gameInit() {
        //Create Floor
        BodyDef aboutFloor = new BodyDef();
        aboutFloor.position.set(0.0f, -8.0f); //-8 instead of -10 so I can see it
        PolygonShape groundBox = new PolygonShape();
        groundBox.setAsBox(50.0f, 10.0f); // top lands at 0 aka floor

        floor = world.createBody(aboutFloor);
        floor.createFixture(groundBox, 0.0f);

        //Create Player
        BodyDef aboutPlayer = new BodyDef();
        aboutPlayer.type = BodyType.DYNAMIC; //movable
        aboutPlayer.position.set(0.0f, 200.0f);
        PolygonShape playerBox = new PolygonShape();
        playerBox.setAsBox(10.0f, 10.0f);
        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = playerBox; 
        fixtureDef.density = 1.0f;
        fixtureDef.friction = 0.3f;

        player = world.createBody(aboutPlayer);
        player.createFixture(fixtureDef);
    }

    public void draw() {
        Vec2 floorLoc = floor.getPosition();
        Vec2 playerLoc = player.getPosition();

        background(0);

        rectMode(RADIUS);
        fill(255,0,0);
        rect(floorLoc.x, fieldBottom - floorLoc.y, 50, 10);

        rectMode(RADIUS);
        fill(255,100);
        rect (playerLoc.x, fieldBottom - playerLoc.y, 10, 10);

        world.step(timeStep, velocityIterations, positionIterations);
    }

}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .