Boids library add force

I think I asked the question the wrong way last time; would anyone be willing to show me and/or provide an example of adding a custom force (it says you can use .addForce) to make the boids follow something?

I’m trying to do some human swarm interaction in VR and I’m having a little trouble

(I should mention that I have done lots of 2D boid coding, but for some reason, am having trouble understanding how to translate that knowledge into working with this library; I’ve done “searching” with 2D boids, e.g.).

Maybe @brunobg is around?

1 Like

No dice, I’m afraid; do you have any other suggestions? Having 3D boids that you can add and manipulate forces to/with would be helpful for the community. I’m eventually going to open source this entire project. (sorry, having examples of this, and added functionality to the above library, like a follow, e.g.)

The developer says: “This is a very simple physics simulator, so all forces are added and the resulting vector is applied for the frame time. So the larger the magnitude of the vector, the larger the force applied. Check a basic Newtonian physics tutorial if you need extra information about it.”

What I’m confused about is that I added a vector that is my player’s location. To increase it’s magnitude, do I have to multiply the xyz by some factor?

I know nothing about physics, maybe @RaananW or @Cedric our resident experts of the subject would have an idea?

first, you can check opensteer. quite old but it’s the reference you can start with: OpenSteer Preliminary Documentation

here is how I would do it:

  • get destination point ‘D’ and ‘O’ is the boid position and ‘V’ is the boid velocity vector
  • target vector is ‘D-O’. normalize it and you get the target velocity direction .
  • normalize ‘V’, do a linear interpolation between |V| and |D-O| (like 5% per frame), normalize the result to get the new boid velocity direction.
  • Get the ‘V’ length, multiply that length with the new boid velocity direction and you get the new velocity.
  • update position of boid with the new velocity and delta time of the frame
  • repeat!
seek(target) {
    let desired = p5.Vector.sub(target, this.position); // A vector pointing from the location to the target
    // Normalize desired and scale to maximum speed
    desired.normalize();
    desired.mult(this.maxspeed);
    // Steering = Desired minus Velocity
    let steer = p5.Vector.sub(desired, this.velocity);
    steer.limit(this.maxforce); // Limit to maximum steering force
    return steer;
  }

That is basically what I did when I built this in 2D. I guess I’m having trouble translating this from p5 → babylonjs, but I’m going to try today.

    //EXPERIMENT WITH THIS:
    boidsManager.addForce((boidsManager, boid) => {
      var desired = new BABYLON.Vector3(0,0,0);
      desired = desired.subtract(
        xr.baseExperience.camera.position,
        boid.position
      );
      desired.normalize();
      desired.multiply(15);
      var steer = new BABYLON.Vector3(0,0,0);
      steer = steer.subtract(desired, boid.velocity);
      //steer.limit(this.maxforce);
      return steer;
    });

this is what I have so far, but they’re completely ignoring my command, lol, so I guess I have to figure out if I’m initializing this correctly, and if it’s getting incorporated into the force balancing that’s happening in the other part of the code

at least it doesn’t break anything

Well, it appears that I see the extra force in the boidsmanager. Like, it got logged, and I see the vector that is produced, but it doesn’t seem to alter the swarm’s behavior still… trying to figure it out.

I made a glitch example and am sharing it with people Glitch :・゚✧ ← still trying to get it to work, but this is very close to a playground.

1 Like

Does anybody know of a playground with a “seek function”? Like one mesh seeking another? (I know the follow camera does this, I guess I could look up the follow camera code).