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 › Bouncing off of diagonal surfaces
Page Index Toggle Pages: 1
Bouncing off of diagonal surfaces (Read 713 times)
Bouncing off of diagonal surfaces
May 2nd, 2010, 8:58pm
 
I'm working on a sketch in which balls bounce off of ledges that the user can move around. The problem I'm having is how to check if the ball is hitting a ledge when the ledge is diagonal. I found a formula for the distance between a point a line and tried to implement it, but now the balls simply bounce when they reach the highest y position of any ledge (regardless of whether or not they're near the ledge)

The formula I'm using right now is
Code:

int A = ballX - ledgeX1;
int B = ballY - ledgeY1;
int C = ledgeX2 - ledgeX1;
int D = ledgeY2 - ledgeY1;

float dist = abs(A * D - C * B) / sqrt(C * C + D * D);

if(dist <= ballRadius){
ball.bounce();
}


Does anyone see what I'm doing wrong or know a better way of doing this?
Re: Bouncing off of diagonal surfaces
Reply #1 - May 2nd, 2010, 10:47pm
 
Posting a block of working code is extremely useful for us helpful people.
Seriously. Post something that we can copy, paste, run, understand, and edit.
Re: Bouncing off of diagonal surfaces
Reply #2 - May 2nd, 2010, 11:54pm
 
Page Index Toggle Pages: 1