From 65f3f9dac1099cc3a670a182d838f7206a7ff438 Mon Sep 17 00:00:00 2001 From: Billy Rennekamp Date: Tue, 18 Jun 2024 20:48:02 +0200 Subject: [PATCH] gravity visible --- src/anybody.js | 4 +++- src/calculations.js | 10 +++++++--- src/nft.js | 1 + src/visuals.js | 27 +++++++++++++++++++++++++++ test/forceAccumulator.test.js | 3 ++- test/nftTest.test.js | 3 ++- 6 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/anybody.js b/src/anybody.js index c5b6a31d..306266a6 100644 --- a/src/anybody.js +++ b/src/anybody.js @@ -429,7 +429,9 @@ export class Anybody extends EventEmitter { // ) // this.bodies = bodies // this.missiles = missiles || [] - this.bodies = this.forceAccumulator(this.bodies) + const forceResults = this.forceAccumulator(this.bodies) + this.bodies = forceResults.bodies + this.forces = forceResults.forces var results = this.detectCollision(this.bodies, this.missiles) this.bodies = results.bodies this.missiles = results.missiles || [] diff --git a/src/calculations.js b/src/calculations.js index a50bd968..8c23822c 100644 --- a/src/calculations.js +++ b/src/calculations.js @@ -4,9 +4,11 @@ export const Calculations = { forceAccumulator(bodies = this.bodies) { bodies = this.convertBodiesToBigInts(bodies) - bodies = this.forceAccumulatorBigInts(bodies) + const results = this.forceAccumulatorBigInts(bodies) + bodies = results.bodies + const forces = results.forces bodies = this.convertBigIntsToBodies(bodies) - return bodies + return { bodies, forces } }, // async circomStep(bodies, missiles) { @@ -94,12 +96,14 @@ export const Calculations = { for (let i = 0; i < bodies.length; i++) { accumulativeForces.push([0n, 0n]) } + const forces = {} const time = BigInt(this.speedFactor) for (let i = 0; i < bodies.length; i++) { const body = bodies[i] for (let j = i + 1; j < bodies.length; j++) { const otherBody = bodies[j] const force = this.calculateForceBigInt(body, otherBody) + forces[i + '-' + j] = force // const bodyVelocity = [ // body.radius == 0n // ? 0n @@ -185,7 +189,7 @@ export const Calculations = { body.position.y = scaledWindowWidth } } - return bodies + return { bodies, forces } }, calculateBodyFinal() { diff --git a/src/nft.js b/src/nft.js index 15f2ab1f..af13bc4f 100644 --- a/src/nft.js +++ b/src/nft.js @@ -8,6 +8,7 @@ window.anybody q5.setup = () => { window.anybody = new Anybody(q5, { mode: 'game', + level: 2, // showLevels: true, // target: 'inside', // globalStyle: 'psycho', diff --git a/src/visuals.js b/src/visuals.js index 2d357433..39212f6b 100644 --- a/src/visuals.js +++ b/src/visuals.js @@ -140,6 +140,10 @@ export const Visuals = { // } // } + if (this.forces) { + this.drawForces() + } + if (!this.firstFrame) { this.drawBodies() } @@ -215,6 +219,29 @@ export const Visuals = { } this.firstFrame = false }, + drawForces() { + const { forces } = this + this.p.stroke('rgba(255,255,255,0.2)') + this.p.strokeCap(this.p.PROJECT) + + for (const [key, value] of Object.entries(forces)) { + // Your code here + console.log({ key }) + console.log({ value }) + const bodyIndexes = key.split('-') + const body1 = this.bodies[bodyIndexes[0]] + const body2 = this.bodies[bodyIndexes[1]] + const force = Math.abs(parseInt(value[0] + value[1])) + console.log({ force }) + this.p.strokeWeight(force) + this.p.line( + body1.position.x, + body1.position.y, + body2.position.x, + body2.position.y + ) + } + }, drawPause() { if (this.paused) { this.p.fill('white') diff --git a/test/forceAccumulator.test.js b/test/forceAccumulator.test.js index 48d4ccfd..9b2f1ae4 100644 --- a/test/forceAccumulator.test.js +++ b/test/forceAccumulator.test.js @@ -38,7 +38,8 @@ describe('forceAccumulatorMain circuit', () => { ) // console.dir({ bodies }, { depth: null }) - const out_bodies = anybody + const results = anybody + const out_bodies = results.bodies .forceAccumulatorBigInts(bodies) .map(anybody.convertScaledBigIntBodyToArray.bind(anybody)) // console.log({ out_bodies }) diff --git a/test/nftTest.test.js b/test/nftTest.test.js index 196152d7..c25396f0 100644 --- a/test/nftTest.test.js +++ b/test/nftTest.test.js @@ -50,7 +50,8 @@ describe.skip('nft circuit', () => { ) // console.dir({ bodies }, { depth: null }) for (let i = 0; i < steps; i++) { - bodies = anybody.forceAccumulatorBigInts(bodies) + const results = anybody.forceAccumulatorBigInts(bodies) + bodies = results.bodies } const out_bodies = bodies.map( anybody.convertScaledBigIntBodyToArray.bind(anybody)