Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

354
Views
¿Cómo hacer que varios objetos se muevan aleatoriamente?

Estoy tratando de hacer una especie de clon de Agar.io en Processing. He generado muchos puntos de comida, pero también quería que se movieran y rebotaran en los bordes del borde de la pantalla para agregar algo de estilo. Sin embargo, no estoy muy seguro de cómo hacer que los puntos se muevan al azar.

 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 answers
Answer question

0

Las elipses ya tienen atributos de posición, por lo que simplemente agregar un método para moverlas debería funcionar. Si desea que colisionen de forma realista con las paredes, deberá asignar a cada elipse una velocidad aleatoria inicial. Luego, actualiza la posición en intervalos de tiempo establecidos en función de la velocidad actual y la duración del intervalo. P.ej:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!