java - How to control indivdual particles in a chain in JBox2D -
i have series of particles chained vertically using processing , box2d. want move particles in controlled way follows:
rather continuous sinusoidal wave along particles, able simple move top 5 particles in , out along x-axis , maybe 5 particles somewhere else (as shown in diagram). dont want move whole chain when this:
i don't seem able control of particles in order this. have far:
import shiffman.box2d.*; 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.*; // reference our box2d world box2dprocessing box2d; // object describe bridget (a list of particles joint connections) bridge bridge; void setup() { size(1040,1060); smooth(); // initialize box2d physics , create world box2d = new box2dprocessing(this); box2d.createworld(); // make bridge //total length,number of points,x start position bridge = new bridge(width/2,width/10,20); } void draw() { background(255); box2d.step(); bridge.display(); (int = 0; < 50; i++) { (float x = 0; x < 500; x++){ vec2 pos = box2d.getbodypixelcoord(bridge.particles.get(i).body); bridge.particles.get(i).body.getposition().set(pos.mul(sin(x) * 10)); } } fill(0); } class bridge { // bridge properties float totallength; // how long int numpoints; // how many points // our chain list of particles arraylist<particle> particles; // chain constructor bridge(float l, int n,int start) { totallength = l; numpoints = n; particles = new arraylist(); float len = totallength / numpoints; // go through , add particles chain for(int i=0; < numpoints+1; i++) { // make new particle particle p = null; if (i == 0) p = new particle(start,i*len,4,true); else if (i == numpoints) p = new particle(start,i*len,4,true); else p = new particle(start,i*len,4,false); particles.add(p); // connect particles distance joint if (i > 0) { distancejointdef djd = new distancejointdef(); particle previous = particles.get(i-1); // connection between previous particle , 1 djd.bodya = previous.body; djd.bodyb = p.body; // equilibrium length djd.length = box2d.scalarpixelstoworld(len); // these properties affect how springy joint djd.frequencyhz = 0; djd.dampingratio = 0; // make joint. distancejoint dj = (distancejoint) box2d.world.createjoint(djd); } } } class particle { // need keep track of body , radius body body; float r; color col; particle(float x, float y, float r_, boolean fixed) { r = r_; // define body bodydef bd = new bodydef(); if (fixed) bd.type = bodytype.static; else bd.type = bodytype.static; // set position bd.position = box2d.coordpixelstoworld(x,y); body = box2d.world.createbody(bd); // make body's shape circle // make body's shape circle circleshape cs = new circleshape(); cs.m_radius = box2d.scalarpixelstoworld(r); fixturedef fd = new fixturedef(); fd.shape = cs; // parameters affect physics fd.density = 1; fd.friction = 0.3; fd.restitution = 0.5; body.createfixture(fd); col = color(175); } void display() { // @ each body , screen position vec2 pos = box2d.getbodypixelcoord(body); // angle of rotation float = body.getangle(); pushmatrix(); translate(pos.x,pos.y); rotate(a); fill(col); stroke(0); strokeweight(1); ellipse(0,0,r*2,r*2); // let's add line can see rotation line(0,0,r,0); popmatrix(); } } // draw bridge void display() { (particle p: particles) { p.display(); } } }
Comments
Post a Comment