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 & HelpPrograms › Putting Traer Physic built objects into classes
Page Index Toggle Pages: 1
Putting Traer Physic built objects into classes (Read 1256 times)
Putting Traer Physic built objects into classes
Oct 5th, 2009, 8:54am
 
Hi, i hope i will get some help as this is driving me crazy for some days now. After my question if i should use traer or toxis verlet physics. Big thanks to PhiLho for answering! I will go with traer physics for this project.

My main goal is to create little objects built out of particles. Every Object should be the instance of a class.

So i first tried to create a class with this simple Ring. But it is not working . No matter what i do i get different kind of errors. I am not sure if i have to create different kind of particle systems to make it work or where to put the tick, etc... like i said, i tried different szenarios but i always got a different kind of error.  Only thing that worked was when i put everything in the class, also the particle system itself. but i guess then its impossible to make them appeal ech other what is my next problem. Next step would be to make them react to each other so that they appeal or repel each other.

so if anybody has made a sketch where he creates some objects out of traer particles and put them into classes, i would be thankful for any hints or tipps


this is a simpel test sketch with only one object without classes:

Code:
import traer.physics.*;

Particle mouse;
ParticleSystem physics;

Particle[] ball;
Spring[] s;


void setup(){
 size( 400, 400 );
 ellipseMode( CENTER );
 noStroke();

 //Create ParticleSystem
 physics = new ParticleSystem( 0, 0.1 );

 //Create Particles
 mouse = physics.makeParticle();
 mouse.makeFixed();


 ball = new Particle[10];
 s = new Spring[10];

 //create Balls and Mouse Attraction
 for(int i=0; i<  ball.length; i++){
   ball[i] = physics.makeParticle( 1.0, random(width),random(height ), 0 );
   physics.makeAttraction( mouse, ball[i],  3000, 50 );
 }

 for(int i=0; i<  ball.length; i++){
   for(int j=i+1; j<  ball.length; j++){
     physics.makeAttraction( ball[i], ball[j], -4500, 50 );
   }
 }

 //Create Springs with strength, damping,  restLength
 for(int i=0; i<  ball.length-1; i++){
   s[i] = physics.makeSpring( ball[i], ball[i+1], 0.4, 0.1,4);
 }
 s[9] = physics.makeSpring( ball[0], ball[ball.length-1], 0.4, 0.1,  4);

}


void draw(){
 physics.tick();
 background( 255 );
 mouse.position().set( mouseX, mouseY, 0 );


 ellipse(mouse.position().x(),mouse.position().y(),15,15);

 fill(0);

 for ( int i = 0; i < ball.length; i++ ){
   ellipse(ball[i].position().x(),ball[i].position().y(),5,5);
 }

 stroke(0);

 for(int i=0; i<  ball.length-1; i++){
   line( ball[i].position().x(), ball[i].position().y(), ball[i+1].position().x(), ball[i+1].position().y() );
 }

 line( ball[0].position().x(), ball[0].position().y(), ball[ball.length -1].position().x(), ball[ball.length -1].position().y() );
}




this is the code where i tried to put it into classes  and it seems to work, but how is it possible to let them interact with each other or the mouse now?

Code:
import traer.physics.*;
Circle[] circle = new Circle[5];



void setup(){
 size( 400, 400 );
 ellipseMode( CENTER );
 noStroke();

 for (int i=0; i<circle.length; i++){
   circle[i] = new Circle(random(width), random(height));
 }
}

void draw(){
 background( 255 );
 for (int i=0; i<circle.length; i++){
   circle[i].render();
 }
}

class Circle{
 float x;
 float y;
 Particle[] ball;
 Spring[] s;
 ParticleSystem physics;


 Circle(float _x, float _y){
   x = _x;
   y = _y;
   //Create ParticleSystem
   physics = new ParticleSystem( 0, 0.1 );
   ball = new Particle[10];
   s = new Spring[10];

   //create Balls and Mouse Attraction
   for(int i=0; i<  ball.length; i++){
     ball[i] = physics.makeParticle( 1.0, x + random( -30, 30 ),y + random( -30, 30 ), 0 );

   }

   for(int i=0; i<  ball.length; i++){
     for(int j=i+1; j<  ball.length; j++){
       physics.makeAttraction( ball[i], ball[j], -4500, 50 );
     }
   }

   //Create Springs with strength, damping,  restLength
   for(int i=0; i<  ball.length-1; i++){
     s[i] = physics.makeSpring( ball[i], ball[i+1], 0.4, 0.1,4);
   }
   s[9] = physics.makeSpring( ball[0], ball[ball.length-1], 0.4, 0.1,  4);

 }

