ofDist only detecting one vector element

Hello. I have a main program that has a ship that is moved successfully using oscHook. I also have a class that gets pulled into the main program. I create a vector of object from the class. Together the vector objects move and I control the ship. I have a warning system (collision detection that makes the ball turn colour when within distance. The problem for me, is that the for loop only seems to carry out the detection collision for one of the class objects. And hints welcomed. Thank you. Rob

HI!

At first glance it appears that you are overwriting the ‘distance’ variable.
The for loop will run balls.size() times, and each time, the ofDist value will be stored over the same ‘distance’ variable.

You can store the values on a vector;

distances[i] = ofDist(…);

Or you can store only the smallest distance

float newdist = ofDist(…);
distance = distance < newdist ? distance : newdist;

Hello and thanks for the reply. I think I did what was suggested by you, but in an around about way. I added another variable to the class constructor. Here is the code. However, I have the same problem. Also I thought I would highlight that the update function in the native class code for the torpedo is not updating anything, I thought the main program would do the updating on vector of torpedos.
thanks
Rob

Hi,

Storing the distance as a member of the class is also a solution. :+1:

Regarding you issue…
I’m not totally sure if I do understand your question. You are talking about updating ‘torpedo’ but the screenshot shows Ball::update.

Anyway, your function looks empty. If you want to modify/update any value, you have to write that at the function.

void Ball::update(float x, float y) {
ballposition.x = x;
ballposition.y = y;
}

Then the Ball::update function has to be called each time you want to update it. Best option is calling it from the App::update function.

Hi again,

I seem to have a half fix and am yet to apply your last suggestion. See code for how far I went with tweaking the ball (torpedo) class, sorry I’m still in the testing phase so variables and methods do not logically match the name of my classes, suffice to say the ball class should at one point be renamed.

So in any case I entered a new variable into the class that would assist in the collision algorithm, but again no change in the logic. I then commented out the else if in the contact function and it works. Each torpedo that comes into contact with the ship changes its colour. I tested this over an over so know this is the case. However; I need to work out a way to revert the colour back after it is out of range. Nevertheless, I think I’m over the hump.
Thank you kindly for your help, it’s been a sharp learning curve.
Rob

Hi,

You look in good track.
The way to reverse the color would be just with an else statement.

if (distance < radius) {
// set the active color
} else {
// set the original color
}

Greetings and good luck