2

enter image description here

That's what my world looks like. there are two classes : box and boundary. I want some of the box objects to pass through the green boundary, remaining to bounce off.

You can think of the green boundary as a filter of some sort.

 class Boundary {

 // A boundary is a simple rectangle with x,y,width,and height
 float x;
 float y;
 float w;
 float h;

Body b;

Boundary(float x_,float y_, float w_, float h_, float a) {
x = x_;
y = y_;
w = w_;
h = h_;

// Define the polygon
PolygonShape sd = new PolygonShape();
// Figure out the box2d coordinates
float box2dW = box2d.scalarPixelsToWorld(w/2);
float box2dH = box2d.scalarPixelsToWorld(h/2);
// We're just a box
sd.setAsBox(box2dW, box2dH);


// Create the body
BodyDef bd = new BodyDef();
bd.type = BodyType.STATIC;
bd.angle = a;
bd.position.set(box2d.coordPixelsToWorld(x,y));
b = box2d.createBody(bd);

// Attached the shape to the body using a Fixture
b.createFixture(sd,1);
}

// Draw the boundary, if it were at an angle we'd have to do something fancier
void display() {
fill(color(18,220,0));
stroke(0);
strokeWeight(1);
rectMode(CENTER);

float a = b.getAngle();

pushMatrix();
translate(x,y);
rotate(-a);
rect(0,0,w,h);
popMatrix();
}

} 




class Box {

// We need to keep track of a Body and a width and height
Body body;
float w;
float h;

// Constructor
Box(float x_, float y_) {
float x = x_;
float y = y_;
w = 24;
h = 24;
// Add the box to the box2d world
makeBody(new Vec2(x,y),w,h);
}

// This function removes the particle from the box2d world
void killBody() {
box2d.destroyBody(body);
}

boolean contains(float x, float y) {
Vec2 worldPoint = box2d.coordPixelsToWorld(x, y);
Fixture f = body.getFixtureList();
boolean inside = f.testPoint(worldPoint);
return inside;
}

// Drawing the box
void display() {
// We look at each body and get its screen position
Vec2 pos = box2d.getBodyPixelCoord(body);
// Get its angle of rotation
float a = body.getAngle();

rectMode(PConstants.CENTER);
pushMatrix();
translate(pos.x,pos.y);
rotate(a);
fill(175);
stroke(0);
rect(0,0,w,h);
popMatrix();
}


// This function adds the rectangle to the box2d world
 void makeBody(Vec2 center, float w_, float h_) {
// Define and create the body
BodyDef bd = new BodyDef();
bd.type = BodyType.DYNAMIC;
bd.position.set(box2d.coordPixelsToWorld(center));
body = box2d.createBody(bd);

// Define a polygon (this is what we use for a rectangle)
PolygonShape sd = new PolygonShape();
float box2dW = box2d.scalarPixelsToWorld(w_/2);
float box2dH = box2d.scalarPixelsToWorld(h_/2);
sd.setAsBox(box2dW, box2dH);

// Define a fixture
FixtureDef fd = new FixtureDef();
fd.shape = sd;
// Parameters that affect physics
fd.density = 1;
fd.friction = 0.3;
fd.restitution = 0.5;

body.createFixture(fd);
//body.setMassFromShapes();

// Give it some initial random velocity
body.setLinearVelocity(new Vec2(random(-5, 5), random(2, 5)));
body.setAngularVelocity(random(-5, 5));
}

}
Rakesh Adhikesavan
  • 11,966
  • 18
  • 51
  • 76

1 Answers1

3

Disclaimer: I don't know box2d or jbox2d

You need to set a filtering category on the boundary object and on the bodies. So for instance, you could set the boundary object to have a Filter with categoryBits equal to 0x0002, then set the bodies which shouldn't collide with the Boundary to have a Filter with maskBits with all bits set except 0x0002.

Since I don't know how your code is organized, here's some code which might push you in the right direction; it will not be able to stand alone:


// Make sure you use a different mask for each boundary
// if you need to filter separately with different boundaries.
public static final int BOUNDARY_CATEGORY = 0x0002

boundaryFilter = new Filter()
boundaryFilter.categoryBits = BOUNDARY_CATEGORY
boundaryFixture.setFilterData(boundaryFilter)

bodyGoingThroughFilter = new Filter()
bodyGoingThroughFilter.maskBits = (0xFFFF & (~BOUNDARY_CATEGORY))
bodyGoingThroughFixture.setFilterData(bodyGoingThroughFixture)

(The bodyGoingThrough stuff should only be run where it pertains to the bodies which are to pass through the boundary.)

Lily Chung
  • 2,919
  • 1
  • 25
  • 42