gui
YaBB Newbies
Offline
Posts: 17
Re: Ball simulation with constrain
Reply #1 - Jul 21st , 2009, 12:44pm
// called when an object is added to the scene void addTuioObject(TuioObject tobj) { println("add object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle()); } // called when an object is removed from the scene void removeTuioObject(TuioObject tobj) { println("remove object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+")"); } // called when an object is moved void updateTuioObject (TuioObject tobj) { println("update object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle() +" "+tobj.getMotionSpeed()+" "+tobj.getRotationSpeed()+" "+tobj.getMotionAccel()+" "+tobj.getRotationAccel()); } // called when a cursor is added to the scene void addTuioCursor(TuioCursor tcur) { println("add cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+ ") " +tcur.getX()+" "+tcur.getY()); int id = tcur.getCursorID(); int idS = int(tcur.getSessionID()); float speed=tcur.getMotionSpeed(); float px = convertX(tcur.getY()); float py = convertY(tcur.getX()); finger[id]=new BallParticle(px,py,25); finger[id].col = finger[id].init_col = color(234,0,0); fill(finger[id].col); ellipse(finger[id].x, finger[id].y, finger[id].radius*2, finger[id].radius*2); } // called when a cursor is moved void updateTuioCursor (TuioCursor tcur) { println("update cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+ ") " +tcur.getX()+" "+tcur.getY() +" "+tcur.getMotionSpeed()+" "+tcur.getMotionAccel()); } // called when a cursor is removed from the scene void removeTuioCursor(TuioCursor tcur) { println("remove cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+")"); int id = tcur.getCursorID(); int idS = int(tcur.getSessionID()); } // called after each message bundle // representing the end of an image frame void refresh(TuioTime bundleTime) { redraw(); } void callFingers(){ Vector tuioCursorList = tuioClient.getTuioCursors(); for (int i=0;i<tuioCursorList.size();i++) { TuioCursor tcur = (TuioCursor)tuioCursorList.elementAt(i); Vector pointList = tcur.getPath(); if (pointList.size()>0) { int id=tcur.getCursorID(); float px = convertX(tcur.getY()); float py = convertY(tcur.getX()); float oscX = px/width; float oscY = py/height; finger[id].x=px; finger[id].y=py; //appears in screen fill(finger[id].col); ellipse(finger[id].x, finger[id].y, finger[id].radius*2, finger[id].radius*2); //interaction with each ball for(int j=0; j<balls.length; j++){ if(finger[id].intersects(balls[j])){ finger[id].col = balls[j].col = color(0); resolveBallCollision(finger[id], balls[j]); } } // send OSC message oscFinger[id] = new OscMessage("/system/finger/"+id); oscFinger[id].add(new float[] {oscX, oscY}); oscP5.send (oscFinger[id], netAddress); } } } float convertX(float x){ return (1-x)*width; } float convertY(float y){ return (1-y)*height; } void initBalls(){ balls = new BallParticle[10]; for(int i=0; i<balls.length; i++) balls[i] = new BallParticle(50+(i%5)*100, 100+(i/5)*100, 25); } void updateBalls(){ for(int i=0; i<balls.length; i++){ balls[i].verlet(0,0,0.97); balls[i].col=lerpColor(balls[i].col, balls[i].init_col, 0.1); } for(int i=1; i<balls.length; i++){ for(int j=0; j<i; j++){ if(balls[i].intersects(balls[j])){ balls[i].col = balls[j].col = color(0); resolveBallCollision(balls[i], balls[j]); } constrainBalls(balls[i]); } } for(int i=0; i<balls.length; i++){ fill(balls[i].col); //sendOSC oscBall[i] = new OscMessage("/system/sound/"+i); oscBall[i].add(new float[] {1-(balls[i].x/width), 1-(balls[i].y/height)}); oscP5.send(oscBall[i], netAddress); ellipse(balls[i].x, balls[i].y, balls[i].radius*2, balls[i].radius*2); } } void setupOSC(){ oscP5 = new OscP5(this, port); netAddress = new NetAddress(ip, port); oscBall = new OscMessage[balls.length]; oscFinger = new OscMessage[nFingers]; }