JavaScript Gravity Simulator: Check One Mutual Attraction Step

Applying the same acceleration to two unequal masses hides the distinction between force and acceleration. A single numerical step makes each quantity inspectable before adding an orbit animation.

Compute the pair force from the old positions, update both velocities, then commit both positions. The dimensionless ledger includes momentum and center of mass.

JavaScript Gravity Simulator: Check One Mutual Attraction Step
JavaScript Gravity Simulator: Check One Mutual Attraction Step
Open the full example ↗

Declare a dimensionless fixture

Use these original supplied settings:

Quantity Body A Body B
Mass 1 2
Position (-1, 0) (1, 0)
Velocity (0, 0) (0, 0)

Set the model parameter G = 1 and time step h = 0.1. These are dimensionless classroom values; G is not the physical SI gravitational constant. The position values represent dimensionless point locations.

NASA's explanation of pair attraction gives force magnitude as G × mA × mB / r², where r is the distance between the particles. NASA weight equation.

The displacement from A to B is (2, 0), so the distance is 2. The magnitude is 1 × 1 × 2 / 2² = 0.5. Attraction points toward the other body: force on A is (+0.5, 0), and force on B is (-0.5, 0).

Equal forces do not mean equal accelerations

For constant masses, use a = F / m separately for each body. The interaction forces are equal in magnitude and opposite in direction. These are the narrow second- and third-law facts used here. NASA Newton's laws.

Body Force Acceleration
A (+0.5, 0) (+0.5, 0)
B (-0.5, 0) (-0.25, 0)

B has twice A's mass, so the same force magnitude produces half the acceleration magnitude. A display labeled only “gravity = 0.5” would blur the distinction between force and acceleration.

Keep the signed forces, acceleration values, and positions labeled separately. The useful first view shows both accelerations beside the equal-and-opposite force pair.

Update both bodies from one unchanged state

For this original numerical exercise, choose a semi-implicit Euler step: compute forces from old positions, update velocities, then update positions using the new velocities. This is an authored method choice; the NASA sources do not endorse this integrator or its accuracy.

  1. Snapshot both old positions and velocities.
  2. Compute the pair force once from that snapshot and assign its opposite to the other body.
  3. Divide each body's force by its mass.
  4. Calculate both new velocities with vNew = vOld + h × aOld.
  5. Calculate both new positions with xNew = xOld + h × vNew.
  6. Commit the complete new state together.

If you move A before computing B's force, the two calculations use different states. That is a different update rule and can break the equal-pair bookkeeping this exercise is meant to inspect.

With the supplied fixture, A's new x velocity is 0 + 0.1 × 0.5 = 0.05; B's is 0 + 0.1 × (-0.25) = -0.025. Their new x positions are -0.995 and 0.9975. All y components remain zero.

Output after one step A B
Velocity (0.05, 0) (-0.025, 0)
Position (-0.995, 0) (0.9975, 0)

Check momentum and center of mass

The expected total x momentum is 1 × 0.05 + 2 × (-0.025) = 0, matching the initial zero momentum. The initial center of mass is (-1 + 2 × 1) / 3 = 1/3; after the step it is (-0.995 + 2 × 0.9975) / 3 = 1/3.

Keep expected and actual values in separate ledger columns. In a later floating-point implementation, confirm a documented numerical tolerance before treating tiny differences as a failure. These two checks do not establish energy conservation, long-term stability, orbital accuracy, or agreement with an experiment.

Hold invalid states before dividing

The helper requires finite positive masses, G, and h, and finite position and velocity components. Coincident centers give zero distance: reject that state before division and clear stale accepted results. Do not silently introduce softening, collisions, mergers, or a guessed separation.

Bound this exercise to two point masses and one step from the configured initial state. Step calculates once from the configured initial state; another activation must not extend the exercise into a many-step orbit simulation. Reset restores the supplied initial fixture and clears the result ledger. Editing settings invalidates prior acceptance and requires a fresh checked step.

Try the worked example

Run the default step and inspect A at −0.995 versus B at 0.9975. Set both positions to 1 and calculate to see division held. Reset before another default step. Both forces must use the same unchanged old positions.

The dimensionless semi-implicit Euler exercise is not an orbital predictor, collision solver, or physical validation.

Recreate this example

Build one complete self-contained HTML page for this task: calculate one simultaneous two-body attraction step along the x axis.

Use these exact original practice data and worked expectations: Compare equal and opposite forces with unequal accelerations in one step. Settings: G = 1; h = 0.1; masses A = 1 and B = 2; positions A = −1 and B = 1; initial velocities = 0. Expected force magnitudes: 0.5; accelerations: 0.5 and −0.25; velocities: 0.05 and −0.025; positions: −0.995 and 0.9975; momentum: 0; center of mass: 1/3.

Implement the following useful local interactions: Provide positive masses, G, h and finite position fields. Step computes once from configured zero velocities. Reject coincident centers, invalid numbers, and nonfinite output. Reset restores default fixture; repeat Step does not advance again. Preserve the supplied source text and distinguish an uncomputed state, a successful local result, and an invalid or incomplete input. Update output only from the current input snapshot. An input edit must retire stale feedback until the reader runs the relevant action again. Do not silently invent missing values or replace a failing case with a success.

Design the page as a compact source-and-result developer workbench with readable code, labeled inputs, and a separate explanation area. Put a compact task heading, actual sample data, the main result, and usable controls early enough to be useful on a narrow screen. Use readable text of at least 16px, generous paragraph spacing, strong contrast, native labels, accessible buttons, and visible keyboard focus. Stack long records at 360px and wrap code without page-level horizontal overflow.

Use type=button and explicit querySelector-based click handlers rather than relying on form submission. If Enter is useful, handle it only on the relevant input and ignore isComposing. Keep effects within this open page; no remote dependencies, network calls, persistence, fake downloads, or automated sending. The dimensionless semi-implicit Euler exercise is not an orbital predictor, collision solver, or physical validation. Place worked explanations beside their data and identify teacher or synthetic examples clearly. Output only the complete HTML document, with inline styles and script and LF line endings.

Review the mutual-attraction step calculator in NOS

Use the force and acceleration ledger to review your update order. Review the unequal accelerations beside the equal-and-opposite forces in NOS, and revise eligible static interpretation notes in visual editing mode if the two quantities are confused.

Keep reading