help before I explode
in
Contributed Library Questions
•
1 year ago
hi all,
I'm stuck with a bit of code and am fairly sure my computer is laughing at me, or else I've been looking at the screen for so long now I'm gone demented...can someone please throw a fresh pair of eyes on this and help me out of a hole!!
the code will create a ball on a mousePress() at a random location, and is subject to gravity. but i want more control over each individual ball, for example id like each ball to be a different (random) color, I've tried a couple of different ways but each time I just get more and more confused.
can someone please help
Thanks
import traer.physics.*;
//*****************************************************Global Variables ****************************************************
float f; // particle x position
float g; // particle y position
float r = random(0,255);
float grn = random(0,255);
float blu = random(0,255);
PFont font;
String s = "S";
ParticleSystem phy = new ParticleSystem();
Particle b;
PImage img;
//*****************************************************void setup*********************************************************
void setup() {
size(500,500);
smooth();
ellipseMode( CENTER );
font = loadFont("AngsanaNew-Italic-24.vlw");
b = phy.makeParticle( 1.0, random( 0, width ),50, 0 );
phy.setGravity(.5);
img = loadImage("imga.jpg");
}
//*****************************************************void draw *******************************************************
void draw() {
phy.tick(); //advances the animation through time
background(255);
//maybe put an i value in() here from a looop
drawNetwork();
handleBoundaryCollisions( b );
}
//************************************************************************************************************************
void drawNetwork() {
for ( int i = 0; i < phy.numberOfParticles(); ++i )
{
fill(r,grn,blu);
Particle v = phy.getParticle( i );
ellipse( v.position().x(), v.position().y(), 16, 16);
handleBoundaryCollisions( v );
}
}
void mousePressed()
{
addNode();
}
void addNode() {
Particle p = phy.makeParticle();
Particle q = phy.getParticle( (int)random( 0, phy.numberOfParticles()-1) );
p.position().set( random(width), random(0, 10), 0 );
}
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.3*p.velocity().y(), 0 );
p.position().set( constrain( p.position().x(), 0, width ), constrain( p.position().y(), 0, height ), 0 );
}
1