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 › Random speed with mousePressed issues
Page Index Toggle Pages: 1
Random speed with mousePressed issues (Read 772 times)
Random speed with mousePressed issues
Feb 3rd, 2010, 4:00pm
 
Hello everyone! I'm pretty new to the forums, and I've just started taking a class in Processing. Just as a heads up, I'm also a newbie to programming in general, so sorry in advance for any stupid questions.

Okay, so I've been given an assignment to write a program where a ball "falls" indefinitely across a screen, and then slows down (gradually, but I can't seem to manage that). Then, when I click my mouse, the ball should be reset with a new random speed that slows down again. I've gotten as far as the "resetting" part, but I can't for the life of me figure out how to start with a new random speed. Any ideas (in terms an idiot could understand)?

Here's the code:

//Variables for ball
float ballX=0;
float ballY=0;

//setup
void setup(){
 size(500,500);
 smooth();
}



//Draw ball
void draw(){
 background(255);
 stroke(175,0,0);
 strokeWeight(3);
 fill(255,0,0);
 ellipse(ballX,ballY,100,100);
 
 //Move ball
 ballX=ballX+3;
 ballY=ballY+3;
 
 //Slow down ball
 if(ballX>width/3){
   ballX=ballX-1;
 }
 else{
   ballX=ballX+3;}
   
   if(ballY>height/3){
     ballY=ballY-1;
   }
   else{
     ballY=ballY+3;
   }
 
}

void mousePressed(){
 ballX=0;
 ballY=0;
 ellipse(ballX,ballY,100,100);
}

P.S. So far, I've been taught 2D shapes, "if else" statements, a few mouse functions, void stuff, and basic variables/arithmetic codes. Basic explanations are very much appreciated.
Re: Random speed with mousePressed issues
Reply #1 - Feb 3rd, 2010, 6:31pm
 
Check the code. There are plenty of comments to keep even the blindest of fish happy...

Quote:
//Variables for ball
float ballX;
float ballY;

// New variables for the ball
float ballXspeed; // How fast the ball is moving in the X direction
float ballYspeed; // How fast the ball is moving in the Y direction

// Even more new variables for the ball
float ballXacceleration; // How much the ball's speed in the X direction is changing.
float ballYacceleration; // How much the ball's speed in the Y direction is changing.

// All of the above variables are givin initial values in setup()'s call to rese
tBall().


//setup
void setup(){
  size(500,500);
  resetBall();
  smooth();
}

//Draw ball
void draw(){

  background(255);
  stroke(175,0,0);
  strokeWeight(3);
  fill(255,0,0);
  ellipse(ballX,ballY,100,100);

  //Move ball
  // ballX=ballX+3;
  // ballY=ballY+3;
  // Instead of a constant 3, the ball will now move
  // depending apon it's X and Y speeds.

  // Like this:
  ballX = ballX + ballXspeed;
  ballY = ballY + ballYspeed;

  //Slow down ball
  // if(ballX>width/3){
  //   ballX=ballX-1;
  // }
  // else{
  //   ballX=ballX+3;}
  //  
  //   if(ballY>height/3){
  //     ballY=ballY-1;
  //   }
  //   else{
  //     ballY=ballY+3;
  //   }
  // Instead of slowing it down in this weird way,
  // the balls speed will now change
  // due to it's acceleration.

  // Like this:
  ballXspeed = ballXspeed + ballXacceleration;
  ballYspeed = ballYspeed + ballYacceleration;
  // moreFunAccelerations(); // Try uncommenting this function call for even more fun!
}

void mousePressed(){
  resetBall();
}

void resetBall(){ // You have to reset more than just the ball's position now.
  ballX = 250;
  ballY = 250;
  ballXspeed = 3;
  ballYspeed = 3;
  ballXacceleration = -0.03; // This slows it down.
  ballYacceleration = -0.03; // This slows it down.
  // ellipse(ballX,ballY,100,100); // You don't need to redraw the ball here.
}

void moreFunAccelerations(){ // Change the ball's acceleration due to it's position.
  if( ballX < 250){
    ballXacceleration = 0.03;
  } 
  else {
    ballXacceleration = -0.03;
  }
  if( ballY < 250){
    ballYacceleration = 0.03;
  } 
  else {
    ballYacceleration = -0.03;
  }
}



Also, welcome! You are doing good so far!  Smiley
Re: Random speed with mousePressed issues
Reply #2 - Feb 3rd, 2010, 6:49pm
 
Awesome! Thanks a ton!  Grin
Page Index Toggle Pages: 1