 void render(){
   fill(0);
   physics.tick();
   for ( int i = 0; i < ball.length; i++ ){
     ellipse(ball[i].position().x(),ball[i].position().y(),5,5);
   }

   stroke(0);

   for(int i=0; i<  ball.length-1; i++){
     line( ball[i].position().x(), ball[i].position().y(), ball[i+1].position().x(), ball[i+1].position().y() );
   }

   line( ball[0].position().x(), ball[0].position().y(), ball[ball.length -1].position().x(), ball[ball.length -1].position().y() );
 }

}






Re: Putting Traer Physic built objects into classes
Reply #1 - Oct 5th, 2009, 9:31am
 
Don't know traer, but I'm suspicious of lines like this:
ball[i].position().x()
position() I can see, if it's a function of the ball's class which returns, say, a PVector.  But the x(), with parentheses, confuses me -- is the position() function returning another object of another class, which owns an x() function?
--Ben
Re: Putting Traer Physic built objects into classes
Reply #2 - Oct 5th, 2009, 9:38am
 
i know its unusual, but thats the way it is done with traer physics

Quote:
float position().x()
float position().y()
float position().z()

This is how you get to the dimensions of particle position.



http://www.cs.princeton.edu/~traer/physics/
Re: Putting Traer Physic built objects into classes
Reply #3 - Oct 5th, 2009, 9:49am
 
Ok, never mind that then :)  what are the errors?
Re: Putting Traer Physic built objects into classes
Reply #4 - Oct 5th, 2009, 9:53am
 
just to make an example. this is one of my test to make 2 created objects make react to the mouse. like the one in the first example above would do.


Code:
import traer.physics.*;
Circle[] circle = new Circle[2];

Particle mouse;
ParticleSystem physics;


void setup(){
size( 400, 400 );
ellipseMode( CENTER );
noStroke();

//Create ParticleSystem
physics = new ParticleSystem( 0, 0.1 );

for (int i=0; i<circle.length; i++){
circle[i] = new Circle(random(width), random(height));
}

//Create Particles
mouse = physics.makeParticle();
mouse.makeFixed();

}


void draw(){
physics.tick();
background( 255 );
mouse.position().set( mouseX, mouseY, 0 );


ellipse(mouse.position().x(),mouse.position().y(),15,15);
for (int i=0; i<circle.length; i++){
circle[i].render();
}

}



class Circle{
float x;
float y;
Particle[] ball;
Spring[] s;


Circle(float _x, float _y){
x = _x;
y = _y;

ball = new Particle[10];
s = new Spring[10];

//create Balls and Mouse Attraction
for(int i=0; i< ball.length; i++){
ball[i] = physics.makeParticle( 1.0, x + random( -30, 30 ),y + random( -30, 30 ), 0 );
physics.makeAttraction( mouse, ball[i], 10000, 50 );
}

for(int i=0; i< ball.length; i++){
for(int j=i+1; j< ball.length; j++){
physics.makeAttraction( ball[i], ball[j], -4500, 50 );
}
}

//Create Springs with strength, damping, restLength
for(int i=0; i< ball.length-1; i++){
s[i] = physics.makeSpring( ball[i], ball[i+1], 0.4, 0.1,4);
}
s[9] = physics.makeSpring( ball[0], ball[ball.length-1], 0.4, 0.1, 4);

}

void render(){
fill(0);

for ( int i = 0; i < ball.length; i++ ){
ellipse(ball[i].position().x(),ball[i].position().y(),5,5);
}

stroke(0);

for(int i=0; i< ball.length-1; i++){
line( ball[i].position().x(), ball[i].position().y(), ball[i+1].position().x(), ball[i+1].position().y() );
}

line( ball[0].position().x(), ball[0].position().y(), ball[ball.length -1].position().x(), ball[ball.length -1].position().y() );
}

}




Re: Putting Traer Physic built objects into classes
Reply #5 - Oct 5th, 2009, 10:41am
 
Looks like you just have to move your circle creation to the end of setup();

--Ben
Re: Putting Traer Physic built objects into classes
Reply #6 - Oct 5th, 2009, 10:50am
 
right. how can i make them attract to the mouse if the mouse isnt created yet. I am an idiot. thank you!
Re: Putting Traer Physic built objects into classes
Reply #7 - Oct 5th, 2009, 10:53am
 
Heh, I doubt you're an idiot.  Cool wigglyrings.
Page Index Toggle Pages: 1