Strategies For Dealing With Too Much Recursion
in
Programming Questions
•
1 year ago
Hi Forum. Hoping some of you might be able to chime in on this issue...
I'm currently having a problem with a sketch (see
this thread), and have come to the conclusion that the issues I am having are with recursion.
Basically, I am attempting to programatically call a function, but it is happening too much. This is a fairly common problem I think, and wanted to know if there are any best practices for dealing with it out there...
In this example, I am attempting to 'bump' an agent (a 3D Vector) up to a higher Z-Plane upon seeing that it is too close to another agent... ( I can't use translate(), as I am also trailing the path, and writing to file... )
I have managed to solve this in a simple function that is called once upon a mousePressed, for example:
- for(int i = 0; i < population.size(); i++){
- Agent a = (Agent)population.get(i);
- a.updatePlane();
- }
And then the Function itself looks like this:
- void updatePlane() {
- aPos.z += 5;
- }
But, in order for it to happen within the sketch - automated so to speak - I have to call it within a conditional (checking to see the distance), which is checked from within a for loop (iterating over the ArrayList size), which is called within an a custom function that is called from within an update method of a class, which is called from within an identical for loop, which is called within draw(), which is called 60+ frames per second... you get the picture...
So obviously, the z-plane is being updated way too much, which results in something like the bottom line in the picture - where I want something like the top line...
I guess there are a few hacks that I can imagine to deal with this, such as trying to use a boolean to allow/disallow the increase, and trying to control the flow within the function that way, but this seems rather inelegant (not that I've ever claimed to be an elegant coder...)
I'm sure this is an issue that has come up before for all of you, and I'd really be interested to hear any thoughts on this...
Cheers,
~ Jesse
1