This
void collision(Ball myBall2) { //correct?
you call this methiod with
- myBall1.collision( myBall2 );
So the actual object you are using
is
myBall1. So
locX, locY is referring to
myBall1 (no need to write myBall1. before).
myBall2 on the other hand is given
as a parameter. So instead of
- float ballSpace = dist(locX, locY, radius, radius); //something wrong here!
you want to get the space between both balls.
Therefore put
- float ballSpace = dist(locX, locY, myBall2.locX, myBall2.locY); //something wrong here!
please. So you are referring to both balls and their radiuses.
since they are not necessarily the same width (same radius) instead of
if (ballSpace <=
radius + radius) // the 2 same radius variables??
better
if (ballSpace <=
radius + myBall2.radius) // the 2 same radius variables??
Remark on stutteringYou see this a lot around here in help and in the forum:
- if (locX >= width-radius || locX <= radius) { //wrong?
- speedX = -speedX;
- locX += speedX;
- }
but since speedX is just switched between minus and plus and vice versa, the following can happen:
ball gets near zero, speedX = -speedX; is calculated (and is plus then, ball moves away from 0 but only a lttle). In the next step the ball is maybe still near 0, so
speedX = -speedX; gets calculated
again and is negativ now, ball moves left towards zero
and stays in that zone near 0 and stutters.
Therefore better:
- if (locX >= width-radius) { // right side
- speedX = - abs(speedX); // abs is always pos and with the negative sign always negative, stable even if executed a few times, no stutter
- } // if
- if (locX <= radius) { // left side
- speedX = abs(speedX); // abs is always pos, so it's stable, no stutter
- } // if
- locX += speedX;
Greetings, Chrisir
