Bug with ArrayList

hi guys,
I’m having a bug that I just can not solve, I think it’s a very stupid thing.
in practice I would like this code to generate a map of points, which save in an arreyList, and then that there are 4 lines that intersect the point. So far, so good. the problem arises when I move the lines from one point to another interpolating the previous point with the next one. in part it works, but not for each loop cycle.

ArrayList<Point> points = new ArrayList<Point>();
Line lines;
ArrayList<PVector> momPos = new ArrayList<PVector>();
int i=1;
int k=0;

void setup() {
  size(1280, 720, P2D);
  for (int i=0; i<20; i++) {
    points.add(new Point());
  }
  for (float j=0.; j<=1.; j+=.01) {
    momPos.add(new PVector(lerp(points.get(i-1).pos.x, points.get(i).pos.x, j), lerp(points.get(i-1).pos.y, points.get(i).pos.y, j)));
  }
}

void draw() {
  background(0);
  noStroke();
  fill(255);
  for (Point p : points) {
    ellipse(p.pos.x, p.pos.y, 8, 8);
  }

  stroke(255, 0, 0);
  if (i>=points.size()-1) i=1;

  if (k>momPos.size()-1) {
    while (momPos.size()>0) {
      for (int j=0; j<momPos.size(); j++) {
        momPos.remove(j);
      }
    }
    for (float j=0.; j<=1.; j+=.01) {
      momPos.add(new PVector(lerp(points.get(i-1).pos.x, points.get(i).pos.x, j), lerp(points.get(i-1).pos.y, points.get(i).pos.y, j)));
    }
    println("hei");
    k=0;
    points.remove(i-1);
    i++;
  }
  lines = new Line(momPos.get(k));
  lines.lineRender();
  k++;
}
class Line {
  PVector pos;
  Line(PVector pos) {
    this.pos=pos;
  }
  
  void lineRender(){
    line(0, pos.y, pos.x, pos.y);
    line(pos.x, 0, pos.x, pos.y);
    line(pos.x, pos.y, width, pos.y);
    line(pos.x, pos.y, pos.x, height);
  }
}
class Point {
  PVector pos;
  Point() {
    pos=new PVector(random(width), random(height));
  }
}

Thanks for your help!!

  • use PVector for a random point OK
  • use a ArrayList for a number of this points good,
    because you want add and remove

– the use of class did not come into my mind for this points or target-line drawing
as it is not shorter or smarter… but, why not.

i just did not understand what the second ArrayList “momPos” is for?
using the same points?

meanwhile not sure what you want to do anyhow, i try my own
test, possibly take a look

Auto Shooter
// https://discourse.processing.org/t/bug-with-arraylist/5949?u=kll
// make a arraylist of pvector points
// calc shortest distance to the next target, aim, fire, remove the point....

int idt, pmax = 10;
ArrayList<PVector> points = new ArrayList<PVector>();
PVector target = new PVector(0, 0);
boolean theend = false, shoot = false;

int k = 0;  //firing circles

void setup() {
  size( 500, 500);
  for (int i = 0; i < pmax; i++) points.add(new PVector(random(-width/2, width/2), random(-height/2, height/2)));
  for ( PVector p : points ) println(p);
}

void draw_lines() {
  stroke(200, 0, 0);
  line(target.x, -height/2, target.x, height/2); 
  line(-width/2, target.y, width/2, target.y);
}

void draw_points() {
  for (int i = 0; i < points.size(); i++) { 
    if ( i == idt ) stroke(200, 0, 0); 
    else noStroke();
    fill(0, 200, 0);
    ellipse(points.get(i).x, points.get(i).y, 8, 8);
  }
}

int get_nearest() {
  int idx = -1;
  float tdist = width;
  for (int i = 0; i < points.size(); i++) {
    float thisdist = dist(points.get(i).x, points.get(i).y, target.x, target.y);
    if ( thisdist < tdist ) { 
      tdist = thisdist; 
      idx = i;
    }
  }
  return idx;
}

void aim() {
  if ( idt > -1 ) {
    PVector waytotarget = points.get(idt).copy();
    waytotarget.sub(target);
    if ( waytotarget.mag() > 0.5 ) {
      waytotarget.setMag(waytotarget.mag()*0.01);              // calc faster to short step in that direction
      target = target.add(waytotarget);     // change target
    } else { 
      if ( !shoot ) println("shoot at "+idt);       // only print 
      shoot = true;
      points.remove(idt);
    }
  } else { 
    println(" i win ! "); 
    theend = true;
  }
}

void fire() {
  if ( shoot) {
    fill(200, 100, 0);
    ellipse(target.x, target.y, 2*k, 2*k);
    k++;
    if ( k >= 30 ) {  
      shoot = false; 
      k = 0;
    }
  }
}

void show_end() {
  fill(0, 200, 0);
  rect(-50, -25, 100, 40);
  fill(200, 0, 0);
  text(" I WIN ! ", -20, 0);
}

void draw() {
  background(200, 200, 0);
  translate(width/2, height/2);
  if ( !theend ) {
    idt = get_nearest();
    draw_lines();
    draw_points();
    if ( !shoot ) aim();              // not walk while firing
    fire();
  } else show_end();
}

if i am OFF, just ignore it.

momPos is to generate the all the position between two point using lerp to interpolate.

I would like the cross to move from point a to point b, then from point b to point c and so on

ok, that i do, just with one arrraylist and some vector math even with speed calc…
did you check it?

yes…maybe i will change that…but the problem is in the index i suppose…btw
if i dont remove elements from the list(comment line 40) it works like i want!

but if you remove a point, you need to rebuild the other array…
in the JAVA doc i see a .clear() does that work?

i avoided all this just by finding the index of the nearest point and walk to it

mmh i rebuilt the momPos array every time k is > of momPos array

yes…clear work…but the line restart from the wrong point…i continue to think that is a problem about index!

definitely, never the point where the cross is removed, always a other one

how can i fix it? i’m going crazy lol

i am not able to follow your code, like the “k” thing…
but i see that you first make a

momPos.add

( using what points??? )
and later a

points.remove(i-1);

sounds wrong,
after remove need a
rebuild_momPos();

i add the points generated in the class a in the void setup.
k is used to iterate and stop momPos array

again, you delete the wrong point ( what i see )
and the cursor jump to different place and start walking to a new target.

and in code you do

momPos.add(…points.get(i-1)…points.get(i)

and then

points.remove(i-1);

??_______________________
pls. can you confirm that how it is happening in my version
is actually that what you want? because from your code i not see that.
and possibly you plan later a real operator shooting…
how i know?

yes it jump to the wrong point…if you delete
points.remove(i-1);
it work exactly how i want…

pls. move that line UP.

up where? out the if?

nope…but if i put it up…it doesn’t remove the right point!

yes, and it never did.
so move it up and delete the right one

  if (k>momPos.size()-1) {
    while (momPos.size()>0) {
      for (int j=0; j<momPos.size(); j++) {
        momPos.remove(j);
      }
    }

    println("hei");
    k=0;
    points.remove(i);
    i++;

    for (float j=0.; j<=1.; j+=.01) {
      momPos.add(new PVector(lerp(points.get(i-1).pos.x, points.get(i).pos.x, j), lerp(points.get(i-1).pos.y, points.get(i).pos.y, j)));
    }
  }

not work! it starts ever from a wrong point

i not know about start, but here it removes the right point