Loading...
Logo
Processing Forum

smooth motion

in Programming Questions  •  4 months ago  
Hi,

I'm trying to make a movement system that causes an object such as a square to speed up from a stop while a key is being held down, and then coast to a stop when they key is released. I need help with the coding. 

This is what I have so far:

Copy code
  1. car[] cars;
  2. int x;

  3. void setup();
  4. size(600,600);
  5. car = new car[10];
  6. for(int i=0;i<cars.length;i++){
  7. cars[i] = new car(startX[i],startY[i]);
  8. }
  9. x=0;
  10. }

  11. void draw(){
  12. background(125);
  13. {
  14.     cars[i].drawcar();
  15.   }
  16. }

  17. void keyPressed(){ 
  18.   if( key >= '0' && key <= '9'){
  19.     x = key - '0';
  20.     return;
  21.   }
  22.   if(key == CODED)
  23.   {
  24.     if (keyCode == UP)
  25.     {
  26.       cars[x].moveUP();
  27.     }
  28.     else if (keyCode == DOWN)
  29.     {
  30.       cars[x].moveDOWN();
  31.     }
  32.     else if (keyCode == LEFT)
  33.     {
  34.       cars[x].moveLEFT();
  35.     }
  36.     else if (keyCode == RIGHT)
  37.     {
  38.       cars[x].moveRIGHT();
  39.     }
  40. }

Replies(6)

Re: smooth motion

4 months ago
You could take a look at the ijeomamotion library. It has lots of functionality to do smooth motions such as slowing down or speeding up.

Re: smooth motion

4 months ago
thanks for your response.
any other options? 

Re: smooth motion

4 months ago
Remark:
car = new car[10];
A bit ambiguous (for our eyes, not for the compiler!), no?
That's why we generally start class names with a capital letter:
Car[] cars = new Car[10];
(mmm, there was a typo in the quoted line, no?)

Perhaps you look for easing algorithms, if I understood you correctly. Have you tried to search this word from Processing.org?

Re: Re: smooth motion

4 months ago
PhiLho,

I rewrote the code:  The ball moves fine with the directional arrows...however when I release the directional arrow I want the ball to slightly ease in...not a sudden stop movement. I searched "easing algorithms" with no results. 

Copy code
  1. int ballX;
  2. int ballY;
  3. int radius;
  4. int ballSpeed;

  5. void setup()
  6. {
  7.   size(600, 600);
  8.   background(255, 255, 255);
  9.   smooth();
  10.   frameRate(60);

  11.   ballX = width/2;
  12.   ballY = height/2;
  13.   radius = 25;
  14.   ballSpeed = 10;
  15. }

  16. void draw()
  17. {
  18.   fill(255, 255, 255, 50);
  19.   noStroke();
  20.   rect(0, 0, width, height);

  21.   fill(255, 0, 0);
  22.   ellipse(ballX, ballY, radius*2, radius*2);
  23. }


  24. void keyPressed()
  25. {
  26.   if ( (keyCode == LEFT) && (ballX > radius) )
  27.   {
  28.     ballX = ballX - ballSpeed;
  29.   }

  30.   if ( (keyCode == RIGHT) && (ballX < width-radius) )
  31.   {
  32.     ballX = ballX + ballSpeed;
  33.   }

  34.   if ( (keyCode == UP) && (ballY > radius) )
  35.   {
  36.     ballY = ballY - ballSpeed;
  37.   }

  38.   if ( (keyCode == DOWN) && (ballY < height-radius) )
  39.   {
  40.     ballY = ballY + ballSpeed;
  41.   }
  42. }

Re: Re: smooth motion

4 months ago
thanks!!