diff options
| author | LalleSX | 2026-10-24 04:35:56 +0200 |
|---|---|---|
| committer | LalleSX | 2026-10-24 04:35:56 +0200 |
| commit | c4c1c840f8761ea45a3d2dcc912a59d8ced78cb5 (patch) | |
| tree | 8fcbbfffb6a34af73697a703f77e6b82f191b91f /src/physics/electricField.ts | |
| download | 3D-Electrophysics-c4c1c840f8761ea45a3d2dcc912a59d8ced78cb5.tar.gz 3D-Electrophysics-c4c1c840f8761ea45a3d2dcc912a59d8ced78cb5.zip | |
init
Diffstat (limited to 'src/physics/electricField.ts')
| -rw-r--r-- | src/physics/electricField.ts | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/physics/electricField.ts b/src/physics/electricField.ts new file mode 100644 index 0000000..26975e3 --- /dev/null +++ b/src/physics/electricField.ts @@ -0,0 +1,22 @@ +export interface Point { x: number; y: number; z: number } +export interface Charge extends Point { coulombs: number } +export const COULOMB_CONSTANT = 8.9875517923e9; + +/** SI units: positions in metres, charges in coulombs, output in N/C. + * Samples inside 0.18 m of a charge are omitted to avoid visual singularities. + */ +export function electricField(point: Point, charges: readonly Charge[]): Point { + const field = { x: 0, y: 0, z: 0 }; + for (const charge of charges) { + const dx = point.x - charge.x; + const dy = point.y - charge.y; + const dz = point.z - charge.z; + const r2 = dx * dx + dy * dy + dz * dz; + if (r2 < 0.18 ** 2) continue; + const factor = COULOMB_CONSTANT * charge.coulombs / (r2 * Math.sqrt(r2)); + field.x += factor * dx; + field.y += factor * dy; + field.z += factor * dz; + } + return field; +} |