Pbox2d or Jbox2D problem. I cannot render/draw all the polygons that are attached to the body in my code
in
Contributed Library Questions
•
18 days ago
Hello,
I have created a simple alien character made of multiple shapes, and the physics for this character are behaving correctly, but of the 2 polygons that are attached to the body, only one or the other can be seen on the screen - one seems to be invisible (it cannot be seen but objects will collide with it nonetheless). I can manipulate the code to choose which polygon is drawn, just by changing the order that the fixtures are created in the code. Basically, the polygon that is drawn, is the shape that is attached to the fixture that was created last in my code. I have tried to find an answer in the box2d and jbox2d documentations, and on the box2d forum, but I have hit a brick wall. Any insight would be appreciated.
Here is an image that shows the problem. The program just creates new alien objects in the mouse location whilst mousePressed is true, then gravity is acted upon the objects
There is a right 'antenna' there, with collisions being detected correctly for it, but it is invisible
I can change the order of the fixtures being created to choose which antenna is drawn and which one is invisible in this part of the code (inside the Alien class)
- body.createFixture(cs, 1);
- body.createFixture(csl, 1);
- body.createFixture(csr, 1);
- // right antenna
- body.createFixture(ps, 1);
- // left antenna
- body.createFixture(ps2, 1);
Here is all the code (requires the PBox2D library)
- import pbox2d.*;
- import org.jbox2d.collision.shapes.*;
- import org.jbox2d.common.*;
- import org.jbox2d.dynamics.*;
- PBox2D box2d;
- ArrayList<Alien> aliens;
- Boundary bound;
- void setup() {
- size(640,360);
- box2d = new PBox2D(this);
- box2d.createWorld();
- // Create ArrayLists
- aliens = new ArrayList<Alien>();
- // X and y are at the centre of the object
- bound = new Boundary(width/2,300,200,5);
- }
- void draw() {
- box2d.step();
- background(255);
- if (mousePressed) {
- Alien a = new Alien();
- aliens.add(a);
- }
- for (Alien A: aliens) {
- A.display();
- }
- // Display the boundary / static object
- bound.display();
- }
- class Alien {
- Body body;
- int bodyr, footr;
- Alien() {
- bodyr = 30;
- footr = 10;
- BodyDef bd = new BodyDef();
- bd.type = BodyType.DYNAMIC;
- bd.position.set(box2d.coordPixelsToWorld(mouseX, mouseY));
- bd.bullet = true;
- body = box2d.createBody(bd);
- // Alien body
- CircleShape cs = new CircleShape();
- cs.m_radius = box2d.scalarPixelsToWorld(bodyr);
- // Left foot
- CircleShape csl = new CircleShape();
- // Converting the vector to Box2D world
- Vec2 offset = new Vec2(bodyr, bodyr-2);
- offset = box2d.vectorPixelsToWorld(offset);
- // Setting the local position of the circle
- csl.m_p.set(offset.x, offset.y);
- csl.m_radius = box2d.scalarPixelsToWorld(footr);
- // Right foot
- CircleShape csr = new CircleShape();
- // Converting the vector to Box2D world
- Vec2 offset2 = new Vec2(-bodyr, bodyr-2);
- offset2 = box2d.vectorPixelsToWorld(offset2);
- // Setting the local position of the circle
- csr.m_p.set(offset2.x, offset2.y);
- csr.m_radius = box2d.scalarPixelsToWorld(footr);
- // Right Antenna
- Vec2[] vertices = new Vec2[4]; // An array of 4 vectors
- vertices[0] = box2d.vectorPixelsToWorld(new Vec2(20, -10));
- vertices[1] = box2d.vectorPixelsToWorld(new Vec2(30, -10));
- vertices[2] = box2d.vectorPixelsToWorld(new Vec2(35, -55));
- vertices[3] = box2d.vectorPixelsToWorld(new Vec2(30, -55));
- //[full] Making a polygon from that array
- PolygonShape ps = new PolygonShape();
- ps.set(vertices, vertices.length);
- //[end]
- // Left Antenna
- Vec2[] vertices2 = new Vec2[4]; // An array of 4 vectors
- vertices2[0] = box2d.vectorPixelsToWorld(new Vec2(-20, -10));
- vertices2[1] = box2d.vectorPixelsToWorld(new Vec2(-30, -10));
- vertices2[2] = box2d.vectorPixelsToWorld(new Vec2(-35, -55));
- vertices2[3] = box2d.vectorPixelsToWorld(new Vec2(-30, -55));
- //[full] Making a polygon from that array
- PolygonShape ps2 = new PolygonShape();
- ps2.set(vertices2, vertices2.length);
- //[end]
- body.createFixture(cs, 1);
- body.createFixture(csl, 1);
- body.createFixture(csr, 1);
- // right antenna
- body.createFixture(ps, 1);
- // left antenna
- body.createFixture(ps2, 1);
- }
- void display() {
- Vec2 pos = box2d.getBodyPixelCoord(body);
- float a = body.getAngle();
- fill(175);
- stroke(0);
- // Draw the body
- pushMatrix();
- translate(pos.x, pos.y);
- rotate(-a);
- ellipse(0, 0, bodyr * 2, bodyr * 2);
- popMatrix();
- // Draw the left foot
- pushMatrix();
- translate(pos.x, pos.y);
- rotate(-a);
- ellipse(bodyr, bodyr-2, footr * 2, footr * 2);
- popMatrix();
- // Draw the right foot
- pushMatrix();
- translate(pos.x, pos.y);
- rotate(-a);
- ellipse(-bodyr, bodyr-2, footr * 2, footr * 2);
- popMatrix();
- // First we get the Fixture attached to the body...
- Fixture f = body.getFixtureList();
- PolygonShape ps = (PolygonShape) f.getShape();
- rectMode(CENTER);
- // Draw right antenna (ps)
- pushMatrix();
- translate(pos.x, pos.y);
- rotate(-a);
- beginShape();
- //[offset-up] We can loop through that array and convert each vertex from Box2D space to pixels.
- for (int i = 0; i < ps.getVertexCount(); i++) {
- Vec2 v = box2d.vectorWorldToPixels(ps.getVertex(i));
- vertex(v.x, v.y);
- }
- endShape(CLOSE);
- popMatrix();
- Fixture f2 = body.getFixtureList();
- PolygonShape ps2 = (PolygonShape) f2.getShape();
- // Draw Left antenna (ps2)
- pushMatrix();
- translate(pos.x, pos.y);
- rotate(-a);
- beginShape();
- //[offset-up] We can loop through that array and convert each vertex from Box2D space to pixels.
- for (int i = 0; i < ps2.getVertexCount(); i++) {
- Vec2 v2 = box2d.vectorWorldToPixels(ps2.getVertex(i));
- vertex(v2.x, v2.y);
- }
- endShape(CLOSE);
- popMatrix();
- }
- void killBody() {
- box2d.destroyBody(body);
- }
- }
- class Boundary {
- //[full] A boundary is a simple rectangle with x, y, width, and height.
- float x,y;
- float w,h;
- //[end]
- Body b;
- Boundary(float x_,float y_, float w_, float h_) {
- x = x_;
- y = y_;
- w = w_;
- h = h_;
- // Build the Box2D Body and Shape.
- BodyDef bd = new BodyDef();
- bd.position.set(box2d.coordPixelsToWorld(x,y));
- // Make it fixed by setting type to STATIC!
- bd.type = BodyType.STATIC;
- b = box2d.createBody(bd);
- float box2dW = box2d.scalarPixelsToWorld(w/2);
- float box2dH = box2d.scalarPixelsToWorld(h/2);
- PolygonShape ps = new PolygonShape();
- // The PolygonShape is just a box.
- ps.setAsBox(box2dW, box2dH);
- // Using the createFixture() shortcut
- b.createFixture(ps,1);
- }
- // Since we know it can never move, we can just draw it
- // the old-fashioned way, using our original
- // variables. No need to query Box2D.
- void display() {
- fill(0);
- stroke(0);
- rectMode(CENTER);
- rect(x,y,w,h);
- }
- }
1