writing some array values to an arraylist
in
Programming Questions
•
1 year ago
Hi,
I'm trying to register every Nth point within and array to an arraylist so I can later include those to a sorted array and compare distance, in some sort of a flocking behaviour. The first array determines a tail for the agent, the arraylist is the collected info and the second array is the sorted list. Here's the code part of it:
- //Every Nth frames
- if (frameCount % TrailInterval == 0 && TrailNodeCount < maxTrailNodes) {
- // Register the Flocker's position
- PVector TrailNodeLoc = Floc.get();
- TrailNode T = new TrailNode(TrailNodeLoc);
- // Add the position to a local nodes list
- Trail.add(T);
- // Register 3 nodes per trail
- if (TrailNodeCount==0 || TrailNodeCount ==int(maxTrailNodes/2) || TrailNodeCount==maxTrailNodes-1 ) TrailsCollection.add(T);
- // If the trail is longer than the specified range
- if (Trail.size() == maxTrailNodes) {
- // Get the first node (that is the older one)
- TrailNode X = (TrailNode)Trail.get(0);
- // Get the index of the node within the global node list (will make sense later)
- int index = TrailsCollection.indexOf(X);
- if (index >=0) {
- // And remove it from the global node list (this is to avoid overflooding)
- TrailsCollection.remove(index);
- }
- // Remove it from the local trail list
- Trail.remove(0);
- }
- //Find the cell containing the Flocker
- int XCell = floor(T.Tloc.x/gridSize);
- int YCell = floor(T.Tloc.y/gridSize);
- int ZCell = floor(T.Tloc.z/gridSize);
- //Check the range and correct it if necessary
- if (XCell > Rows) XCell = Rows;
- if (YCell > Cols) YCell = Cols;
- if (ZCell > Stacks) ZCell = Stacks;
- if (XCell < 1) XCell = 0;
- if (YCell < 1) YCell = 0;
- if (ZCell < 1) ZCell = 0;
- //Enumerate the corresponding cell (-1 because it counts from zero)
- int Cell = (XCell + (YCell*Rows) + (ZCell*Cols*Rows))-1;
- if (Cell < 0) Cell = 0;
- if (Cell >= Cells) Cell = Cells-1;
- // Add the node to its Cell
- TrailCollection[Cell].add(T);
- }
- // And draw a polyline through the list of local nodes list, from first to last
- beginShape();
- for (int t=0; t<Trail.size(); t++) {
- TrailNode T = (TrailNode)Trail.get(t);
- MakeTrail[t] = T.Tloc;
- stroke(150, 20, 20);
- vertex(MakeTrail[t].x, MakeTrail[t].y, MakeTrail[t].z);
- }
- vertex(Floc.x, Floc.y, Floc.z);
- endShape();
- }
Somehow, I'm having strange behaviours with it and I can't sort it out.
Could you check it and see if you find the error here?
1