Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selective Wind #2085

Open
bptarpley opened this issue Nov 29, 2023 · 0 comments
Open

Selective Wind #2085

bptarpley opened this issue Nov 29, 2023 · 0 comments

Comments

@bptarpley
Copy link

This feature request is to make the "wind" feature more robust. The idea is to make it configurable such that a wind from a particular direction will blow nodes that belong to particular groups.

Let's assume, for instance, that we set the "group" attribute on 100 nodes--specifically, for the first 25 set that attribute to "a", for the next 25 set "b", next 25 set "c", and the last 25 set "d".

Let's also assume that we can pass in a "wind" specification to the groups module:

groups: {
  a: {
    color: "red",
    wind: { x: 0, y: -1 }
  },
  b: {
    color: "blue",
    wind: { x: 0, y: 1 }
  },
  c: {
    color: "green",
    wind: { x: 1, y: 0 }
  },
  d: {
    color: "yellow",
    wind: { x: -1, y: 0 }
  },
}

Then, upon running the physics simulation, a "wind" effectively blows all nodes with "group" set to "a" northward, all set to "b" southward, all set to "c" eastward, and all set to "d" westward.

The use case for such a feature would be to help tease out and organize particularly messy graphs with heterogeneous types of nodes.

I was able to make this work with the following hack, so it should be relatively easy to implement. I'd make a pull request but I'm fuzzy on how to validate the available options, update the documentation, etc:

this.network.physics._performStep = function(nodeId) {
    const node = this.body.nodes[nodeId];
    const force = this.physicsBody.forces[nodeId];

    // THIS IS THE HACK. IT'S HACKY BECAUSE IT PRESUMES THAT A "wind" OBJECT EXISTS
    // IN THE GROUPS CONFIGURATION
    if (node.options.hasOwnProperty('group')) {
        force.x += this.options.groups[node.options.group].wind.x;
        force.y += this.options.groups[node.options.group].wind.y;
    } 

    const velocity = this.physicsBody.velocities[nodeId];

    // store the state so we can revert
    this.previousStates[nodeId] = {
        x: node.x,
        y: node.y,
        vx: velocity.x,
        vy: velocity.y,
    };

    if (node.options.fixed.x === false) {
        velocity.x = this.calculateComponentVelocity(
            velocity.x,
            force.x,
            node.options.mass
        );
        node.x += velocity.x * this.timestep;
    } else {
        force.x = 0;
        velocity.x = 0;
    }

    if (node.options.fixed.y === false) {
        velocity.y = this.calculateComponentVelocity(
            velocity.y,
            force.y,
            node.options.mass
        );
        node.y += velocity.y * this.timestep;
    } else {
        force.y = 0;
        velocity.y = 0;
    }

    const totalVelocity = Math.sqrt(
        Math.pow(velocity.x, 2) + Math.pow(velocity.y, 2)
    );
    return totalVelocity;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant