How to let my WhiteBall find the GreenBall?

I want to make an IF statement to add a point if WhiteBall hits GreenBall.

I want to write something similar to

if (WhiteBall.position.x+WhiteBall.radius OR WhiteBall.position.x-WhiteBall.radius== GreenBall.position.x+GreenBall.radius OR GreenBall.position.x-GreenBall.radius){

cout <<“hit!”<<endl;


Right now this doesn’t seem to work.
I would appreciate it if anyone could help me with this.

Ive tried to make the x pos of WhiteBall meet the x pos of GreenBall.
But apparently its not making a rounded number on the greenball. Hence why it’ll never meet the x pos of the whiteball.

(512 = xpos of the WhiteBall, 512.47 = xpos of the GreenBall)

Is there a way to make the x pos of the green ball rounded to a solid number?
Right now it’s an ofPoint. If I change it to INT it gives me an error.

Are you looking only for hit test, like collision detection between two objects (WhiteBall and GreenBall in your case)? Or do you need the exact x,y center-points of these circle shapes?

Cos in the case of the first, there’s an easy simple way to do it…
You can wrap your ball objects into bounding boxes utilizing ofRectangle class.
ofRectangle has this convenient boolean intersect function -!show_intersects
where you test one object to another… a rather traditional approach to hit-test collisions as collision detection with complicated shapes is a really tricky one…

Of course, this has some serious drawbacks, like if the balls hit each other’s corners of the bounding boxes, you’re gonna have a hit-test when no visual hit-test should have occured, obviously… :stuck_out_tongue:
Well then, it has to be done using the x,y coordinates in combination with radius from the center-points…

Yeah, C++ is type-sensitive, but I guess you could do with ofVec3f or ofVec2f… ofPoint is actually a typedef to ofVec3f, so you should be able to access its members using the dot syntax like:

// position of the green ball
ofPoint greenBallPos;
greenBallPos.x ... ; // we are accessing it's horizontal position
greenBallPos.y ... ; // we are accessing it's vertical position

If ofPoint is restricted for 3d only (I don’t think so, I remember using it as 2-dimensional point like ofVec2f), then I recommend using ofVec2f instead.
You can then typecast its .x and .y members into (int) as needed I guess…

Hi @Rii
Although the logic of your if statement makes sense its syntax is wrong.
In C++ you use the || as the OR operator and you do not say compare this value or this other value against this value or this other one, as I can guess you mean. What you say is is this boolean expression (that resolves into true or false) true OR is this other boolean expresion true, and so on.

Then, when testing collisions it is quite hard that you’ll get the exact same number so usually you use the < or > (less than or greater than) operators.
In the case of your problem, what’s being suggested by @mr.Bitmap is a sensible choice but it has some drawbacks as he also states.
Instead you can do something a lot cleaner and easier but with a higher computational expense.
you simply measure the distance between center and compare against the sum of the radius of the balls. So, if the balls position is an ofPoint you can say

if(WhiteBall.position.distance(GreenBall.position) <= WhiteBall.radius + GreenBall.radius){
    cout << "hit!" << endl;

if you have just two balls it should not be a problem in terms of computational expense to call this on each frame, but it you have several more it might become noticeable. The distance function computes a square root, which is computationally expensive, so in order to avoid it you can use the following, which should give you the exact same results.

float r = WhiteBall.radius + GreenBall.radius;
if(WhiteBall.position.squareDistance(GreenBall.position) <= r*r){
    cout << "hit!" << endl;

hope this helps.

1 Like