36 lines
1.2 KiB
Markdown
36 lines
1.2 KiB
Markdown
# Step 03 — Integration (moving over time)
|
|
|
|
## Concept
|
|
|
|
"Integration" sounds like calculus, but the version we need is one line. An
|
|
object has a **position** and a **velocity**. Each frame we advance the position
|
|
by the velocity, scaled by how much time passed:
|
|
|
|
```
|
|
newPosition = position + velocity * delta
|
|
```
|
|
|
|
That's `add(pos, scale(vel, delta))`. This single step is the beating heart of
|
|
every game's update loop.
|
|
|
|
### Why `delta`?
|
|
|
|
`delta` is the number of **milliseconds since the last frame**. Frames are not
|
|
evenly spaced — a busy frame takes longer. If you moved a fixed amount *per
|
|
frame* instead of *per millisecond*, your game would run faster on a fast
|
|
computer and slower on a slow one.
|
|
|
|
By storing velocity as **units-per-millisecond** and multiplying by `delta`, the
|
|
distance travelled over a given stretch of real time is the same no matter how
|
|
the frames are chopped up. That property is called **framerate independence**,
|
|
and one of the tests below proves it: moving once with `delta = 16` lands in the
|
|
same place as moving sixteen times with `delta = 1`.
|
|
|
|
## Task
|
|
|
|
Implement `integrate(pos, vel, delta)` in `motion.ts`.
|
|
|
|
```sh
|
|
bun test workshop/steps/03-integration
|
|
```
|