hello, I'm working on the bouncing ball example dowloaded from traer website.
Once i've created my "magnet" particle and created the attraction between the magnet and the all others particles, i'd like to change its strength() dynamically.
When I press the mouse button the attraction force is 10000 otherwise is 0. The code is below
any suggestion????
following the complete code with the error line commented...
Thanks a lot.....
Once i've created my "magnet" particle and created the attraction between the magnet and the all others particles, i'd like to change its strength() dynamically.
When I press the mouse button the attraction force is 10000 otherwise is 0. The code is below
- void mousePressed() {
attraction=10000;
mouse.setStrength( attraction );
}
void mouseReleased() {
attraction=0;
mouse.setStrength( attraction );
}
any suggestion????
following the complete code with the error line commented...
Thanks a lot.....
- ////******* modified from bouncing balls example @ ~traer/physics *******////
//// http://pixelorchestra.com ////
import processing.opengl.*;
import traer.physics.*;
Particle mouse;
ParticleSystem physics;
Particle[] p;
int numParticle=10000;
float attraction=10000;
void setup()
{
size( 800, 800, OPENGL);
// hint(ENABLE_OPENGL_4X_SMOOTH);
hint(DISABLE_DEPTH_TEST);
frameRate( 60 );
smooth();
ellipseMode( CENTER );
noStroke();
noCursor();
strokeWeight(2);
physics = new ParticleSystem(0,0);
mouse = physics.makeParticle();
mouse.makeFixed();
p= new Particle[numParticle];
for (int i=0; i<p.length;i++) {
p[i]=physics.makeParticle( 1.0, random( width/2-100, width/2+100 ), random( height/2-100,height/2+100 ), 0 );
p[i].velocity().add (random(-1,1), random(-1,1), 0 ); // add some random moviment
physics.makeAttraction( mouse, p[i],attraction, 100 ); // make attraction between mouse and particles
}
}
void draw()
{
physics.tick(); // advance particle simulation
mouse.position().set( mouseX, mouseY, 0 ); // move the magnet
for (int i=0; i<p.length;i++) { //
handleBoundaryCollisions( p[i] );
}
background( 0 );
// draw the magnet
stroke( 255 );
noFill();
ellipse( mouse.position().x(), mouse.position().y(), 35, 35 );
// draw the lines
stroke( 255,70 );
// for all particles
for (int i=0; i<p.length;i++) {
// if not the first particle
if(i!=0) {
// calculate the distance between the particles
float d=dist(p[i].position().x(), p[i].position().y(),p[i-1].position().x(), p[i-1].position().y());
// if the distance is less than 200
if(d<200)
// draw a line
line(p[i].position().x(), p[i].position().y(),p[i-1].position().x(), p[i-1].position().y());
}
}
}
// really basic collision strategy:
// sides of the window are walls
// if it hits a wall pull it outside the wall and flip the direction of the velocity
// the collisions aren't perfect so we take them down a notch too
void handleBoundaryCollisions( Particle p )
{
if ( p.position().x() < 0 || p.position().x() > width )
p.velocity().set( -0.9*p.velocity().x(), p.velocity().y(), 0 );
if ( p.position().y() < 0 || p.position().y() > height )
p.velocity().set( p.velocity().x(), -0.9*p.velocity().y(), 0 );
p.position().set( constrain( p.position().x(), 0, width ), constrain( p.position().y(), 0, height ), 0 );
}
void mousePressed() {
attraction=10000;
// mouse.setStrength( attraction );
}
void mouseReleased() {
attraction=0;
// mouse.setStrength( attraction );
}
1