Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CubicCurve::default().position() causes panic: "attempt to subtract with overflow" - Default should work for this. #11209

Closed
laundmo opened this issue Jan 4, 2024 · 0 comments · Fixed by #11335
Labels
A-Math Fundamental domain-agnostic mathematical operations C-Bug An unexpected or incorrect behavior D-Trivial Nice and easy! A great choice to get started with Bevy

Comments

@laundmo
Copy link
Contributor

laundmo commented Jan 4, 2024

What problem does this solve or what need does it fill?

Currently, unintuitively, when spawning for example a Component with Default which contains a CubicCurve, other code relying on this component before it was updated crashes.

quick reproduction:

let c = CubicCurve::default();
let p: Vec2 = c.position(0.1);

What solution would you like?

The common way default works is by using zeros (like on Vec2/3). I believe a custom Default impl on CubicCurve which does the same to prevent this issue makes sense.

What alternative(s) have you considered?

instead of panicking, a lot of methods could return Option<>. But this is annoying to deal with in game code.

Additional context

@laundmo laundmo added C-Feature A new feature, making something new possible S-Needs-Triage This issue needs to be labelled labels Jan 4, 2024
@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior D-Trivial Nice and easy! A great choice to get started with Bevy A-Math Fundamental domain-agnostic mathematical operations and removed C-Feature A new feature, making something new possible S-Needs-Triage This issue needs to be labelled labels Jan 4, 2024
github-merge-queue bot pushed a commit that referenced this issue Jan 14, 2024
# Objective

- Implementing `Default` for
[`CubicCurve`](https://docs.rs/bevy/latest/bevy/math/cubic_splines/struct.CubicCurve.html)
does not make sense because it cannot be mutated after creation.
- Closes #11209.
- Alternative to #11211.

## Solution

- Remove `Default` from `CubicCurve`'s derive statement.

Based off of @mockersf comment
(#11211 (comment)):

> CubicCurve can't be updated once created... I would prefer to remove
the Default impl as it doesn't make sense

---

## Changelog

- Removed the `Default` implementation for `CubicCurve`.

## Migration Guide

- Remove `CubicCurve` from any structs that implement `Default`.
- Wrap `CubicCurve` in a new type and provide your own default.

```rust
#[derive(Deref)]
struct MyCubicCurve<P: Point>(pub CubicCurve<P>);

impl Default for MyCubicCurve<Vec2> {
    fn default() -> Self {
        let points = [[
            vec2(-1.0, -20.0),
            vec2(3.0, 2.0),
            vec2(5.0, 3.0),
            vec2(9.0, 8.0),
        ]];

        Self(CubicBezier::new(points).to_curve())
    }
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Math Fundamental domain-agnostic mathematical operations C-Bug An unexpected or incorrect behavior D-Trivial Nice and easy! A great choice to get started with Bevy
Projects
None yet
2 participants