collision detection

Hello,
my program doesn't have any errors but for some reason I am stuck on this collision detection part of it:

void Ball::collisions() {
if (BallLoc.y >= ofGetHeight()) {

velocity *= -1;
}

if (BallLoc.y < 0) {

velocity *= -1;

}

distance = playerpos.distance(BallLoc); //from hereon doesn't work
if(distance< 40)
{

velocity *= -1;
ofDrawRectangle(40, 40, 40, 40);

}

}
what does it do, and what should it do?
is distance() returning the correct value?
The distance was returning various numbers between 200-700 even when the paddle was next to the ball.
I even changed the code so it detects if x>required/x<required and it only detects collision on the first frame (as soon as the sketch is played the detection for those coordinates are detected).
ok so we need to see the distance function if you are still having issues.
Well I wrote numerous ones, before they were in the 'ball' class and inheriting the 'player' class.

Now I'm only trying to detect collision between the 2 in the ofApp.cpp (openframeworks):

void ofApp::draw(){
background.draw(locX, locY, width, height);
player_1.draw();
ball_1.draw();
ball_1.update();
ball_1.collisions();
player_1.collision();
//ball_1.collisions();

if (ball_1.BallLoc.x > player_1.loc.x && ball_1.BallLoc.x < (player_1.loc.x+player_1.width)) {

ball_1.reverse();
}

It still only works on the first frame, so on the first frame if they are colliding it will constantly reverse (just jitters at the top).
for now, would the standard distance formula work instead?

like,
dist = sqrt( (x-x1)*(x-x1) + ( y-y1)*(y-y1) );

if (dist too small, collide)
{
....

there is usually a more optimal form, but I tend to follow the KISS method, get it working, and tweak later.

distance = sqrt( ((x2-x1)^2) + (y2-y1)^2 )

So you're needing to know if the boundary of a circle is touching or otherwise past the boundary of a "paddle". You can think of the paddle as having two interactive lines, a top line and a bottom line. If a point along one of these lines is inside the circle then a collision has taken place.

How far have you gotten in math classes at school?
http://stackoverflow.com/questions/1073336/circle-line-segment-collision-detection-algorithm
Thank you but I've already done that euclidean distance formula and it makes no difference to my program as to what I'm doing now. My problem is that only on the first frame if there's collision the 'velocity' will reverse and the ball jitters at the top.
if(distance< 40)
once this is true, if the velocity is not huge, it will just sit there.

dist = 20;
reverse velocity, move a little, but dist = 23, so its still true, revers, dist is 19, still true, reverse, dist is 25, reverse, 15, ... whatever... it will oscillate here.

you need to change the condition once its met, to a new condition... what will it hit after it rebounds (the velocity flip?).

That's it thank you!

May I ask why you help out on the forums? It's a very nice thing to take time out of your day to help.
I come here to learn too. I learned c++ way before the STL existed, and put it aside a few years ago, trying to refresh my skills. Ive learned tons already. Its a give and take.

Topic archived. No new replies allowed.