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