# The Pull of One Star

*Task 1 of 5 · [N-Body Gravity](https://gpu.rocks/learn/n-body-gravity-5de47751.md) · GPU.js Learn*

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
**acceleration** — `a = 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

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

## Requirements

- Use the `dx`, `dy` offsets to the star (already wired up)
- Compute the squared distance: `r² = dx·dx + dy·dy`
- Return `starMass / r²` — inverse-square, with `G = 1`

## Hint 1 — no square root needed

The law wants `r²`, and `dx*dx + dy*dy` *is*
`r²`. 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.

## Starter code

```js
// 64 bodies, one star. Each thread owns one body and asks:
// how hard does the star pull on ME?
const gpu = new GPU({ mode });

const pull = gpu.createKernel(function (posX, posY, starX, starY, starMass) {
  const dx = starX - posX[this.thread.x];
  const dy = starY - posY[this.thread.x];
  // TODO: inverse-square law — return starMass / r²,
  // where r² = dx·dx + dy·dy. (G = 1 here.)
  return 0;
}, { output: [64] });

const strength = await pull(posX, posY, 0, 0, 100);
console.log('pull on body 0:', strength[0]);
```

---

Interactive version: https://gpu.rocks/learn/n-body-gravity-5de47751/1

[Next task](https://gpu.rocks/learn/n-body-gravity-5de47751/2.md)
