Move object along a path

I show the example code and ask the question.

ArrayList<Nave> naves;
int tiempo;

void settings(){
  size(640,480,P2D);
  noSmooth();
}

void setup(){
  frameRate(60);
  rectMode(CENTER);
  
  naves = new ArrayList<Nave>();
}

void draw(){
  background(0,0,0);
  
  //crear objetos cada tiempo
  tiempo++;
  if(tiempo >= 120){
    naves.add(new Nave());
    tiempo = 0;
  }
  
  //actualizar,pintar
  for(int indice = naves.size()-1;indice >= 0;indice--){
    Nave nave = naves.get(indice);
    
    nave.actualizar();
    nave.pintar();
  }
  
  //texto
  textSize(24);
  textAlign(CENTER);
  fill(color(255,255,255));
  text("tiempo: "+tiempo,320,32);
}
public class Nave{
  private float x,y,ancho,alto,velocidad;
  private color colores;
  
  public Nave(){
    this.ancho = 50;
    this.alto = 50;
    this.x = random(32,608);
    this.y = random(-32,-608);
    this.velocidad = 3;
    this.colores = color(225,225,0);
  }
  
  public void actualizar(){
    mover();
    eliminar();
  }
  
  private void pintar(){
    fill(colores);
    rect(x,y,ancho,alto);
  }
  
  private void mover(){
    y += velocidad;
  }
  
  private void eliminar(){
    if(y > 440){
      naves.remove(this);
    }
  }
}//fim clase nave

This example shows some objects that appear at the top and move down.I would like to know how a coordinate path could be created and the objects would move along that path.

I thought that you could create two lists and add the “x” coordinates in one list and the “y” coordinates in another list. Then the object would have to traverse those coordinates at a set speed. Do you know how I could do this because I have no idea, Although if you have another idea I would be happy to read it.

That could work. That’s called parallel lists, because the elements in one list correspond to the elements in another list.

Instead, you could create a class (or use the existing PVector class) that contains x and y fields, and then store instances of that class in a single list. I wrote about that here:

You might also find the lerp() function useful:

This is what I have done so far, but I don’t know what else to do, I am stuck at this point.

public class Nave{
  private float ancho,alto,velocidad;
  private color colores;
  private PVector[] camino; 
  
  public Nave(){
    this.ancho = 50;
    this.alto = 50;
    this.camino = new PVector[5];
    
    for(int i = 0;i < this.camino.length;i++){
      this.camino[i] = new PVector(random(32,608),random(-32,-608));
    }
    
    this.velocidad = 3;
    this.colores = color(225,225,0);
  }
  
  public void actualizar(){
    mover();
    eliminar();
  }
  
  private void pintar(){
    fill(colores);
    for(int i = 0;i < camino.length;i++){
      rect(camino[i].x,camino[i].y,ancho,alto);
    }
  }
  
  private void mover(){
    for(int i = 0;i < camino.length;i++){
      camino[i].add(0,velocidad);
    }
  }
  
  private void eliminar(){
    for(int i = 0;i < camino.length;i++){
      if(camino[i].y > 440){
        naves.remove(this);
      }
    }
  }
}//fim clase nave

What does your code do so far?

I’d start with a smaller example, something like a single point that moves from point A to point B. Get that working, and then add logic that moves from point B to point C, and then keep improving from there.

If you get stuck on a smaller example, it’ll also be a lot easier to help when you’re stuck!