# Collision1D

> Carts A and B on a track that collide head on: masses m1, m2, velocities u1, u2, centers x1, x2, and a restitution from 1 (elastic) to 0 (they stick…

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

Carts A and B on a track that collide head on: masses `m1`, `m2`, velocities `u1`, `u2`,
centers `x1`, `x2`, and a `restitution` from 1 (elastic) to 0 (they stick together). The
motion is a closed form, and heavier carts are drawn longer. Each cart carries its velocity.
Momentum bars under the track show `p A`, `p B` and their sum, which stays the same through
the collision. A cart stops at the end of the track, and the end's push changes the sum
then. `collision_time`, `final_velocities`, `state` and `momenta` give the numbers. Gallery
scene: `Collision1D`.

Gallery clip of `Collision1D`: https://fframes-kit.pages.dev/media/scenes/Collision1D-68c421ce.mp4

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

fn figure<'a>(ui: &mut Ui<'a, '_, '_>) -> Svgr<'a> {
    ui.show(Collision1D {
        m1: 2.0,
        m2: 1.0,
        u1: 2.0,
        u2: -0.5,
        x1: 1.5,
        x2: 5.5,
        restitution: 1.0,
        track: (-6.0, 0.2),
        ..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`).

### Collision1D

| Field | Type | Default | Description |
| --- | --- | --- | --- |
| `m1` | `f64` | `2.0` | Mass of cart A in kg. |
| `m2` | `f64` | `1.0` | Mass of cart B in kg. |
| `u1` | `f64` | `3.0` | Velocity of cart A before the collision in m/s; positive moves right. |
| `u2` | `f64` | `-1.0` | Velocity of cart B before the collision in m/s; positive moves right. |
| `x1` | `f64` | `2.0` | Center of cart A in meters from the track's left end at the start. |
| `x2` | `f64` | `8.0` | Center of cart B in meters from the track's left end at the start. |
| `restitution` | `f64` | `1.0` | Bounciness: 1 is an elastic collision, 0 makes the carts stick together. |
| `track` | `(f64, f64)` | `(-6.0, 0.0)` | Left end of the track, in units of `ui.space`. |
| `track_length` | `f64` | `12.0` | Length of the track in meters; a cart stops at its end. |
| `scale` | `f64` | `1.0` | Units of `ui.space` per meter. |
| `delay` | `f32` | `0.0` | Seconds after the container's start before the carts start moving. |
| `time_scale` | `f32` | `1.0` | Speed of the motion over local time; 2 runs twice as fast, 1 is real time. |
| `vectors` | `bool` | `true` | Shows each cart's velocity as an arrow with its value in m/s. |
| `momentum` | `bool` | `true` | Shows bars under the track for both momenta and their sum. |
| `bar_scale` | `f32` | `40.0` | Pixels of bar per kg·m/s. |
| `color_a` | `Color` | `theme.colors.accent` | Colour of cart A, its velocity and its momentum bar. |
| `color_b` | `Color` | `theme.colors.highlight` | Colour of cart B, its velocity and its momentum bar. |
| `total_color` | `Color` | `theme.colors.text` | Colour of the total momentum label. |
| `track_color` | `Color` | `theme.colors.muted` | Colour of the track, the wheels and the momentum axis. |
| `stroke_width` | `f32` | `theme.stroke.regular` | Width of the track and the velocity arrows in pixels. |
| `enter` | `Option<EnterAnim>` | `None` | How the track draws in; `None` shows it from the start. |
| `exit` | `Option<ExitAnim>` | `None` | How the track leaves at the end of the container; `None` stays. |
| `parts_enter` | `Option<FadeIn>` | `None` | How the carts and bars fade in; `None` for none. |
| `parts_exit` | `Option<FadeOut>` | `None` | How the carts and bars fade out before the end; `None` for none. |

Methods:

- `fn cart_length(mass: f64) -> f64`: Meters long, a cart of `mass` kg: heavier carts are longer.
- `fn collision_time(&self) -> Option<f64>`: Seconds until the carts touch, or `None` when they never close in.
- `fn final_velocities(&self) -> (f64, f64)`: The velocities after the collision, from momentum and the restitution.
- `fn state(&self, t: f64) -> [(f64, f64); 2]`: Each cart's center in meters and velocity in m/s after `t` seconds.
- `fn momenta(&self, t: f64) -> (f64, f64)`: The momenta of A and B in kg·m/s after `t` seconds.

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