boxes collision with Pbox2d library
in
Contributed Library Questions
•
1 year ago
Hi , in the examples folder of the library Pbox2d there is an example called Boxes that implement some falling boxes , i need that when the boxes collides with each other they change its color, i been trying to adapt the code using the example CollisionsAndControl as a base but i cannot make it work, any idea whats wrong in my code? why do they dont change the color?
thanks
Vortix
thanks
Vortix
- import pbox2d.*;
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.*;
// A reference to our box2d world
PBox2D box2d;
// A list we'll use to track fixed objects
ArrayList<Boundary> boundaries;
// A list for all of our rectangles
ArrayList<Box> boxes;
void setup() {
size(400,300);
smooth();
// Initialize box2d physics and create the world
box2d = new PBox2D(this);
box2d.createWorld();
// We are setting a custom gravity
box2d.setGravity(0, -20);
// Create ArrayLists
boxes = new ArrayList<Box>();
boundaries = new ArrayList<Boundary>();
// Add a bunch of fixed boundaries
boundaries.add(new Boundary(width/4,height-5,width/2-50,10));
boundaries.add(new Boundary(3*width/4,height-5,width/2-50,10));
boundaries.add(new Boundary(width-5,height/2,10,height));
boundaries.add(new Boundary(5,height/2,10,height));
}
void draw() {
background(255);
// We must always step through time!
box2d.step();
// When the mouse is clicked, add a new Box object
if (mousePressed) {
Box p = new Box(mouseX,mouseY);
boxes.add(p);
}
// Display all the boundaries
for (Boundary wall: boundaries) {
wall.display();
}
// Display all the boxes
for (Box b: boxes) {
b.display();
}
// Boxes that leave the screen, we delete them
// (note they have to be deleted from both the box2d world and our list
for (int i = boxes.size()-1; i >= 0; i--) {
Box b = boxes.get(i);
if (b.done()) {
boxes.remove(i);
}
}
}
// Collision event functions!
void addContact(ContactPoint cp) {
// Get both shapes
Shape s1 = cp.shape1;
Shape s2 = cp.shape2;
// Get both bodies
Body b1 = s1.getBody();
Body b2 = s2.getBody();
// Get our objects that reference these bodies
Object o1 = b1.getUserData();
Object o2 = b2.getUserData();
// What class are they? Box or Particle?
String c1 = o1.getClass().getName();
String c2 = o2.getClass().getName();
// If object 1 is a Box, then object 2 must be a particle
// Note we are ignoring particle on particle collisions
if (c1.contains("Box")) {
//ArrayList<Box> boxes;
Box p = (Box) o2;
p.change();
print("moco " );
}
// If object 2 is a Box, then object 1 must be a particle
else if (c2.contains("Box")) {
Box p = (Box) o1;
p.change();
print("moco " );
}
}
// Contacts continue to collide - i.e. resting on each other
void persistContact(ContactPoint cp) {
}
// Objects stop touching each other
void removeContact(ContactPoint cp) {
}
// Contact point is resolved into an add, persist etc
void resultContact(ContactResult cr) {
} - class Boundary {
// A boundary is a simple rectangle with x,y,width,and height
float x;
float y;
float w;
float h;
// But we also have to make a body for box2d to know about it
Body b;
Boundary(float x_,float y_, float w_, float h_) {
x = x_;
y = y_;
w = w_;
h = h_;
// Figure out the box2d coordinates
float box2dW = box2d.scalarPixelsToWorld(w/2);
float box2dH = box2d.scalarPixelsToWorld(h/2);
Vec2 center = new Vec2(x,y);
// Define the polygon
PolygonDef sd = new PolygonDef();
sd.setAsBox(box2dW, box2dH);
sd.density = 0; // No density means it won't move!
sd.friction = 0.3f;
// Create the body
BodyDef bd = new BodyDef();
bd.position.set(box2d.coordPixelsToWorld(center));
b = box2d.createBody(bd);
b.createShape(sd);
}
// Draw the boundary, if it were at an angle we'd have to do something fancier
void display() {
fill(0);
stroke(0);
rectMode(CENTER);
rect(x,y,w,h);
}
} - class Box {
// We need to keep track of a Body and a width and height
Body body;
float w;
float h;
color col = 0;
// Constructor
Box(float x, float y) {
w = random(4,16);
h = random(4,16);
// 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);
}
// Is the particle ready for deletion?
boolean done() {
// Let's find the screen position of the particle
Vec2 pos = box2d.getBodyPixelCoord(body);
// Is it off the bottom of the screen?
if (pos.y > height+w*h) {
killBody();
return true;
}
return false;
}
void change() {
col = color(255,0,0);
}
// 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(CENTER);
pushMatrix();
translate(pos.x,pos.y);
rotate(-a);
fill(col);
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 a polygon (this is what we use for a rectangle)
PolygonDef sd = new PolygonDef();
float box2dW = box2d.scalarPixelsToWorld(w_/2);
float box2dH = box2d.scalarPixelsToWorld(h_/2);
sd.setAsBox(box2dW, box2dH);
// Parameters that affect physics
sd.density = 1.0f;
sd.friction = 0.3f;
sd.restitution = 0.5f;
// Define the body and make it from the shape
BodyDef bd = new BodyDef();
bd.position.set(box2d.coordPixelsToWorld(center));
body = box2d.createBody(bd);
body.createShape(sd);
body.setMassFromShapes();
// Give it some initial random velocity
body.setLinearVelocity(new Vec2(random(-5,5),random(2,5)));
body.setAngularVelocity(random(-5,5));
}
}
1