Hi,
This is a very basic question about how to create multiple instances of Toxi's soft body demo verlet particle mesh.
As a demonstration I have created 2 separate classes Mesh and Mesh2.
I initialise them in void setup but for some reason processing only displays the second class (Mesh2).
I get the feeling that I am calling the same Verletphysics mesh in each. How do I create two separate meshes? Is this possible?
Thanks. Here is the code:
Code:
/**
* Softbody square demo
*/
import processing.opengl.*;
import toxi.physics2d.constraints.*;
import toxi.physics2d.*;
import toxi.geom.*;
import toxi.math.*;
Timer timer;
int DIM=28;
int REST_LENGTH=18;
float STRENGTH=0.125;
float INNER_STRENGTH = 0.2;
VerletPhysics2D physics;
VerletParticle2D a1, a5, a6, b1, b5, b6;
Mesh mesh;
Mesh2 mesh2;
/// Setup
void setup() {
size(1280,720,OPENGL);
smooth();
timer = new Timer(3200);
timer.start();
mesh = new Mesh ();
mesh2 = new Mesh2 ();
}
////Draw
void draw() {
background(0);
stroke(255);
mesh.points();
mesh2.points();
physics.update();
if (timer.isFinished()) {
// update();
timer.start();
}
if (mousePressed == true) {
a5.set(random(1280),random(720));
a6.set(random(1280),random(720));
} else {
a5.unlock();
a6.unlock();
}
// Iterator in the library, default goes to 50
for(Iterator i=physics.springs.iterator(); i.hasNext();) {
VerletSpring2D s=(VerletSpring2D)i.next();
line(s.a.x,s.a.y,s.b.x,s.b.y);
}
}
void update(){
int d = round(random(1280));
int f = round(random(720));
a5.set(d,f);
a6.set(d,f);
if (timer.isFinished()) {
loop();
timer.start();
}
}
class Timer {
int savedTime; // When Timer started
int totalTime; // How long Timer should last
Timer(int tempTotalTime) {
totalTime = tempTotalTime;
}
// Starting the timer
void start() {
savedTime = millis();
}
boolean isFinished() {
int passedTime = millis()- savedTime;
if (passedTime > totalTime) {
return true;
} else {
return false;
}
}
}
///////////// Two classes Mesh and Mesh2 below:
class Mesh {
Mesh(){
physics=new VerletPhysics2D();
physics.gravity=new Vec2D(0,0.006);
physics.setWorldBounds(new Rect(0,0,width,height));
for(int y=0,idx=0; y<DIM; y++) {
for(int x=0; x<DIM; x++) {
// Verlet Particle are the matrix points, x*Rest_Length etc. are scaling factors - it's creating a series of new objects for the mesh layout
//Make array of matrices here
VerletParticle2D p=new VerletParticle2D(x*REST_LENGTH,y*REST_LENGTH);
// Adds a particle to the list
physics.addParticle(p);
if (x>0) {
VerletSpring2D s=new VerletSpring2D(p,physics.particles.get(idx-1),REST_LENGTH,STRENGTH);
physics.addSpring(s);
}
if (y>0) {
VerletSpring2D s=new VerletSpring2D(p,physics.particles.get(idx-DIM),REST_LENGTH,STRENGTH);
physics.addSpring(s);
}
idx++;
}
}
a1=physics.particles.get((DIM-1));
// Total number is 195
a5=physics.particles.get(288);
a6=physics.particles.get(198);
a1.lock();
}
void points(){
a1.set(360,30);
}
}
class Mesh2 {
Mesh2(){
physics=new VerletPhysics2D();
physics.gravity=new Vec2D(0,0.006);
physics.setWorldBounds(new Rect(0,0,width,height));
for(int y=0,idx=0; y<DIM; y++) {
for(int x=0; x<DIM; x++) {
// Verlet Particle are the matrix points, x*Rest_Length etc. are scaling factors - it's creating a series of new objects for the mesh layout
//Make array of matrices here
VerletParticle2D p2=new VerletParticle2D(x*REST_LENGTH,y*REST_LENGTH);
// Adds a particle to the list
physics.addParticle(p2);
if (x>0) {
VerletSpring2D s2=new VerletSpring2D(p2,physics.particles.get(idx-1),REST_LENGTH,STRENGTH);
physics.addSpring(s2);
}
if (y>0) {
VerletSpring2D s2=new VerletSpring2D(p2,physics.particles.get(idx-DIM),REST_LENGTH,STRENGTH);
physics.addSpring(s2);
}
idx++;
}
}
b1=physics.particles.get((DIM-1));
// Total number is 195
b5=physics.particles.get(288);
b6=physics.particles.get(198);
b1.lock();
}
void points(){
b1.set(760,100);
}
}