npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@corollarium/babylon-boids

v1.0.2

Published

A boids library for BabylonJS

Downloads

8

Readme

babylon-boids

A boids library for BabylonJS. Used in the Laje de Santos Virtual Dive project. Check:

Shark boids Shark boids with the debugging helpers

How to use it

First create your BoidsManager object and associate it with your meshes.

function loadCubes (total, scene) {
    // instantiate our boids manager. Note it doesn't handle the meshes themselves, only the total meshes.
    const boidsManager = new BoidsManager(total, new BABYLON.Vector3(0.0, 0.0, 0.0), 10.0, 10.0);

    // so let's now create our meshes
    const models = [];
    const brickMaterial = new BABYLON.StandardMaterial('brickMaterial', scene);
    brickMaterial.diffuseTexture = new BABYLON.Texture('does_not_exist');
    for (let i = 0; i < total; i++) {
        // create a box
        const box = BABYLON.MeshBuilder.CreateBox(
            'box' + i,
            {
                width: 0.3, height: 0.5, depth: 1.0
            }
        );

        // associate box to boid data. 
        box.boid = boidsManager.boids[i];
        box.material = brickMaterial;
        models.push(box);
    }

    // if you want to get a debug with bounding spheres and velocity vectors
    boidsManager.showDebug();
    boidsManager.gui(scene);

    // so we return 3 things here: the models, the boidsManager, and an update callback
    // for the render loop.
    return {
        models,
        boidsManager,
        update: ((_boids, _models) => {
            return (deltaTime) => {
                // update the boid simulation first
                _boids.update(deltaTime);
                // now apply the calculated positions/orientations to the meshes
                _models.forEach((m) => {
                    m.position.copyFrom(m.boid.position);
                    m.setDirection(m.boid.orientation);
                });
            };
        })(boidsManager, models)
    };
}

Remember to call the update function

    // create a boid block with 20 boids
    const myBoids = loadCubes(20, scene);

    // Register a render loop to repeatedly render the scene
    engine.runRenderLoop(() => {
        const timeDiff = engine.getDeltaTime() / 1000.0;

        // update boids
        myBoids.update(timeDiff);

        scene.render();
    });

API

constructor(total, center, initialRadius = 1.0, boundRadiusScale = 100.0, initialVelocity = null)

Constructor. See parameters for class parameters that can be accessed and changed directly.

int total: Total number of boids in simulation

BABYLON.vector3 center: The center point of the boid volume.

float initialRadius: The radius of the initial boid volume. Boids are distributed randomly within this volume from center when created.

float boundRadiusScale: The bounding volume for the total simulation. Boids are restricted to this volume from center.

BABYLON.vector3 initialVelocity: The inicial velocity for the boids. A small randomness factor is added to each boid.

update(deltaTime)

Updates the boids. Call on every render loop.

Number deltaTime The time since last frame in seconds.

addForce(c)

Adds a force callback function. This enables you to change the simulation with your own forces. Called fr

callback c A valid JS function, called on update() for each boid. Receives as parameters: (BoidsManager, Boid)

showDebug(scene)

Turns on debug visual helpers.

BABYLON.scene scene The scene object.

hideDebug(scene)

Turns off debug visual helpers.

BABYLON.scene scene The scene object.

gui(scene)

Turns on a panel to visually change the boid simulation parameters.

BABYLON.scene scene The scene object.

Parameters

All accessed on the BoidsManager instance.

cohesion Cohesion factor.

separation Separation factor.

alignment Alignment factor.

separationMinDistance The

maxSpeed The maximum allowed speed for a boid, in units per second.

boundsMin The minimum bounds for the valid boid volume.

boundsMax The maximum bounds for the valid boid volume.

LICENSE

MIT