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.
Page Index Toggle Pages: 1
mouse follow (Read 508 times)
mouse follow
Nov 12th, 2008, 3:45pm
 
can anybody help me figure out how to get the tips of the tree branches to follow the mouse in this script? i have tried to use an example and create a 'void mousefollow' however nothing happens!

Tree branches;

void setup() {
 size(500,500);
 smooth();  
 branches = new Tree (6,5);
}
void draw() {
 background(192);
 stroke(255);
 branches.draw();
}
class Tree
{
 float m = 100;
 float n = 100;
 float x2= width/2;
 float y2= height/2;

 float dx;
 float dy;

 int frame=0;  
 float theta;
 float branch;
 float branchlength;
 float a;
 float y;

 Tree (float x, float s)
 {
   a = x; // this controls the angle of the branches
   theta = radians(a);
   branchlength = (height/s);  
   branch=y;
 }
 void draw () {
   translate(x2,y2);
   // Start the recursive branching!
   branch(branchlength);
   mousefollow(x2,y2); //think this is wrong???
 }
 void branch (float y){
   // Each branch will be 0.7 the size of the previous one
   y=y*0.7;
   if (y > 10) {
     pushMatrix();    
     rotate(a);  
     line(0,0,0,-y);  
     translate(0,-y);
     branch(y);
     popMatrix();  
     //  branch off to left
     pushMatrix();
     rotate(-a+0.6);
     line(0,0,0,-y+2);
     translate(0,-y+2);
     branch(y+0.4);
     popMatrix();
   }
 }
 //this is taken from the reach1 example.
 void mousefollow(float m,float n){
   float dx = mouseX - m; //position of mousex
   float dy = mouseY - n; //position of mousey
   float angle1 = atan2(dy, dx);  //angle of the branches
   float tx = mouseX - cos(angle1) * branchlength;  
float ty = mouseY - sin(angle1) * branchlength;

   dx = tx - x2;
   dy = ty - y2;

   float angle2 = atan2(dy, dx);  
   m = x2 + cos(angle2) * branchlength;
   n = y2 + sin(angle2) * branchlength;
 }
}


Re: mouse follow
Reply #1 - Nov 13th, 2008, 12:49pm
 
I am not sure what you expect your mousefollow to do, but currently it does a number of operations that are just lost at the end of the function...

You set m and n, but they are passed to the function by value, the parameters act as local variables.

If you plan to modify x2 and y2, just use them: they are visible from the mousefollow() method, since it is in the class.
Page Index Toggle Pages: 1