Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

353
Vistas
How to make multiple objects move randomly?

I'm trying to make a sort of Agar.io clone in Processing. I have spawned in a lot of food dots, but I wanted to also make them move about and bounce off the edges of the screen border to add some flair. However I am not too sure how to go about making the dots all move about randomly.

ArrayList<Ellipse> ellipse = new ArrayList <Ellipse>();

//Images
PImage background;
int x = 2;

//Words 
PFont arial;


void setup(){
  size(1920,1080);
  
  //Background change
  if (x == 1){
   background = loadImage("backdrop1.jpg");
}
 if (x == 2){
   background = loadImage("backdrop2.jpg");
}
//Creating the font
arial = createFont ("Arial", 16, true); //the true is for antialiasing

//Load from text file
//tbd...

//Adding the food ellipses

for(int foodSpawn = 0; foodSpawn < 50; foodSpawn++){
  ellipse.add(new Ellipse(random(100,1820),random(100,980), 50, 50));
}
}

void draw(){
  background(background); 
  for(int i = 0; i<ellipse.size(); i++){
  Ellipse e = ellipse.get(i);
  
  fill(#62C3E8);
  ellipse(e.xLoc,e.yLoc, e.eWidth, e.eHeight);
  }
}

class Ellipse {
 float xLoc;
 float yLoc;
 float eWidth;
 float eHeight;
 
 public Ellipse(float xLoc, float yLoc, float eWidth, float eHeight){
   this.xLoc = xLoc;
   this.yLoc = yLoc;
   this.eWidth = eWidth;
   this.eHeight = eHeight;
}
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The ellipses already have position attributes, so simply adding a method to move them should work. If you want them to collide realistically with the walls, you'll need to give each ellipse an initial random velocity. Then, you update the position at set time intervals based on the current velocity and the interval length. E.g.:

public void move() {
    // Note: these are signed, and xvel and vyel are in pixels/second
    float x_move_dist = this.xvel*time_int
    float y_move_dist = this.yvel*time_int

    // Update xloc
    // Check collision with left wall
    if (this.xloc + x_move_dist - this.eWidth/2 < 0) {
        // Assuming conservation of momentum, we can reflect the movement off the wall
        this.xloc = -(this.xloc + x_move_dist + this.eWidth/2)
    }
    // Check collision with right wall
    else if (this.xloc + x_move_dist + this.eWidth/2 > 1920) {
        // Again, reflect off wall
        this.xloc = 1920 - ((this.xloc + x_move_dist) - 1920) - this.eWidth/2
    }
    // Otherwise, just update normally
    else {
        this.xloc = this.xloc + x_move_dist
     }

    // Update yloc
    // Check collision with bottom wall
    if (this.yloc + y_move_dist - this.eHeight/2 < 0) {
        // Again, reflect off wall
        this.yloc = -(this.yloc + y_move_dist) + this.eHeight/2
    }
    // Check collision with top wall
    else if (this.yloc + y_move_dist + this.eHeight/2 > 1080) {
        // Again, reflect off wall
        this.yloc = 1920 - ((this.yloc + y_move_dist) - 1920) - this.eHeight/2
    }
    // Otherwise, just update normally
    else {
        this.yloc = this.yloc + y_move_dist
     }
 
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda