forces that bounce
in
Contributed Library Questions
•
1 year ago
Hi all, can someone please tell me how to make all the balls in this program bounce off each other?? and also is it possible to rearrange them according to a pattern???
Thanks :)
import traer.physics.*;
//*****************************************************Global Variables ****************************************************
ArrayList particles;
Ball ballobject = new Ball();
ArrayList colors = new ArrayList();
PFont font;
String s = "S";
ParticleSystem phy = new ParticleSystem();
//*****************************************************void setup*********************************************************
void setup() {
size(500,500);
smooth();
ellipseMode( RADIUS );
//font = loadFont("AngsanaNew-Italic-24.vlw");
colors.add(color(255));
phy.setGravity(.5);
particles = new ArrayList();
}
//*****************************************************void draw *******************************************************
void draw() {
phy.tick(); //advances the animation through time
background(255);
ballobject.drawNetwork();
}
//************************************************************************************************************************
void mousePressed()
{
ballobject.addNode();
}
class Ball {
PVector pos = new PVector();
float rad = 16;
Particle v;
Ball() {
}
void drawNetwork() {
for ( int i = 0; i < particles.size(); ++i )
{
v = phy.getParticle( i );
fill( (Integer)colors.get(i) );
ellipse( v.position().x(), v.position().y(), rad, rad); // NOTE here is how the animation zooms out %%%%%%%%%%%%%%%%%%%%%%%
handleBoundaryCollisions( v );
pos.x = v.position().x();
pos.y = v.position().y();
}
}
void addNode() {
colors.add(color(random(255), random(255), random(255)));
Particle p = phy.makeParticle();
// Particle q = phy.getParticle( (int)random( 0, phy.numberOfParticles()-1) );
p.position().set( random(width), random(0, 10), 0 );
particles.add(new Ball());
}
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 );
}
//#######################################################################
void display() {
//fill
//ellipse
}
}// class end
1