We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › How do I create multiple instances of this object
Page Index Toggle Pages: 1
How do I create multiple instances of this object? (Read 782 times)
How do I create multiple instances of this object?
Nov 11th, 2009, 12:25pm
 
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);

}

}




Re: How do I create multiple instances of this object?
Reply #1 - Nov 12th, 2009, 1:45am
 
I won't solve your problem, but I have a remark on your approach.
You create two (nearly) identical classes. It is a no-no in programming, avoiding duplicate work is a must.
If you need two instances of a class, just use 'new' twice. Perhaps with parameters on the constructor (or changing some fields after) to make them behave differently.

More related to your issue: you have only one global physics variable. When you create mesh, you initialize the variable. When you create mesh2, you overwrite the previous value.
You should put physics declaration in Mesh, so that each instance owns its own physics engine.
Re: How do I create multiple instances of this object?
Reply #2 - Nov 12th, 2009, 2:24am
 
Hi PhilLo,

As always I owe you a massive thank you!! Smiley

Thank you for the advice and also putting the physics variable declaration inside the class worked! This is a massive help. Thank you so much!
Page Index Toggle Pages: 1