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 & HelpSyntax Questions › Ball get stuck in pad
Page Index Toggle Pages: 1
Ball get stuck in pad (Read 643 times)
Ball get stuck in pad
Dec 19th, 2009, 1:26pm
 
I am having some problems with the ball getting stuck in the pad.
I am a beginner at this and would be very thankful if I could get some help. What have I missed?


Code:

void checkForPad() {
if(ballX + rad > padX - padWidth/2 && ballX - rad < padX + padWidth/2 && ballY + rad > padY - padHeight/2 && ballY - rad < padY + padHeight/2) {
dy=dy*-1;

if(ballX < padX && dx > 0) {
dx=dx*-1;
}
else if(ballX > padX && dx < 0) {
dx=dx*-1;
}
}
}
Re: Ball get stuck in pad
Reply #1 - Dec 19th, 2009, 3:09pm
 
This is a common problem - before multiplying speed by -1 you need to place the object within safe bounds by adjusting the relevant x or y coordinate which may require splitting your conditions up.  e.g.:

Code:
if(ballX < padX){
ballX = padX;
dx *= -1; // shortcut for 'dx = dx * -1'
}

Re: Ball get stuck in pad
Reply #2 - Dec 21st, 2009, 3:22pm
 
Thanks for your help, I think I know what you mean, but your example doesn't help me very much I'm afraid. I've tried something similar before and I have tried other things but I can't get it right..
Re: Ball get stuck in pad
Reply #3 - Dec 21st, 2009, 4:11pm
 
It's actually a little more tricky than I first thought since you need to know which direction the ball is coming from and that can get tricky if the ball is travelling at high speed, especially if your rectangle is small.  Here's a function that bounces a ball off a rectangle:

Code:
// Assuming a rectangle using rectMode(CORNER)
// Note that this will fail if ballSpeed is high enough to place the ball beyond the far boundary of the rect on collision
void rectBounce(Ball ball) {
 int rectRight = rectX + rectWidth;
 int rectBottom = rectY + rectHeight;
 // check that the ball is inside the rectangle
 if(ball.x > rectX-ball.radius && ball.x<rectRight+ball.radius && ball.y > rectY-ball.radius && ball.y < rectBottom+ball.radius){
   // If it is check which boundary it has hit
   // Note we don't use the radius offset here as the ball is already inside the rectangle
   if(ball.x < rectX) {
     ball.x = rectX-ball.radius;// place at appropriate side
     ball.vx *= -1;
   }
   else if(ball.x > rectRight) {
     ball.x = rectRight+ball.radius;// place at appropriate side
     ball.vx *= -1;
   }
   if(ball.y < rectY){
     ball.y = rectY-ball.radius;
     ball.vy *= -1;
   }
   else if (ball.y > rectBottom) {
     ball.y = rectBottom+ball.radius;
     ball.vy *= -1;
   }
 }
}


This takes a ball object as a parameter, but it should be simple enough to adapt and assumes variables have been set to define the rectangle (or 'bat' in your case) dimensions.

Mind you I suspect this is a FAQ and you may find better solutions if you search; either here or on the wider net.
Page Index Toggle Pages: 1