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 › rotate / align images on particles
Page Index Toggle Pages: 1
rotate / align images on particles (Read 1097 times)
rotate / align images on particles
Dec 10th, 2009, 1:00pm
 
i am using traer physics to create some kind of creatures and i want to keep the structure as simple as possible. so creating some meshes is maybe not a god solution. Anyway. What i want to do is to add images to the particles. But i am having some problems rotating the images they way they should be.
To explain it better i made a quick sketch of what i got and what i want.

...

I know i asked some similar questions before. But is there a simple way to do that without adding any more particles or do some complicated vector math?


Here is some code to test it :

Code:
import traer.physics.*;

Particle mouse, p1, p2;
Particle[] ball;
Spring[] s;
ParticleSystem physics;



void setup(){
size( 400, 400 );
frameRate( 24 );
smooth();
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];

//Mouse Attraction
for(int i=0; i< ball.length; i++){
ball[i] = physics.makeParticle( 1.0, random( 0, width ), random( 0, 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], -4000, 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.5, 0.1, 10);
}
s[9] = physics.makeSpring( ball[0], ball[ball.length-1], 0.5, 0.1, 10);


}


void draw(){

mouse.position().set( mouseX, mouseY, 0 );
physics.tick();

background( 255 );
fill(0);
ellipse(mouse.position().x(),mouse.position().y(),15,15);

for ( int i = 0; i < ball.length; i++ )
{
beginShape();
fill(255,0,0,150);
rect(ball[i].position().x(),ball[i].position().y(),2,25);
// 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() );

noStroke();
}
Re: rotate / align images on particles
Reply #1 - Dec 11th, 2009, 4:32am
 
Work with springs, maybe ?

Code:
for (int i = 0; i < springs.length; i++) {
 Particle start = springs[i].getOneEnd();
 Particle end   = springs[i].getTheOtherEnd();
 float x1 = start.position().x();
 float y1 = start.position().y();
 float x2 = end.position().x();
 float y2 = end.position().y();
 line(x1, y1, x2, y2);
 float a = atan2(y2 - y1, x2 - x1);
 pushMatrix();
   translate(x1 + (x2 - x1)/2, y1 + (y2 - y1)/2);
   rotate(a);
   rect(0, 0, 2, 25);
 popMatrix();
}
Re: rotate / align images on particles
Reply #2 - Dec 11th, 2009, 8:51am
 
Thats really good. Thank you!
i had a similar idea when i was on the bust today.
i thought i could use the coordinates of the points to get the rotating angle. thast actually really close to what you suggested...

i used your code and just adapted it to what i thought of...

  Code:
for ( int i = 0; i < ball.length; i++ )  {
   
 float x1 = ball[i].position().x();
 float y1 = ball[i].position().y();
 float x2 = ball[i+1].position().x();
 float y2 = ball[i+1].position().y();

float a = atan2(y2 - y1, x2 - x1);
 pushMatrix();
   translate(x1 + (x2 - x1)/2, y1 + (y2 - y1)/2);
   rotate(a);
   rect(0, 0, 2, 25);
 popMatrix();
 }



its working rightnow.

only problem i dont know how to align the last or first element and draw it within the for loop.

oh and another question is, do you know what to change to get them pack to the particle positions (corners of the shape) and not between them ?


here is the whole working code :
Code:

import traer.physics.*;

Particle mouse, p1, p2;
Particle[] ball;
Spring[] s;
ParticleSystem physics;



void setup(){
 size( 400, 400 );
 frameRate( 24 );
 smooth();
 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];

 //Mouse Attraction
 for(int i=0; i<  ball.length; i++){
   ball[i] = physics.makeParticle( 1.0, random( 0, width ), random( 0, 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], -4000, 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.5, 0.1, 10);
 }
 s[9] = physics.makeSpring( ball[0], ball[ball.length-1], 0.5, 0.1, 10);


}


void draw(){

 mouse.position().set( mouseX, mouseY, 0 );
 physics.tick();

 background( 255 );
 fill(0);
 ellipse(mouse.position().x(),mouse.position().y(),15,15);

 


 for ( int i = 0; i < ball.length-1; i++ )  {
   
 float x1 = ball[i].position().x();
 float y1 = ball[i].position().y();
 float x2 = ball[i+1].position().x();
 float y2 = ball[i+1].position().y();

float a = atan2(y2 - y1, x2 - x1);
 pushMatrix();
   translate(x1 + (x2 - x1)/2, y1 + (y2 - y1)/2);
   rotate(a);
   rect(0, 0, 2, 25);
 popMatrix();
 }



 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() );

 noStroke();
}





Re: rotate / align images on particles
Reply #3 - Dec 11th, 2009, 10:26am
 
Quote:
only problem i dont know how to align the last or first element and draw it within the for loop.

You can't, since you are calculating the angle between two consecutive nodes in the array. By the way, I would stick to the spring idea, that would avoid the problem.

oh and another question is, do you know what to change to get them pack to the particle positions (corners of the shape) and not between them ?
change the translate(...) command to translate(x1, y1), but since my code was about springs, the atan(...) formula is no more appropriate (just try and see : the angle is bad).
Re: rotate / align images on particles
Reply #4 - Dec 11th, 2009, 10:31am
 
alright, i will try and work with the springs. probably the better solution in this case! thx again
Re: rotate / align images on particles
Reply #5 - Dec 11th, 2009, 10:44am
 
or try this :

in setup(), after having filled ball :
Code:
ball_extended = new Particle[ball.length + 2];
arrayCopy(ball, 0, ball_extended, 1, ball.length);
ball_extended[0] = ball[ball.length - 1];
ball_extended[ball_extended.length - 1] = ball[0];


in draw() :
Code:
for (int i = 1; i < ball_extended.length - 1; i++)  {
 float x1 = ball_extended[i-1].position().x();
 float y1 = ball_extended[i-1].position().y();
 float x2 = ball_extended[i+1].position().x();
 float y2 = ball_extended[i+1].position().y();
 float a = atan2(y2 - y1, x2 - x1);
 pushMatrix();
   translate(ball_extended[i].position().x(), ball_extended[i].position().y());
   rotate(a);
   rect(0, 0, 2, 25);
 popMatrix();
}
Page Index Toggle Pages: 1