Skip to content

Commit

Permalink
start testing Simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
lounsbrough committed Aug 22, 2024
1 parent 5e8fd3d commit a6dc577
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 16 deletions.
3 changes: 2 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export default tseslint.config(
},
{
rules: {
'semi': ['error', 'never']
semi: ['error', 'never'],
quotes: ['error', 'single']
}
}
)
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/** @type {import('ts-jest').JestConfigWithTsJest} **/
// eslint-disable-next-line no-undef
module.exports = {
testEnvironment: "node",
testEnvironment: 'node',
transform: {
"^.+.tsx?$": ["ts-jest", {}],
'^.+.tsx?$': ['ts-jest', {}],
},
}
1 change: 0 additions & 1 deletion rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export default {
sourcemap: true
}
],
external: ['node:events'],
plugins: [
typescript()
]
Expand Down
4 changes: 2 additions & 2 deletions src/Body.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Body from "./Body"
import Vector from "./Vector"
import Body from './Body'
import Vector from './Vector'

describe('Body', () => {
describe('setExternalForces', () => {
Expand Down
52 changes: 52 additions & 0 deletions src/Simulation.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import Body from './Body'
import Simulation from './Simulation'
import Vector from './Vector'

describe('Simulation', () => {
it('should call changeCallback', async () => {
let resolver: (value?: unknown) => void
const promise = new Promise((resolve) => { resolver = resolve })

const simulation = new Simulation({
bodies: [],
renderInterval: 0,
changeCallback: () => { resolver() }
})

await promise
simulation.stop()
})

it('should move bodies through universe', async () => {
let resolver: (value?: unknown) => void
const promise = new Promise((resolve) => { resolver = resolve })

const startingPositions = [
new Vector(1, 1, 1),
new Vector(-1, -1, -1)
]

const simulation = new Simulation({
bodies: [
new Body({
position: startingPositions[0],
velocity: new Vector(0, 0, 0)
}),
new Body({
position: startingPositions[1],
velocity: new Vector(0, 0, 0)
})
],
renderInterval: 0,
changeCallback: () => { resolver() }
})

expect(simulation.bodies).toBe(simulation.universe.bodies)

await promise
simulation.stop()

expect(simulation.bodies[0].position).not.toEqual(startingPositions[0])
expect(simulation.bodies[1].position).not.toEqual(startingPositions[1])
})
})
25 changes: 19 additions & 6 deletions src/Simulation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import EventEmitter from 'node:events'
import Universe from './Universe'
import Body from './Body'

Expand All @@ -10,8 +9,8 @@ export default class Simulation {
renderInterval: number

universe: Universe
eventEmitter: EventEmitter
simulationInterval: NodeJS.Timeout
changeCallback: () => void

constructor({
/** orbital bodies in the simulation */
Expand All @@ -23,19 +22,23 @@ export default class Simulation {
/** simulation time elapsed between iterations */
deltaTime,
/** computer time between iterations */
renderInterval
renderInterval,
/** callback fired when simulation state changes */
changeCallback
}: {
bodies: Body[]
gravity?: number
collisions?: boolean
deltaTime?: number
renderInterval?: number
changeCallback: () => void
}) {
this.bodies = bodies
this.gravity = gravity ?? 6.674e-11
this.collisions = collisions ?? false
this.deltaTime = deltaTime ?? 0.5
this.renderInterval = renderInterval ?? 10
this.changeCallback = changeCallback

this.universe = new Universe({
bodies: this.bodies,
Expand All @@ -44,10 +47,20 @@ export default class Simulation {
deltaTime: this.deltaTime
})

this.eventEmitter = new EventEmitter()
this.start()
}

/** start simulation */
start() {
clearInterval(this.simulationInterval)
this.simulationInterval = setInterval(() => {
this.universe.moveBodiesThroughTime()
this.eventEmitter.emit('deltaTime')
}, renderInterval)
this.changeCallback()
}, this.renderInterval)
}

/** stop simulation */
stop() {
clearInterval(this.simulationInterval)
}
}
6 changes: 3 additions & 3 deletions src/StableUniverses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,19 @@ export const FigureEight = new Universe({
mass: 1,
position: new Vector(0.97000436, -0.24308753, 0),
velocity: new Vector(0.466203685, 0.43236573, 0),
color: "#3d9900"
color: '#3d9900'
}),
new Body({
mass: 1,
position: new Vector(-0.97000436, 0.24308753, 0),
velocity: new Vector(0.466203685, 0.43236573, 0),
color: "#00314e"
color: '#00314e'
}),
new Body({
mass: 1,
position: new Vector(0, 0, 0),
velocity: new Vector(-0.93240737, -0.86473146, 0),
color: "#008ebd"
color: '#008ebd'
})
],
gravity: 1, collisions: false, deltaTime: 0.005
Expand Down
2 changes: 1 addition & 1 deletion src/Vector.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Vector from "./Vector"
import Vector from './Vector'

describe('Vector', () => {
describe('constructor', () => {
Expand Down

0 comments on commit a6dc577

Please sign in to comment.