How to add Image Texture to the PBox2d Particle System(circle)
in
Contributed Library Questions
•
1 year ago
Hello All, I am using PBox2d library by daniel shiffman. I want add the image texture to the falling particle(circles in the example). I am stuck at how to add the texture to the particles. Can somebody please help me to solve this. Below is the code i am using
- 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;
- PImage ball;
- // An ArrayList of particles that will fall on the surface
- ArrayList<Particle> particles;
- // The Spring that will attach to the box from the mouse
- //Spring spring;
- // Perlin noise values
- float xoff = 0;
- float yoff = 1000;
- void setup() {
- size(1024, 768);
- smooth();
-
- ball = loadImage("Cricket-Ball.png");
- // Initialize box2d physics and create the world
- box2d = new PBox2D(this);
- box2d.createWorld();
- // Create the empty list
- particles = new ArrayList<Particle>();
- }
- void draw() {
- background(255);
- if (random(1) < 0.2) {
- float sz = random(10, 25);
- particles.add(new Particle(random(0,1024), -50, sz));
- }
- // We must always step through time!
- box2d.step();
-
- // Look at all particles
- for (int i = particles.size()-1; i >= 0; i--) {
- Particle p = particles.get(i);
- p.display();
- // Particles that leave the screen, we delete them
- // (note they have to be deleted from both the box2d world and our list
- if (p.done()) {
- particles.remove(i);
- }
- }
- }
- // A circular particle
- class Particle {
- // We need to keep track of a Body and a radius
- Body body;
- float r;
- color col;
- Particle(float x, float y, float r_) {
- r = r_;
- // This function puts the particle in the Box2d world
- makeBody(x, y, r);
- body.setUserData(this);
- col = color(175);
- }
- // 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+r*2) {
- killBody();
- return true;
- }
- return false;
- }
- //
- 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();
- pushMatrix();
- translate(pos.x, pos.y);
- // rotate(random(0,360));
- fill(col);
- stroke(0);
- strokeWeight(1);
- //ellipse(0,0,r*2,r*2);
- //float w = random(20, 50);
- image(ball, pos.x, pos.y, 50, 50);
- // Let's add a line so we can see the rotation
- //line(0,0,r,0);
- popMatrix();
- }
- // Here's our function that adds the particle to the Box2D world
- void makeBody(float x, float y, float r) {
- // Define a body
- BodyDef bd = new BodyDef();
- // Set its position
- bd.position = box2d.coordPixelsToWorld(x, y);
- body = box2d.world.createBody(bd);
- // Make the body's shape a circle
- CircleDef cd = new CircleDef();
- cd.radius = box2d.scalarPixelsToWorld(r);
- cd.density = 1.0f;
- cd.friction = 0.01f;
- cd.restitution = 0.3f; // Restitution is bounciness
- body.createShape(cd);
- // Always do this at the end
- body.setMassFromShapes();
- // Give it a random initial velocity (and angular velocity)
- body.setLinearVelocity(new Vec2(random(-10f, 10f), random(-10f, 10f)));
- // body.setAngularVelocity(random(-10,10));
- }
- }
this is something i want to archieve(see below image)
2