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

167
Views
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
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!