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 › anyone got a code for the bouncing ball program
Page Index Toggle Pages: 1
anyone got a code for the bouncing ball program ? (Read 2717 times)
anyone got a code for the bouncing ball program ?
Apr 13th, 2010, 3:54am
 
Quote:
So basically what I want is when the user presses and holds
down the mouse button, a black circle of diameter 30 pixels centered on
the mouse position should be drawn. The circle should follow the mouse while the mouse button remains
pressed.
When the user lets go of the mouse button, the circle should continue moving in the
direction that the mouse is moving. Also, the speed at which the circle moves should be
determined by how fast the mouse was moving when the user let go of the button. When
flying of its own accord, the circle should bounce in a realistic way off all four sides of the
window.


I'm having hard times implementing this program...
Re: anyone got a code for the bouncing ball program ?
Reply #1 - Apr 13th, 2010, 4:06am
 
I've managed to get the ball moving across the screen bouncing off of the walls...
I'm having problems with mousepressed and mousereleased functions...

any help ?

this is what my code look like right now
Quote:
//Set up necessary variables.
int x=10; //x-coordinate of ellipse moving around sketch edge.
int y=10; //y-coordinate of ellipse moving around sketch edge.
int cx=100; //x-coordinate of bouncing ball
int cy=10; //y-coordinate of bouncing ball
int r=30; //radius of bouncing ball
int vx=2; //bouncing ball's direction on the x-axis
int vy=3; //bouncing ball's direction on the y-axis

//Set up variable for speed of ellipse moving around sketch edge.
int speed=1;

//Allows ellipse moving around sketch edge to change direction depending on position.
int state=0;

//Set up size of and smooth out sketch.
void setup(){
size(500, 300);
smooth();
}

//Set background to white to hide previous draw commands.
void draw(){
background(255);

noStroke();
//Fill bouncing ball as black.
fill(0);
//Change ellipse mode.
ellipseMode(CENTER);
//Draw bouncing ball with variables.
ellipse(cx, cy, r, r);

//Set cy variable to move across the sketch
cy = cy + vy;

//Set condition to bounce ball once it reaches sketch edge.
if((cy>height-40)||(cy<0)){
  vy = vy * -1;
}
//Set cx variable to bouncing ball move across the sketch.
cx = cx + vx;

//Set condition to bounce ball once it reaches sketch edge.
if((cx > width - 10)||(cx < 0)){
  vx = vx * -1;
}

}

Re: anyone got a code for the bouncing ball program ?
Reply #2 - Apr 13th, 2010, 4:30am
 
This is a simple example that I used myself as first steps in processing. It does almost what you need and might help you with the mouse event stuff.

Andreas

Quote:
PApplet app = this;

Ball ball;
PImage bg;

