how to simulate pearl beads behaviour??
in
Contributed Library Questions
•
2 years ago
I wish to develop 3rd app in following video :
http://www.youtube.com/watch?v=G1iSD6StpCA&feature=player_embedded
I have developed following code but it does not simulate the pearl behavior smoothly. Can anyone please suggest me how can I improve the code.
Thanks.
import TUIO.*;
TuioProcessing tp;
boolean gravity = true;
ArrayList touchPoints;
/* Particle count */
int particleCount = 10;
PImage bk;
PImage pearl;
/* Particle array */
ArrayList particles ;
void setup()
{
size(screen.width,screen.height);
bk = loadImage("m1.jpg");//b11.jpg
bk.resize(width,height);
pearl = loadImage("p2.png");//p5
pearl.resize(13,13);
smooth();
frameRate(30);
tp = new TuioProcessing(this);
particles = new ArrayList();
}
void draw()
{
background(bk);
for (int j=0; j<particles.size()-1; j++)
{
Particle particle = (Particle) particles.get(j);
particle.flag =false;
}
Vector tuioCurList = tp.getTuioCursors();
float X,Y,lastX,lastY;
for(int i=0 ; i<tuioCurList.size() ;i++)
{
TuioCursor tcur = (TuioCursor)tuioCurList.elementAt(i);
Vector pointList = tcur.getPath();
if (pointList.size()<18)
{
TuioPoint start_point = (TuioPoint)pointList.firstElement();;
TuioPoint end_point = (TuioPoint)pointList.elementAt(pointList.size()-1);
X = start_point.getScreenX(width);
Y = start_point.getScreenY(height);
lastX = end_point.getScreenX(width);
lastY = end_point.getScreenY(height);
float angle = atan2(Y-lastY , X-lastX);
for (int j=0; j<particles.size()-1; j++)
{
Particle particle = (Particle) particles.get(j);
if(particle.id == tcur.getCursorID() )
{
if( particle.timer <=0)
particles.remove(j);
else
{
particle.flag = true;
particle.update2(angle);
particle.draw();
}
}
}
}
}
for (int j=0; j<particles.size(); j++)
{
Particle particle = (Particle) particles.get(j);
if (particle.flag ==false)
{
if( particle.timer <=0)
particles.remove(j);
else
{
particle.update3();
particle.draw();
}
}
}
}
class Particle
{
/* the x and y are our coordinates for each particles */
float x,y;
float id , Pangle;
float vx,vy;
boolean flag;
int timer;
/* Particle initialization. We define the beginning properties of each particle here. */
Particle(float X,float Y, float tid)
{
x = X;
y = Y;
id = tid;
Pangle = 0;
timer =(int)random(40,60);
}
[b]void update2(float angle)
{
// vx *= 0.99;
// vy *= 0.99;
timer -=1;
vx -= 2* cos(angle);
vy -= 2* sin(angle);
x+=vx;
y+=vy;
Pangle = angle;
}
[/b]
[b]void update3()
{
// vx *= 0.99;
//vy *= 0.99;
timer -=1;
boundaryChk();
vx -= 2 * cos(Pangle);
vy -= 2 * sin(Pangle);
x+=vx;
y+=vy;
}[/b]
void boundaryChk()
{
/* Border collisions */
if (x > width) {
/* Reverse the velocity if towards wall */
if (abs(vx) == vx)
vx *= -1.0;
/* The particle is placed back at the wall */
x = width;
}
if (x < 1) {
if (abs(vx) != vx)
vx *= -1.0;
x = 1;
}
if (y < 1) {
if (abs(vy) != vy)
vy *= -1.0;
y = 1;
}
if (y > height) {
if (abs(vy) == vy)
vy *= -1.0;
// vx *= 0.6;
y = height;
}
}
void draw()
{
image(pearl,int(x),int(y));
}
}
// called when an object is added to the scene
void addTuioObject(TuioObject tobj)
{
}
// called when an object is removed from the scene
void removeTuioObject(TuioObject tobj)
{
}
// called when an object is moved
void updateTuioObject (TuioObject tobj)
{
}
// called when a cursor is added to the scene
void addTuioCursor(TuioCursor tcur)
{
for(int i =0;i<5;i++)
[b]particles.add(new Particle( random(tcur.getScreenX(width)-20,tcur.getScreenX(width)+20),random(tcur.getScreenY(height)-15,tcur.getScreenY(height)+15) , tcur.getCursorID()) );[/b]
}
// called when a cursor is moved
void updateTuioCursor (TuioCursor tcur)
{
for(int i =0;i<5;i++)
[b] particles.add(new Particle( random(tcur.getScreenX(width)-5,tcur.getScreenX(width)+5),random(tcur.getScreenY(height)-10,tcur.getScreenY(height)), tcur.getCursorID() ) );
[/b]
}
// called when a cursor is removed from the scene
void removeTuioCursor(TuioCursor tcur)
{
}
// called after each message bundle
// representing the end of an image frame
void refresh(TuioTime bundleTime) {
redraw();
}
1