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?
// 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();
// 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);
Hi, i was wondering if there is a way of restarting an app from an object?
I would like to define a method in my class that allows my objects to restart the app or reset all the software parameters and start as if i would start the program again.
Hi , i have a .txt file storing point(x, y,z ) cordinates, i would like to create a voronoi mesh from that set of points, i would like to know if there is a library of an easy way of doing this? or should i implement my own voronoi algorithm by my own?
Hello folks, im new in this forum.
I have some agents that move randomly over a plane (x, y)
.
I have a terrain made with perlin noise.
How can i make if i want that my agents can move over the surface?