# MultiPendulum

> MultiPendulumMotion::new(lengths, masses, angles, seconds) solves a chain of point masses on massless rods released at rest, a double pendulum with two…

Page: https://fframes-kit.pages.dev/docs/physics/multi-pendulum/
Cargo feature: `physics` (`fframes-kit = { version = "..", features = ["physics"] }`)

`MultiPendulumMotion::new(lengths, masses, angles, seconds)` solves a chain of point masses
on massless rods released at rest, a double pendulum with two links, from the Lagrangian with
Runge-Kutta at 240 steps a second. Keep it in the video, built once. Over 10 s a chaotic
double pendulum keeps its energy within 1e-6 on the steps. Sampled between two steps, the
blended state is off by about 1e-3, which does not show. `state(t)`, `positions(t)` (meters
from the pivot, `y` up) and `energy(t)` give the numbers. `MultiPendulum` draws it from
`pivot`, `scale` units per meter. With `trail` the last bob leaves a trail, sampled like
`TracedPath`, over the last `trail_time` seconds. Gallery scenes: `MultiPendulum`, and `ChaosFan`:
24 double pendulums released 0.002 rad apart, which swing as one and then fan out.

Gallery clip of `MultiPendulum`: https://fframes-kit.pages.dev/media/scenes/MultiPendulum-77cd4fbd.mp4

Gallery clip of `ChaosFan`: https://fframes-kit.pages.dev/media/scenes/ChaosFan-9fee79a1.mp4

```rust
use fframes_kit::prelude::*;

struct Video {
    chain: MultiPendulumMotion,
}

impl Video {
    fn new() -> Self {
        Self {
            chain: MultiPendulumMotion::new(&[1.4, 1.1], &[1.0, 1.0], &[2.2, 2.6], 10.0),
        }
    }

    fn figure<'a>(&'a self, ui: &mut Ui<'a, '_, '_>) -> Svgr<'a> {
        ui.show(MultiPendulum {
            motion: Some(&self.chain),
            pivot: (0.0, 1.0),
            trail_time: Some(2.0),
            ..ui.defaults()
        })
    }
}
```

## API

Components are plain structs. Set the fields you need and fill the rest with `..ui.defaults()`: the defaults below come from the current theme (`theme.` values are fields of `Theme`).

### MultiPendulum

| Field | Type | Default | Description |
| --- | --- | --- | --- |
| `motion` | `Option<&MultiPendulumMotion>` | `None` | The simulated chain to draw, built once in the video's constructor; `None` draws nothing. |
| `pivot` | `(f64, f64)` | `(0.0, 2.5)` | Where the first rod hangs from, in units of `ui.space`. |
| `scale` | `f64` | `1.0` | Units of `ui.space` per meter. |
| `delay` | `f32` | `0.0` | Seconds after the local start before the chain is released. |
| `time_scale` | `f32` | `1.0` | Speed of the motion over local time; 2 runs twice as fast, 1 is real time. |
| `bob_radius` | `f32` | `18.0` | Radius of the bobs in pixels. |
| `trail` | `bool` | `true` | Leaves a trail behind the last bob. |
| `trail_time` | `Option<f32>` | `Some(3.0)` | Seconds of the trail that stay visible; `None` keeps all of it. |
| `color` | `Color` | `theme.colors.text` | Colour of the rods, the pivot and the ceiling. |
| `bob_color` | `Color` | `theme.colors.accent` | Colour of the bobs. |
| `trail_color` | `Color` | `theme.colors.highlight` | Colour of the trail. |
| `stroke_width` | `f32` | `theme.stroke.regular` | Width of the rods, the ceiling and the trail in pixels. |
| `enter` | `Option<EnterAnim>` | `None` | How the ceiling draws in; `None` shows it from the start. |
| `exit` | `Option<ExitAnim>` | `None` | How the ceiling leaves at the end of the container; `None` stays. |
| `parts_enter` | `Option<FadeIn>` | `None` | How the rods, bobs and trail fade in; `None` for none. |
| `parts_exit` | `Option<FadeOut>` | `None` | How the rods, bobs and trail fade out before the end; `None` for none. |

Feature `physics` · [Source](https://github.com/khanhthanhdev/fframes-kit/blob/main/fframes-kit/src/kits/physics/multi_pendulum.rs#L159)
