Skip to content

Improve Frustum struct readability#21508

Merged
alice-i-cecile merged 4 commits into
bevyengine:mainfrom
Breakdown-Dog:frustum_improve_readability
Oct 29, 2025
Merged

Improve Frustum struct readability#21508
alice-i-cecile merged 4 commits into
bevyengine:mainfrom
Breakdown-Dog:frustum_improve_readability

Conversation

@Breakdown-Dog

Copy link
Copy Markdown
Contributor

Objective

  • This PR refactors the Frustum struct to improve code readability and maintainability by replacing magic numbers with named constants and unrolling a loop for clarity.

Testing

  • I ran the command 'cargo test --package bevy_camera --lib -- primitives::tests --show-output' and all the test are passed.

@janis-bhm janis-bhm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this makes this section much more readable!

Comment thread crates/bevy_camera/src/primitives.rs Outdated
let row2 = clip_from_world.row(2);
let row3 = clip_from_world.row(3);

half_spaces[Self::LEFT_PLANE_IDX] = HalfSpace::new(row3 + row0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this section could also be written as a simple array instead of assigning each element individually.
i.e. Self { half_spaces: [HalfSpace::new(row3 + row0), ..] }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think what you said makes sense. We don't even need to declare a  mut   half_space  in advance and can directly return the value. However, some constants seem a bit redundant—I don't see where these constants are needed externally either.

Comment thread crates/bevy_camera/src/primitives.rs Outdated
const NEAR_PLANE_IDX: usize = 4;
const FAR_PLANE_IDX: usize = 5;

const INACTIVE_HALF_SPACE: Vec4 = Vec4::new(0.0, 0.0, 0.0, f32::MAX);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is copied from the original code, but I'm wondering if it's correct to have a halfspace of NaNs here?

Comment thread crates/bevy_camera/src/primitives.rs Outdated
Comment on lines +278 to +283
const LEFT_PLANE_IDX: usize = 0;
const RIGHT_PLANE_IDX: usize = 1;
const BOTTOM_PLANE_IDX: usize = 2;
const TOP_PLANE_IDX: usize = 3;
const NEAR_PLANE_IDX: usize = 4;
const FAR_PLANE_IDX: usize = 5;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These could be pub, since half_spaces is pub as well, and is used outside of this module.

@janis-bhm janis-bhm added D-Trivial Nice and easy! A great choice to get started with Bevy C-Code-Quality A section of code that is hard to understand or change S-Needs-Review Needs reviewer attention (from anyone!) to move forward A-Camera User-facing camera APIs and controllers. labels Oct 11, 2025
@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Oct 27, 2025
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Oct 29, 2025
Merged via the queue into bevyengine:main with commit 2b74e52 Oct 29, 2025
38 checks passed
@Breakdown-Dog Breakdown-Dog deleted the frustum_improve_readability branch November 16, 2025 08:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Camera User-facing camera APIs and controllers. C-Code-Quality A section of code that is hard to understand or change D-Trivial Nice and easy! A great choice to get started with Bevy S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants