arta
YaBB Newbies
Offline
Posts: 13
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; } }