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 › Moving an object to a specified coordinate
Page Index Toggle Pages: 1
Moving an object to a specified coordinate (Read 613 times)
Moving an object to a specified coordinate
Dec 31st, 2009, 3:59pm
 
In the following program:

Code:
int x,y;
int go = 0;

void setup() {
size(500,500);
background(0,0,0);
ellipseMode(CENTER);
}

void draw() {
if (go == 1) {
fill(x/2, y/2, 100);
background(0,0,0);
ellipse(x, y, 50, 50);
}
}

void mousePressed() {
go = 1;
x = mouseX;
y = mouseY;
}


I'm trying to get the ellipse that is created upon clicking the mouse to move to a specified coordinate. Any help on how to do this?

Thanks.
Re: Moving an object to a specified coordinate
Reply #1 - Dec 31st, 2009, 10:31pm
 
Try this...
Code:

float x,y;
float speed = 1;
PVector vm, ve, vgo;

void setup() {
 size(500,500);
 frameRate(60);
 background(0,0,0);
 ellipseMode(CENTER);
 vm = new PVector(0,0);
 ve = new PVector(0,0);
 vgo = new PVector(0,0);
}

void draw() {
   vgo = PVector.sub(vm,ve); //calculate the vector between ellipse and mouse
   vgo.normalize();  //sets the vector length to 1
   vgo.mult(speed);  //sets the vector length to "speed"
   ve.add(vgo);      //add the movement vector to the ellipse position
   fill(ve.x/2, ve.y/2, 100);  //draw ellipse
   background(0,0,0);
   ellipse(ve.x, ve.y, 50, 50);
}

void mousePressed() {
 vm = new PVector (mouseX, mouseY);
}  

Page Index Toggle Pages: 1