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 › How to bounce off paddle
Page Index Toggle Pages: 1
How to bounce off paddle? (Read 992 times)
How to bounce off paddle?
Mar 12th, 2010, 2:29pm
 
I'm trying to create a simple pong program but can't figure out how to make it bounce off the paddle.

I figured out how to make them bounce off of the 3 walls and then disappear off of the right hand wall but I'm at a complete loss for the paddle =\ can anyone help?
Re: How to bounce off paddle?
Reply #1 - Mar 12th, 2010, 2:58pm
 
how do you make them bounce of the wall.

something like if(ball.y>height || ball.y<0) direction*-1; ?

if so do the same for the paddle.
as your paddle always has the same x coordinates do a check like this

if(ball.x<paddle.x && dist(paddle.y,ball.y)<paddle.size/2)direction*-1;

this code wont work, as it depends on how your code looks like, but i hope you get the idea.

Re: How to bounce off paddle?
Reply #2 - Mar 12th, 2010, 4:16pm
 
Isn't dist() only used for mice?  Here is my code, I have basically everything set except the  bouncing off the paddle.
Code:


final int WIDTH = 400, HEIGHT = 400;
final int RAD = 5;
final int PAD_HEIGHT = 40, PAD_WIDTH = 5;
final int PAD_X = 385;
final int PAD_SPEED = 10;
final int PU = 38;
final int DN = 40;
final int MAX_SPEED = 5;

final color WHITE = color(255);
final color BLACK = color(0);
final color RED = color(255,0,0);
final color GREEN = color(0,255,0);
final color BLUE = color(0,0,255);

int ballx = 200;
int bally = 200;
int pady = 180;
int speedx = int(random(1,MAX_SPEED));
int speedy = int(random(1,MAX_SPEED));
int points = 0;


void setup(){
 size(WIDTH,HEIGHT);
 ellipseMode(RADIUS);
 smooth();
}

void draw(){

background(WHITE);


 bally = bally - speedy;
 ballx = ballx - speedx;
 
 //Bounce off walls
 if (ballx < 5){
   speedx *= - 1;  
}

 if (bally > WIDTH || bally < 5){
   speedy *= - 1;
   
 }
 
    Bounce off Paddle
if (ballx >= 350  && (pady >= bally && pady + 40 <= bally)){
  speedy *= - 1;
  points += 1;
}
 
 //Print Game Over
if (ballx == WIDTH || ballx == HEIGHT)
{
println("Game Over, Guy.");
println(points);
}

 fill(RED);
 ellipse(ballx,bally,RAD,RAD);
 noStroke();
 fill (BLUE);
 rect(PAD_X,pady,PAD_WIDTH,PAD_HEIGHT);
 

}

void keyPressed(){
   if(keyCode == PU){
     pady -= PAD_SPEED;
   }
   if(keyCode == DN){
     pady += PAD_SPEED;
   }
   
   //Stop paddle
   if(pady == 370){
     pady -= PAD_SPEED;
   }
    if(pady <= -10){
     pady += PAD_SPEED;
   }
}

Re: How to bounce off paddle?
Reply #3 - Mar 12th, 2010, 4:46pm
 
dist is for calculating the distance between two points, could be mouseX,mouseY or anything else

but the code was like i said...
you could have done that by yourself.

 //Bounce off pad
 if (ballx > PAD_X && dist(ballx,bally,PAD_X,pady)<PAD_HEIGHT/2){
   speedx *= - 1;  
}

Re: How to bounce off paddle?
Reply #4 - Mar 14th, 2010, 5:41am
 
Here is a really basic sketch but it has potential

http://www.openprocessing.org/visuals/?visualID=8228


/Implements the technique of optikorakel
//openprocessing.org/visuals/?visualID=7554

From here you can modify when the ball collides but just checking to see what colour it comes into contact and then change direction based on where on the ellipse it hits.

Easy Ball collision code or even for some type of water-gun game where you can check and see what colour combination you can come up with.

Original sketch
http://openprocessing.org/visuals/?visualID=8227

//instructables.com/id/shaped-buttons-with-Processing/

Here is the condensed code but it is faster when you have the BNC (bounce procedure) as a separate class/sketch/tab

Code:

