to make the shaky ball move around faster

edited October 2013 in Questions about Code

hi guys, does anyone have any idea on how to make the shaky ball move around faster. thankyou

float posX, posY;
float speedX, speedY;
boolean paused = false;

void setup()
{
  size(640, 480);
  frameRate(100);

  // initialise your variables to random values

  // the position can be anywhere on screen
  posX = random(0, width);
  posY = random(0, height); 

  // speedX and speedY can be up to 2 pixels per frame
  // and in either direction
  speedX = random(-2, 2);
  speedY = random(-2, 2);
}

void draw()

// redraw the background so you blank the screen
{
  background(255);
  fill(0, 255, 20);


  fill(0, 80); // The circles fades away.
  rect(0, 0, width, height);
   speedX = random(-1, 1);
  speedY = random(-1, 1);// 
  fill(0, 215, 255); 
  ellipse( random(width), random(height), random(50), random(50));


  if (!paused)


  { do
    {
      paused = !paused;
    } 
    while (paused=false);
    posX += speedX;
    posY += speedY;


    // update the position based on the speed
    posX += speedX;
    posY += speedY;
     //ellipse( random(width), random(height), random(50), random(50));

    // draw a ball
    ellipse(posX, posY, 40, 40);

    if (posX < 0 || posX > width)
    {
      speedX = -speedX;
    }
    if (posY < 0 || posY > height)
    {
      speedY = -speedY;



    }
  }
}



void keyPressed()
{
  if (key == 'p')
    paused = !paused;
}

Answers

  • could it be something to do with the two variables called speedX and speedY on lines 32 and 33?

  • _vk_vk
    edited October 2013

    Please, format your code, I think you missed a blank line between the text and first line of code...

    http://forum.processing.org/two/discussion/32/how-to-format-text-and-code-on-the-new-forum#Item_20

    about the code: Maybe you don't want to change the speed(x and y) inside draw by calling random 60 times per sec. Try comment that out, is this that you are looking for? Also there is some others notes, but I'm not sure cause code like this get comments crazy and I don't know if I'm seeing what you wrote.

  • _vk_vk
    edited October 2013

    Ahh, much better now :) As koogs said line 32 and 33. You may do like

    speedX += random(-1, 1);
    speedY += random(-1, 1);
    

    To alter the speed instead of just swap it. Kind of an acceleration... Also the call to background on line 26 covers the effect of trail attempted in line 31. You might want to get rid of it, the rect already clears the screen for you, try with a lower alpha value, like 40 for instance.

Sign In or Register to comment.