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 › Move Ellipse to a Coordinat
Page Index Toggle Pages: 1
Move Ellipse to a Coordinat (Read 1603 times)
Move Ellipse to a Coordinat
Dec 26th, 2009, 11:44am
 
So far I have:

class Ball {
 color c;
 float xpos;
 float ypos;
 float xspeed;
 
Ball(color tempC, float tempXpos, float tempYpos, float tempXspeed) {
 c = tempC;
 xpos = tempXpos;
 ypos = tempYpos;
 xspeed = (250,250);
}
 
void display() {
 stroke(0);
 fill(c);
 ellipseMode(CENTER);
 ellipse(xpos,ypos,20,10);
}
 
void drive(){
 xpos = xpos + xspeed;
 if (xpos > width) {
   xpos = 0;
 }

(this is just a sample of my code)


from one of the processing tutorials.

It's a really simple problem, I am new to Processing, but I can't figure out how to replace "xspeed" with a coordinate, or if it needs a new function.
Re: Move Ellipse to a Coordinat
Reply #1 - Dec 27th, 2009, 12:10am
 
http://processing.org/reference/PVector.html ?
Re: Move Ellipse to a Coordinat
Reply #2 - Dec 27th, 2009, 2:59am
 
Just add a yspeed variable?
Re: Move Ellipse to a Coordinat
Reply #3 - Dec 29th, 2009, 9:56pm
 
@JR
Strangely enough, there's no "move" function in that code, contrary to the description.

@Phi.ho
I tried, it then moves the ellipse diagonally, but not to a point.
Re: Move Ellipse to a Coordinat
Reply #4 - Dec 30th, 2009, 2:24am
 
Well there are lots of ways of handling motion, but whichever you choose you're likely to need yspeed and then in each frame you add appropriate xspeed and yspeed to the ball's xpos and ypos.  And in that case xspeed and yspeed would be relatively small numbers rather than "(250,250)"...  Presumably that's the point you want to move it to?

Here's a 'motion to a target' example using easing - you should be able to apply the same principles to your ball:

Code:
// easing: the larger the number the faster it moves
float easing = 0.1;
// start position
int posX = 50;
int posY = 50;
// target position
int targetX = 300;
int targetY = 300;

boolean dragging = false;

void setup() {
 size(600,600);
 smooth();
}

void draw() {
background(0,155,155);
if (!dragging) {
  // calculate the difference in position, apply easing and add to vx/vy
   float vx = (targetX - posX) * easing;
   float vy = (targetY - posY) * easing;
   // Add the velocity to the current position: make it move!
   posX += vx;
   posY += vy;
}

if(mousePressed) {
  dragging = true;
  posX = mouseX;
  posY = mouseY;
}
else {
  dragging = false;
}
// draw the ball
 noStroke();
 fill(204, 102, 0);
 ellipse(posX, posY, 50, 50);
   
}
Re: Move Ellipse to a Coordinat
Reply #5 - Jan 2nd, 2010, 4:40pm
 
blindfish wrote on Dec 30th, 2009, 2:24am:
Well there are lots of ways of handling motion, but whichever you choose you're likely to need yspeed and then in each frame you add appropriate xspeed and yspeed to the ball's xpos and ypos.  And in that case xspeed and yspeed would be relatively small numbers rather than "(250,250)"...  Presumably that's the point you want to move it to

Here's a 'motion to a target' example using easing - you should be able to apply the same principles to your ball:

Code:
// easing: the larger the number the faster it moves
float easing = 0.1;
// start position
int posX = 50;
int posY = 50;
// target position
int targetX = 300;
int targetY = 300;

boolean dragging = false;

void setup() {
 size(600,600);
 smooth();
}

void draw() {
background(0,155,155);
if (!dragging) {
  // calculate the difference in position, apply easing and add to vx/vy
   float vx = (targetX - posX) * easing;
   float vy = (targetY - posY) * easing;
   // Add the velocity to the current position: make it move!
   posX += vx;
   posY += vy;
}

if(mousePressed) {
  dragging = true;
  posX = mouseX;
  posY = mouseY;
}
else {
  dragging = false;
}
// draw the ball
 noStroke();
 fill(204, 102, 0);
 ellipse(posX, posY, 50, 50);
   
}

OH MY GOD THANK YOU. Rep +1
Page Index Toggle Pages: 1