void setup()
{
  size(800, 600);
  smooth();
  //bg = loadImage("Tennis_Court_adf.jpg");
  ball = new Ball(300, 0, .25);


void draw()
{
  ball.move();
  if (!ball.isAtRest())
  {
    //background(bg);
    background(255);
    ball.display();
  }
}



Quote:
public class Ball
{    
  float x;
  float y;
  float xspeed;
  float yspeed;
  float phi;
  float phispeed;
  float gravity;
  float friction;
  float ballsize;
  float invSize;
  boolean rest;
  boolean mousedown;
  int ball_width;
  int ball_height;

  PImage ballshape;

  float xmin;
  float ymin;
  float xmax;
  float ymax;

  float phiscale;


  Ball(float tempX, float tempY, float tempSize)
  {
    rest = false;
    x = tempX;
    y = tempY;
    ballsize = tempSize;
    invSize=1;
    xspeed = 0;
    yspeed = 0;
    phi = 0;
    phispeed = 0;
    gravity = 1;
    friction = 0.99;
    /*
    ballshape = loadImage("Tennis_ball_adf.png");
     ballshape.resize(round(ballshape.width*ballsize), round(ballshape.height*ba
llsize));

     ball_width=ballshape.width;
     ball_height=ballshape.height;
     */
    ball_width=round(136*ballsize);
    ball_height=round(136*ballsize);
    xmin = 0;
    ymin = 0;
    xmax = width-ball_width;
    ymax = height-ball_height;
    phiscale =2.0/(ball_height);

    app.registerMouseEvent(this);

  }

  boolean isAtRest()
  {
    return rest;
  }

  void reset(float newX, float newY, float newXspeed, float newYspeed)
  {
    x=newX;
    y=newY;  
    xspeed=newXspeed;
    yspeed=newYspeed;  
  }

  void move()
  {
    float reverseSpeed = -0.83;
    float slowSpeed = 0.985;
    float bounce = 0;

    if (!rest)
    {
      if (mousedown) {
        this.reset(mouseX,mouseY,(mouseX-pmouseX),(mouseY-pmouseY));
      }
      yspeed=friction*yspeed+gravity;
      phispeed=friction*phispeed;
      y=y+yspeed;
      x=x+xspeed;
      phi=phi+phispeed;

      if (y > ymax)
      {
        yspeed = yspeed * reverseSpeed;
        xspeed = xspeed * slowSpeed;
        y = ymax-bounce;

        phispeed=xspeed*phiscale;

        // Set speed here =======================================================
        if( (abs(yspeed) < 2) && (abs(xspeed) < 0.1) )
        {
          rest=true;
        }
      }

      if (y < ymin)
      {
        yspeed = yspeed * reverseSpeed;
        xspeed = xspeed * slowSpeed;

        y = ymin+bounce;
      }
      if (x > xmax)
      {
        xspeed = xspeed * reverseSpeed;
        yspeed = yspeed * slowSpeed;
        x = xmax-bounce;
      }
      if (x < xmin)
      {
        xspeed = xspeed * reverseSpeed;
        yspeed = yspeed * slowSpeed;
        x = xmin+bounce;
      }
    }
  }

  void mouseEvent(MouseEvent event)
  {
    switch (event.getID())
    {
    case MouseEvent.MOUSE_PRESSED:
    case MouseEvent.MOUSE_DRAGGED:
      rest = false;
      mousedown = true;  
      break;
    case MouseEvent.MOUSE_RELEASED:
      mousedown = false;  
      break;
    }
  }

  void display()
  {    
    float offsetX=invSize*(x)+ball_width/2;
    float offsetY=invSize*(y)+ball_height/2;
    translate(offsetX, offsetY);
    rotate(phi);
    translate(-offsetX, -offsetY);
    //image(ballshape, x, y);    
    ellipse(x+ball_width/2,y+ball_height/2,ball_width,ball_height);
    line(x+ball_width/2,y+ball_height/2,x+0.8*ball_width,y+0.8*ball_height);
  }
}
Re: anyone got a code for the bouncing ball program ?
Reply #3 - Apr 13th, 2010, 4:37am
 
thanks man a lot, but I'm just a beginner...and your code looks way too complicated for me Cheesy

we haven't learnt this kind of stuff yet, especially applets ...

is there an easier way in doing this ? like something for beginners I guess...
Re: anyone got a code for the bouncing ball program ?
Reply #4 - Apr 13th, 2010, 7:47am
 
I don't think this example is complicated. Unfortunatly it lacks comments and explantions which makes it harder to understand.

I guess you are just a bit daunted by the use of a class and several methods (functions) rather than having it all in one block of code. In that case I would recommend that you start with some general processing tutorial to get familiar with this style of programming. It will help a lot - even the absolute beginner.
Re: anyone got a code for the bouncing ball program ?
Reply #5 - Apr 13th, 2010, 8:28am
 
This may help:

Quote:
float ballX, ballY, xVel, yVel, ballDiam=30;

void setup(){
  size(640,480);
  strokeWeight(2);
}

void draw(){
  background(150);
  if (mousePressed){
    // if the mouse is held down, the ball location
    // should be the same as the mouse location.
    ballX=mouseX;
    ballY=mouseY;
  }
  else{
    // otherwise it should be traveling
    // according to its velocity.
    ballX += xVel;
    ballY += yVel;
  }
  // draw the ball:
  ellipse(ballX, ballY, ballDiam, ballDiam);
  // here is the bounce part:  if the ball gets close to
  // any edge of the window, reverse its X or Y velocity.
  // in this case, "too close" is "past the ball's radius."
  if ((ballX<ballDiam/2) || (ballX>width-ballDiam/2)) xVel = -xVel;
  if ((ballY<ballDiam/2) || (ballY>height-ballDiam/2)) yVel = -yVel;
}

void mouseReleased(){
  // when the mouse is released, "throw" the ball
  // by setting its x and y velocities.
  xVel = mouseX-pmouseX;
  yVel = mouseY-pmouseY;
}
Re: anyone got a code for the bouncing ball program ?
Reply #6 - Apr 13th, 2010, 9:33am
 
wow man, this looks a lot easier, thank you so much...my last question
if I want the ball eventually to stop ? do I need to use "gravity" ?
Re: anyone got a code for the bouncing ball program ?
Reply #7 - Apr 13th, 2010, 10:10am
 
No - you'd use friction for that.  Gravity would just pull it down towards the 'ground' (though there's nothing to stop gravity going up or sideways if you want).

