Creating a ball every time I press "b" KEY. The code hangs when you create the second ball. Where is the error?

Dear openFrameworkers, I have a problem in my program. The code implements a “Ball” class that creates a new ball every time I press the “b” key, if there is more than one ball it evaluates the distance between all of them and in the case of detecting a collision the balls involved bounce.

Running the code when I first press the “b” KEY one ball is created and seen on screen, so far so good. When I press the “b” key a second time the code hangs up and everything stops. What’s the matter?

Attached to file .zip the code.

I use Visual Studio 2017 and W10 64 bits.

Thanks a lot

Best regards

Creando una pelota cada vez que presiono la tecla “b”. El código se cuelga al crear la segunda pelota. Donde esta el error?

Estimados openFrameworkers, tengo un problema en mi programa. El código implementa una clase “Ball” que crea una nueva pelota cada vez que presiono la tecla “b”, si hay mas de una pelota evalua la distancia entre todas ellas y en el caso de detectar una colisión las pelotas involucradas rebotan.

Corriendo el código al presionar por primera vez la tecla “b” una pelota se crea y se ve en pantalla, hasta aquí todo bien. Al presionar por segunda vez la tecla “b” el código se cuelga y todo se detiene. ¿Cuál es el problema?

Adjunto en archivo .zip el código.

Muchas Gracias

Saludos
src.rar (2,1 KB)

for (int tu = yo + 1; tu < pelotas.size() - 1; tu++)

this seems to be causing an infinite loop
if it is the latest in the vector how can it be “yo + 1” and yet be less than pelotas.size() ?

1 Like

Hi,
please, next time, instead of uploading a zip with the source code please paste it directly in the text box and format it as code, by selecting it and pressing the </> button. That way it is easier to review.

I think that the problem is in your update function. There is no need to do all those if and elses. you can simply run one for loop inside of another one, and simply check if the indices are not the same in order to check the distance. you can also add a bool to the Ball class which you set to false for all balls at the begining of update. then you set that to true when you check the distance and only check the distance if it is false. this way you avoid checking twice each pair.

I think this line
if (yo = 0)
should have “==” too for comparsion

for (int yo = 0; yo < pelotas.size(); yo++)
	{
		for (int tu = 0; tu < pelotas.size(); tu++)// Others Ball have index 1 to pelotas.size()
		{
			if(tu != yo){
				medirDistancia(yo, tu); // evaluate distance between Me and You	
			}
		}
	}
2 Likes

I really like roymacdonald’s suggestion for re-writing the loops.

Also, the comparisons in the else-if statements in ofApp::update() has an issue. If you keep the else-if statments, this one:

else if (yo > 0 && yo < pelotas.size())

should probably become:

else if (yo > 0 && yo < pelotas.size() - 1)

which would allow the block of the subsequent else-if:

else if (yo == pelotas.size() - 1)

to execute as a separate case.

Then, because the arguments for ofApp::medirDistancia() are used as index values for the std::vector pelotas, you have to make sure the function is only called with values >= 0 and < pelotas.size(). So like Dimitre indicated, yo + 1 can not be greater than pelotas.size() - 1.