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 › forloop and individual settings ...
Page Index Toggle Pages: 1
forloop and individual settings ... (Read 358 times)
forloop and individual settings ...
Jan 23rd, 2006, 4:46pm
 
Hi,

A beginners question: I am trying to use a for-loop to create multiple shapes that attract or repel differently to the magnetic shape that follows the mouse.
Each shape should be attracted with various ranges, and each shape should be repelled into a certain distance, so that it doesn't slide outside the screen area.



import processing.opengl.*;

float wX;
float wY;
float delay = 60.0;


void setup(){
  size(300, 300, OPENGL);
  fill(200, 0, 100);
  noStroke();
  framerate(30);
  //smooth();
}
 

void draw(){
 background(0);
 
 
 if(mousePressed==true) { // attract
   float dx = mouseX - wX;
   if(abs(dx) > 20) {
     wX = wX + dx/delay;
   }
   float dy = mouseY - wY;
   if(abs(dy) > 20) { // this number should vary for each shape when it is  
                             //repeated by the forloop
     wY = wY + dy/delay;
   }
 } else { // repel
   float dx = mouseX + wX;
   if(abs(dx) > 20) {
     wX = wX - dx/delay; // it should only repel untill a certain number that
                                     //varies for each shape
       
   }
   float dy = mouseY + wY;
   if(abs(dy) > 20) {
     wY = wY - dy*1/delay;
   }
     
 }  
 
 pushMatrix();
 translate(wX, wY);
 beginShape(POLYGON);
   curveVertex(200,  200);
   curveVertex(84,  91);
   curveVertex(68,  19);
   curveVertex(21,  17);
   curveVertex(32, 100);
   curveVertex(32, 100);
 endShape();
 popMatrix();
 
 // 'magnetic shape'
 pushMatrix();
 translate(mouseX, mouseY);
 beginShape(POLYGON);
   curveVertex(200,  200);
   curveVertex(84,  91);
   curveVertex(68,  19);
   curveVertex(21,  17);
   curveVertex(32, 100);
   curveVertex(32, 100);
 endShape();
 point(mouseX+50, mouseY+50);
 popMatrix();
 
}
Page Index Toggle Pages: 1