//Owaun Scantlebury
//Implements the technique of optikorakel
//openprocessing.org/visuals/?visualID=7554
//instructables.com/id/shaped-buttons-with-Processing/

public boolean forward = true;
public boolean up = false;
public int px=0;
public int py =0;
public int ly=0;
public int lx=0;


public color[] A,B,mix;
PGraphics ulayer;
void setup(){
 mix = new color[8];
 mix[0]=color(233,23,23);
 mix[1]=color(5,55,35);
 mix[2]=color(255,245,0);
 mix[3]=color(133,33,123);
 mix[4]=color(9,125,125);
 mix[5]=color(145,245,0);
 mix[6]=color(104,10,10);
 mix[7]=color(133,25,225);
 size(255,255,JAVA2D);
 frameRate(3000);
 loadPixels();
 A= pixels;

 ulayer = createGraphics(width,height,P2D);
 ulayer.loadPixels();

 B = ulayer.pixels;

 ulayer.smooth();
 makemask();
 smooth();
 rectMode(CENTER);
 ellipseMode(CENTER);
}

void draw(){
 bounce();

 background(-1);

 //line(width/2,height/2,width-50,50);
 ////line(width-50,50,50,50);
 //line(width/2,height/2,50,50);
 //line(width/2,height/2,50,height-50);
 //line(width/2,height/2,width-50,height-50);
 //line(width/2,height/2,50,height/2);
 //line(width/2,height/2,width-50,height/2);
 //makepoint(width/2,height/2,25, 25,mix[0]);


 line(px,py,width-50,50);
 line(px,py,50,50);
 line(px,py,50,height-50);
 line(px,py,width-50,height-50);
 line(px,py,50,height/2);
 line(px,py,width-50,height/2);
 makepoint(px,py,25, 25,mix[0]);

 makepoint(width-50,50,25, 25,mix[1]);

 makepoint(50,50,25, 25,mix[2]);
 makepoint(50,height-50,25, 25,mix[3]);
 makepoint(width-50,height-50,25, 25,mix[4]);
 makepoint(50,height/2,25, 25,mix[5]);
 makepoint(width-50,height/2,25, 25,mix[6]);
 if (mousePressed) {
   background(-1);
   image(ulayer,0,0);

 }
}
void makemask(){
 ulayer.beginDraw();
 ulayer.rectMode(CENTER);
 ulayer.ellipseMode(CENTER);

 makepoint2(width/2,height/2,25, 25,mix[0]);

 makepoint2(width-50,50,25, 25,mix[1]);

 makepoint2(50,50,25, 25,mix[2]);
 makepoint2(50,height-50,25, 25,mix[3]);
 makepoint2(width-50,height-50,25, 25,mix[4]);
 makepoint2(50,height/2,25, 25,mix[5]);
 makepoint2(width-50,height/2,25, 25,mix[6]);
 ulayer.endDraw();
}

void makepoint(int x,int y,int h, int w,color f){
 //noFill();
 fill(-1);
 stroke(1);
 if (ulayer.get(mouseX,mouseY)==f){
   fill(f);
   rect(x,y,h,w);
 }

 ellipse(x,y,h,w);
}

void makepoint2(int x,int y,int h, int w,color f){

 ulayer.fill(f);
 ulayer.noStroke();

 ulayer.ellipse(x,y,h,w);
}


void keyPressed(){
if (!online)save("marker.png");
}


public void bounce(){
boolean rnd = true;
int by = 3;
int lo = 1;
int hi = 5;
 
if (forward){
 if (rnd)px+=random(lo,hi);
  if (!rnd){
   px+=by;
  }
}


  if (!forward){
    if(rnd)px-=random(lo,hi);  
    if (!rnd){
     px-=by;
    }
    }
 
  if (up){
   if (rnd)py-=random(lo,hi*2);
    if (!rnd){
      py+=by;
    }
    }
   
    if (!up){
     if(rnd)py+=random(lo,hi*2);
    if (!rnd){
      py-=by;
    }  
     
    }
 
 
  if (px>width)
    {
     
      forward = false;
     
    }
   
    if (get(px,py)==mix[0]){
      forward = false;
    }
  if (px<0)    forward = true;
  if (py>height)up     = true;
  if (py<0     )up     = false;
 }  
 

 
 
Page Index Toggle Pages: 1