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

173
Vistas
Trying to add separation to boids simulation in p5js. Collision teleporting boids to origin

I'm trying to make a boids simulation in p5js. Currently I'm trying to implement the separation rule, following this tutorial. (The relevant section is 6.11). Whenever two boids collide, one of them gets "teleported" to the origin instead of avoiding the other boid. I've narrowed down the problem to the separate function in my Boid class, which is below.

separate(boids) {
    var count = 0
    var desiredVelocity = createVector(0, 0)
    // loop through boids to find if any are too close
    for (const boid of boids) {
      const distance = this.position.dist(boid.position)
      if ((distance < this.desiredSeparation) && (boid.id != this.id)) {
        count += 1
        // too close; move away from boid
        var steerAwayVector = boid.position.sub(this.position)
        this.drawArrow(steerAwayVector, this.position, '#eb9ac1')
        steerAwayVector.normalize()
        steerAwayVector.div(distance)
        desiredVelocity.add(steerAwayVector)
      }
    }
    if (count > 0) {
      desiredVelocity.setMag(this.maxSpeed)
      var steer = desiredVelocity.sub(this.velocity)
      steer.limit(this.maxForce)
      this.applyForce(steer)
    }
  }

See my full code on github here. What/where is the problem? How do I fix this behavior?

about 4 years ago · Juan Pablo Isaza
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