Skip to content

Commit

Permalink
Gravity example: Update params + use step delta in gravity force
Browse files Browse the repository at this point in the history
  • Loading branch information
DrA1ex committed Sep 29, 2023
1 parent 921952c commit 19ff842
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions examples/gravity/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ const {
count, minSize, maxSize, gravity, particleScale, particleOpacity, worldScale, minInteractionDistance,
particleTextureUrl, particleBlending, particleColoring
} = Params.parseSettings({
count: {parser: Params.Parser.int, param: "count", default: 200},
minSize: {parser: Params.Parser.int, param: "min_size", default: 10},
maxSize: {parser: Params.Parser.int, param: "max_size", default: 20},
gravity: {parser: Params.Parser.float, param: "gravity", default: 10},
count: {parser: Params.Parser.int, param: "count", default: 300},
minSize: {parser: Params.Parser.int, param: "min_size", default: 20},
maxSize: {parser: Params.Parser.int, param: "max_size", default: 40},
gravity: {parser: Params.Parser.float, param: "gravity", default: 1000},
particleScale: {parser: Params.Parser.float, param: "p_scale", default: 20},
particleOpacity: {parser: Params.Parser.float, param: "opacity", default: 1},
worldScale: {parser: Params.Parser.float, param: "w_scale", default: 40},
Expand Down Expand Up @@ -108,8 +108,13 @@ for (let i = 0; i < count; i++) {
BootstrapInstance.enableHotKeys();
BootstrapInstance.run();

function gravityStep() {

let stepDelta = 0;

function gravityStep(delta) {
const tree = BootstrapInstance.solver.stepInfo.tree;
stepDelta = delta;

if (tree) {
calculateTree(tree);
}
Expand Down Expand Up @@ -168,7 +173,7 @@ function calculateLeafBlock(blocks, pForce) {
function calculateLeafData(leaf, pForce) {
for (let i = 0; i < leaf.items.length; i++) {
const attractor = leaf.items[i];
attractor.velocity.add(pForce);
attractor.velocity.add(pForce.scaled(stepDelta));

for (let j = 0; j < leaf.items.length; j++) {
if (i === j) continue;
Expand All @@ -194,8 +199,8 @@ function calculateForce(p1, p2, g, out) {

const force = -g / distSquare;
if (out.velocity !== undefined) {
out.velocity.x += dx * force;
out.velocity.y += dy * force;
out.velocity.x += dx * force * stepDelta;
out.velocity.y += dy * force * stepDelta;
} else {
out.x += dx * force;
out.y += dy * force;
Expand Down

0 comments on commit 19ff842

Please sign in to comment.