Friction is easy enough: you need a value just below 1 (e.g. 0.99) and multiply the velocities before adding them to the position - so each frame the velocity is made a little bit smaller.  Gravity would again be a fairly small number that you'd normally add to the yVel, again before applying to the y position.  So you might have the following in the movement code, where you've already defined and set values for 'friction' and 'gravity':

Code:
// snip...
else{
   // apply gravity and friction to the velocities:
   yVel += gravity;

   xVel *= friction;
   yVel *= friction;

   // otherwise it should be traveling
   // according to its velocity.
   ballX += xVel;
   ballY += yVel;
 }
Re: anyone got a code for the bouncing ball program ?
Reply #8 - Apr 19th, 2010, 4:41am
 
ok. I've figured out how to make a ball stop, but why it keeps bouncing even when it stops ?
by the way, don't pay attention on ballImage thing, I was just playing around, trying to import an image in my code...

Quote:
int width = 500;
int height = 300;

int buttonx = 400;
int buttony = 270;
int buttonwidth = 150;
int buttonheight = 30;


int count = 0;
int num = 0;
int sqrsize = 20;
int posx = 0;

float ballX, ballY, xVel, yVel, gravity = 0.1, friction = 0.99, ballDiam = 30;

PImage ballImage;

void setup(){
 size(width, height);
 textFont (createFont ("Georgia", 24));
 strokeWeight(2);
 ballImage = loadImage ("soccer-ball-1.png");
 ballImage.resize (50,0);
 ballX = width - ballImage.width;
 ballY = height - ballImage.height;
}

void draw(){
 background(255);
 rect (buttonx, buttony, buttonwidth, buttonheight);
 fill (0);
 text (count, 450, 30);
 if (mousePressed){
   // if the mouse is held down, the ball location
   // should be the same as the mouse location.
   ballX = mouseX - ballImage.width / 2;
   ballY = mouseY - ballImage.height / 2;
 }
 else{
   // otherwise it should be traveling
   // according to its velocity.
   ballX += xVel;
   ballY += yVel;
   
 }
 // draw the ball:
 fill (0);
 image (ballImage, ballX, ballY);
 // here is the bounce part:  if the ball gets close to
 // any edge of the window, reverse its X or Y velocity.
 // in this case, "too close" is "past the ball's radius."
 if ((ballX < ballDiam / 2) || (ballX > width - ballDiam / 2)) xVel = -xVel;
 if ((ballY < ballDiam / 2) || (ballY > height - ballDiam / 2)) yVel = -yVel;
 else {
  yVel += gravity;
  xVel *= friction;
  yVel *= friction;
  if ((ballX >= buttonx) && (ballX < buttonx + buttonwidth) &&
  (ballY >= buttony) && (ballY < buttony + buttonheight)) {
    count = count + 1;
  } else {
    num = 0;
    while (num < count) {
      rect (posx, sqrsize * num, sqrsize, sqrsize);
      num = num + 1;
    }
    posx = posx + sqrsize;
  }
 }
}
void mouseReleased(){
 // when the mouse is released, "throw" the ball
 // by setting its x and y velocities.
 xVel = mouseX - pmouseX;
 yVel = mouseY - pmouseY;
 
 }
 
Page Index Toggle Pages: 1