Skip to content

Make math primitives runtime constructable, meshable #20250

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

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

tychedelia
Copy link
Member

Objective

Many math primitives require const generics and thus are not constructable at runtime and also not meshable. While the use of const generics is theoretically more performant, it makes them very difficult to interact with in a generic way, particularly in relationship to mesh construction. For example, a ui that would allow selecting a primitive in order to create a mesh.

Solution

Make them alloc and meshable.

@tychedelia tychedelia added A-Rendering Drawing game state to the screen A-Math Fundamental domain-agnostic mathematical operations S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jul 22, 2025
@tychedelia tychedelia added C-Usability A targeted quality-of-life change that makes Bevy easier to use M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide labels Jul 22, 2025
Copy link
Contributor

It looks like your PR is a breaking change, but you didn't provide a migration guide.

Please review the instructions for writing migration guides, then expand or revise the content in the migration guides directory to reflect your changes.

Copy link
Contributor

Your PR caused a change in the graphical output of an example or rendering test. This might be intentional, but it could also mean that something broke!
You can review it at https://pixel-eagle.com/project/B04F67C0-C054-4A6F-92EC-F599FEC2FD1D?filter=PR-20250

If it's expected, please add the M-Deliberate-Rendering-Change label.

If this change seems unrelated to your PR, you can consider updating your PR to target the latest main branch, either by rebasing or merging main into it.

1 similar comment
Copy link
Contributor

Your PR caused a change in the graphical output of an example or rendering test. This might be intentional, but it could also mean that something broke!
You can review it at https://pixel-eagle.com/project/B04F67C0-C054-4A6F-92EC-F599FEC2FD1D?filter=PR-20250

If it's expected, please add the M-Deliberate-Rendering-Change label.

If this change seems unrelated to your PR, you can consider updating your PR to target the latest main branch, either by rebasing or merging main into it.

@tychedelia tychedelia added this to the 0.17 milestone Jul 23, 2025
Copy link
Contributor

Your PR caused a change in the graphical output of an example or rendering test. This might be intentional, but it could also mean that something broke!
You can review it at https://pixel-eagle.com/project/B04F67C0-C054-4A6F-92EC-F599FEC2FD1D?filter=PR-20250

If it's expected, please add the M-Deliberate-Rendering-Change label.

If this change seems unrelated to your PR, you can consider updating your PR to target the latest main branch, either by rebasing or merging main into it.

Copy link
Contributor

@Jondolf Jondolf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making this! I definitely prefer just using Vecs for the general polygon and polyline types :)

Left some thoughts on a few methods and the ConvexPolygon type

Comment on lines +1933 to +1935
pub struct ConvexPolygon {
/// The vertices of the [`ConvexPolygon`].
#[cfg_attr(feature = "serialize", serde(with = "super::serde::array"))]
vertices: [Vec2; N],
vertices: Vec<Vec2>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one I'm less sure about, since convex polygons tend to have a small number of vertices, and you can have a lot of them (ex: as a result of convex decomposition). I imagine that ConvexPolygon would primarily be used for collision detection or other geometry stuff where this can be relevant.

I wonder if the ideal representation would be to have a MAX_VERTICES const generic and to store an ArrayVec, that way you can store the vertices inline and have a cap based on how many vertices you expect to have 🤔 For example, for colliders I'd expect a max of 8 vertices per polygon. Box2D does basically the same (source).

Aside: Depending on what we intend ConvexPolygon to be used for, we might also want to store the outward edge normals here. It's required for collision detection, computing support faces, and some other stuff. For example Parry and Box2D store them. That's out of scope for this PR though.

Comment on lines +1585 to +1597
/// Create a new `Polyline2d` from two endpoints and a number of segments.
pub fn with_segment_count(start: Vec2, end: Vec2, segments: usize) -> Self {
if segments == 0 {
return Self::new(Vec::from([start, end]));
}

let step = (end - start) / segments as f32;
let mut vertices = Vec::with_capacity(segments + 1);
for i in 0..=segments {
vertices.push(start + step * i as f32);
}
Self { vertices }
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, segments == 0 and segments == 1 produce the same result here, right? Both have just the start and end point. Not sure if that's expected.

I wonder if something like subdivisions or resolution would be clearer: 0 returns the polyline as-is, 1 divides the line segment(s) through the middle, and so on. It could even be generalized to not just take two endpoints, but to divide each line segment in a given polyline into parts and to return the resulting higher res polyline.

impl BoxedPolyline2d {
/// Create a new `BoxedPolyline2d` from its vertices
impl Polyline2d {
/// Create a new `Polyline2d` from its vertices
pub fn new(vertices: impl IntoIterator<Item = Vec2>) -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these just take a Vec, considering from_iter already exists if you want to use an iterator-like?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I would "never" (say never) use Vec as an argument if I can have impl. Maybe remove from_iter instead?

If we change it, please add a comment to refer to from_iter.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from_iter here comes from the FromIterator trait, which makes sense to have implemented IMO.

I'm not sure if Rust does some special optimization here, but mainly my motivation is that from_iter does iter.into_iter().collect(), which (I believe) allocates a new vector, unless Rust is smart enough to figure out when it can just use the original vec. For most of my use cases I would instead want to move the given vector directly without doing any unnecessary allocations.

But you can also just not use the constructor at all and do Polyline2d { vertices: todo!() }, so it's not a big problem regardless

@alice-i-cecile alice-i-cecile added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jul 29, 2025
@alice-i-cecile
Copy link
Member

@tychedelia, I'm waiting on your response to feedback on this PR currently :) Once that's done, ping me for a review please.

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 A-Rendering Drawing game state to the screen C-Usability A targeted quality-of-life change that makes Bevy easier to use M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

4 participants