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 › simple easing motion
Page Index Toggle Pages: 1
simple easing motion (Read 367 times)
simple easing motion
Mar 19th, 2009, 5:21am
 
I am writing some very simple code that moves
a circle up, left, right, and down using the arrow
keys as input. The code that sets the x and y velocity
is here:

void keyPressed(){
   if( key == CODED)
   {
     if(keyCode == UP){
       yVel=-3;
     }
     if(keyCode == DOWN){
       yVel=3;
     }
     if(keyCode == LEFT){
       xVel=-3;
     }
     if(keyCode == RIGHT){
       xVel=3;
     }
   }
}

I also have a few lines of code that creates the "easing" effect by reducing the velocities by a certain amount each time draw runs, as well as updating the position based on the velocities:

xVel=xVel*0.94;
yVel=yVel*0.94;
posX+=xVel;
posY+=yVel;
ellipse(posX,posY,10,10);

My issue with this is that the code works fine for the left and up arrow keys (negative values of xVel or yVel), but the circle barely even moves to the right or up. The code seems like it should work properly, but I am stuck now.

Any help you could give me would be great.
Re: simple easing motion
Reply #1 - Mar 19th, 2009, 8:41am
 
I just copy-pasted your code, it seems to work just fine here?
All I did extra was to declare all the variables as float:

float xVel;
float yVel;
float posX;
float posY;

void keyPressed(){  
   if( key == CODED)
   {
if(keyCode == UP){
  yVel=-3;
}
if(keyCode == DOWN){
  yVel=3;
}
if(keyCode == LEFT){
  xVel=-3;
}
if(keyCode == RIGHT){
  xVel=3;
}
   }
}
void draw()
{
xVel=xVel*0.94;
yVel=yVel*0.94;
posX+=xVel;
posY+=yVel;
ellipse(posX,posY,10,10);
}
Re: simple easing motion
Reply #2 - Mar 19th, 2009, 1:55pm
 
You're right that does work. I don't know why I was getting the results I received before. I'll just rewrite this section and see what happens. Thanks.
Re: simple easing motion
Reply #3 - Mar 19th, 2009, 2:29pm
 
No need to thank me, you didn't need my help Wink
Page Index Toggle Pages: 1