Collision with boundaries

edited May 2016 in Library Questions

Hello, do you know how to do that when a ball collisions with a specific boundary, make a sound? This is the code

//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.*;

//PBox2D box2d; ArrayList balls; ArrayList boundaries;

boundaries.add(new Boundary(108.32, 402.37, 33.72, 5, 320)); boundaries.add(new Boundary(184, 461, 36.27, 5, 319));

void setup() { size(1280, 720); smooth(); //box2d = new PBox2D(this); //box2d.createWorld(); //box2d.setGravity(0, -20); balls = new ArrayList(); }

void keyPressed() { if (key=='z') { balls.add(new Ball(63, 0, 8)); } }

void draw() { background(0); //image (fondo, 0, 0); //box2d.step(); for (Ball b : balls) { b.draw(); } }

class Ball { float px, py, r, vx, vy, ax, ay; Ball(float ix, float iy, float ir) { px=ix; py=iy; r=ir; vx=0; vy=0; ax=0; ay=1; } void draw() { simulate(); render(); } void simulate() { vx+=ax; vy+=ay; px+=vx; py+=vy; } void render(){ fill(255,0,0); ellipse(px,py,r,r); } }

Tagged:

Answers

  • Wow, something has gone terribly wrong with your code when you posted it here. Edit your post, select your code, and try pressing ctrl + o to format it. 8-|

  • //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.*;

    //PImage fondo; //PBox2D box2d; ArrayList balls; //ArrayList boundaries;

    boolean drop_balls = false; int next_ball_time = -1;

    void setup() { size(794, 450); smooth(); //fondo = loadImage("Fondo.jpg"); //box2d = new PBox2D(this); //box2d.createWorld(); //box2d.setGravity(0, -20); balls = new ArrayList();

    boundaries.add(new Boundary(108.32, 402.37, 33.72, 5, 320)); boundaries.add(new Boundary(184, 461, 36.27, 5, 319)); }

    void keyPressed() { if (key=='z') { balls.add(new Ball(63, 0, 8)); drop_balls = !drop_balls; next_ball_time = -1; // NOW. } }

    void draw() { background(0); //image (fondo, 0, 0); //box2d.step(); if(drop_balls){ if( millis() > next_ball_time ){ balls.add(new Ball(63, 0, 8)); next_ball_time = millis() + 2000; } } for (Ball b : balls) { b.draw(); } }

    class Ball { float px, py, r, vx, vy, ax, ay; Ball(float ix, float iy, float ir) { px=ix; py=iy; r=ir; vx=0; vy=0; ax=0; ay=1; } void draw() { simulate(); render(); } void simulate() { vx+=ax; vy+=ay; px+=vx; py+=vy; } void render(){ fill(255,0,0); ellipse(px,py,r,r); } }

    Is the same code of the timer on/off, remember? :) But I don't know how to put the class boundary if it's not in another window, so I'll put it here:

    class Boundary {

    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_;

    PolygonShape ps = new PolygonShape();
    float box2dW = box2d.scalarPixelsToWorld(w/2);
    float box2dH = box2d.scalarPixelsToWorld(h/2);
    ps.setAsBox(box2dW, box2dH);
    
    BodyDef bd = new BodyDef();
    bd.type = BodyType.STATIC;
    bd.position.set(box2d.coordPixelsToWorld(x,y));
    bd.angle = a;
    b = box2d.createBody(bd);
    
    
    b.createFixture(ps,1);
    

    }

    void display() { fill(100); noStroke(); float a = b.getAngle(); pushMatrix(); translate(x,y); rectMode(CENTER); rotate(-a); rect(0,0,w,h); popMatrix(); } }

  • Go back,

    Edit your post,

    select your code,

    and try pressing ctrl + o to format it.

    Empty line before and after the code

  • //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.*;
    
    //PImage fondo; //PBox2D box2d; ArrayList balls; //ArrayList boundaries;
    
    boolean drop_balls = false; int next_ball_time = -1;
    
    void setup() { size(794, 450); smooth(); //fondo = loadImage("Fondo.jpg"); //box2d = new PBox2D(this); //box2d.createWorld(); //box2d.setGravity(0, -20); balls = new ArrayList();
    
    boundaries.add(new Boundary(108.32, 402.37, 33.72, 5, 320)); boundaries.add(new Boundary(184, 461, 36.27, 5, 319)); }
    
    void keyPressed() { if (key=='z') { balls.add(new Ball(63, 0, 8)); drop_balls = !drop_balls; next_ball_time = -1; // NOW. } }
    
    void draw() { background(0); //image (fondo, 0, 0); //box2d.step(); if(drop_balls){ if( millis() > next_ball_time ){ balls.add(new Ball(63, 0, 8)); next_ball_time = millis() + 2000; } } for (Ball b : balls) { b.draw(); } }
    
    class Ball { float px, py, r, vx, vy, ax, ay; Ball(float ix, float iy, float ir) { px=ix; py=iy; r=ir; vx=0; vy=0; ax=0; ay=1; } void draw() { simulate(); render(); } void simulate() { vx+=ax; vy+=ay; px+=vx; py+=vy; } void render(){ fill(255,0,0); ellipse(px,py,r,r); } }
    
  • No....

    hit ctrl-t in processing

    then copy paste

    Then select

    Then ctrl-o

Sign In or Register to comment.