Task 1 of 5

The Pull of One Star

Newton, in one line: the gravitational pull between two bodies is G · m₁ · m₂ / r². Divide out the mass being pulled and you get its accelerationa = G · M / r² — which only depends on the other body. In this course G = 1 (astrophysicists rescale units to do exactly this, so you're in good company).

Here 64 bodies drift around one star. Each thread owns one body — its position is posX[this.thread.x], posY[this.thread.x] — and answers a single question: how hard does the star pull on me? No loops yet; that's next.

Goal: make the kernel return the strength of the star's pull on this thread's body: starMass / r².

Requirements

Hint 1 — no square root needed

The law wants , and dx*dx + dy*dy is . Taking Math.sqrt just to square it again is the most popular way to waste GPU cycles.

Hint 2 — the one-liner

return starMass / (dx * dx + dy * dy);

Same idea elsewhere

One-thread-per-body is the opening move of GPU physics everywhere: the CUDA SDK's classic nbody sample assigns body i to thread i exactly like this, and its HIP port runs the identical mapping on ROCm.

All tasks in N-Body Gravity

  1. The Pull of One Star
  2. Every Body Pulls on Every Body
  3. Softening the Singularity
  4. One Tick of the Clock
  5. Put It Together: 128 Bodies

This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.