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 › ellipse follow mouse within set boundries
Page Index Toggle Pages: 1
ellipse follow mouse within set boundries (Read 996 times)
ellipse follow mouse within set boundries
Jan 9th, 2008, 8:55pm
 
okay so heres my conundrum:

i'm very new to processing and am trying to figure out how to do the following

i have an image of a cartoon character, I want to make the black dot (pupil i guess) of his eye follow the mouse as it moves around the window. however I do not want the dot to leave the boundaries of the eye (otherwise we'd have flying pupils) but i have no idea how i would go about doing so.

i figured out how to get the dot to follow the mouse, but have yet to figure out how to set the boundaries for them. it would be great help!
Re: ellipse follow mouse within set boundries
Reply #1 - Jan 9th, 2008, 9:38pm
 
This will solve your problems: http://processing.org/learning/basics/constrain.html
Re: ellipse follow mouse within set boundries
Reply #2 - Jan 9th, 2008, 9:52pm
 
perfect!! thanks so much that's exactly what i needed!
Re: ellipse follow mouse within set boundries
Reply #3 - Jan 9th, 2008, 10:38pm
 
alright quick question,

i've got my first eyeball moving, however I cannot figure out how to go about creating the second one.

i tried copying the code and then changing the co-ordinates but that resulted in an error. any thoughts?
My Code
Reply #4 - Jan 10th, 2008, 5:15am
 
The Code Thus Far:

PImage bg;
int a;
float mx;
float my;
float easing = 0.05;
float esize = 4.5;
int box = 10;


void setup()
{
 size(351,337);
 bg = loadImage("170.jpg");
 noStroke();
 smooth();
 ellipseMode(CENTER_RADIUS);  
}

void draw()
{
 background(bg);
if(abs(mouseX - mx) > 0.1) {
   mx = mx + (mouseX - mx) * easing;
 }
 if(abs(mouseY - my) > 0.1) {
   my = my + (mouseY- my) * easing;
 }
 
 float distance = esize * 2;
 mx = constrain(mx, 120, 155);  //x co-ordinates for eye (min, max)
 my = constrain(my, 89, 115);  //y co-ordinate for eye (min, max)
 fill(0);  
 ellipse(mx, my, esize, esize);
 }
Re: ellipse follow mouse within set boundries
Reply #5 - Jan 10th, 2008, 6:29am
 
since you've changed (mx, my) values with constrain(), you can't reuse them for the second eye. try this instead :

Code:
 void draw() {

background(bg);
if(abs(mouseX - mx) > 0.1) {
mx = mx + (mouseX - mx) * easing;
}
if(abs(mouseY - my) > 0.1) {
my = my + (mouseY- my) * easing;
}

float distance = esize * 2;

float mx1 = constrain(mx, 120, 155); // eye 1
float my1 = constrain(my, 89, 115); // eye 1
float mx2 = constrain(mx, 220, 255); // eye 2
float my2 = constrain(my, 89, 115); // eye 2

fill(0);
ellipse(mx1, my1, esize, esize);
ellipse(mx2, my2, esize, esize);

}
Page Index Toggle Pages: 1