Skip to content

Conversation

@uckelman
Copy link
Contributor

@uckelman uckelman commented Dec 10, 2025

Make Gizmos::rect_2d and primitive_2d draw the joint of the first corner of closed shapes

Objective

Solution

  • Gizmos::rect_2d and primitive_2d draw using Gizmos::linestrip_2d, which doesn't draw a closed shape.
  • In order to make linestrip_2d to draw a joint at a corner, that corner has to be in the points list with a predecessor and a successor.
  • There is no predecessor for the first element in the list, and no successor when it is repeated as the last.
  • Appending the second point to the list is enough to put the first point between two other points, which causes the joint to be rendered.
  • I added a Gizmos::lineloop_2d function which handles the final joint for all closed shapes.
  • Draw closed shapes with the lineloop_2d function, rather than manually adding closing vertices in each case.

Testing

  • I tested my change against this demo:
use bevy::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, setup)
        .add_systems(Update, draw_shapes)
        .run();
}

fn setup(
    mut config_store: ResMut<GizmoConfigStore>,
    mut commands: Commands
)
{
    let (config, _) = config_store.config_mut::<DefaultGizmoConfigGroup>();
    config.line.width = 10.0;
    config.line.joints = GizmoLineJoint::Miter;

    commands.spawn(Camera2d);
}

fn draw_shapes(mut gizmos: Gizmos) {
    gizmos.rect_2d(
        Vec2::new(-200.0, 0.0),
        Vec2::new(50.0, 50.0),
        Color::srgb_u8(0xFF, 0, 0)
    );

    gizmos.primitive_2d(
        &Rhombus { half_diagonals: Vec2::new(20.0, 35.0) },
        Vec2::new(-100.0, 0.0),
        Color::srgb_u8(0xFF, 0, 0)
    );

    gizmos.primitive_2d(
        &Triangle2d { vertices: [Vec2::ZERO, Vec2::new(50.0, 0.), Vec2::new(0.0, 60.0) ] },
        Vec2::new(-20.0, -30.0),
        Color::srgb_u8(0xFF, 0, 0)
    );

    gizmos.primitive_2d(
        &Rectangle { half_size: Vec2::new(25.0, 25.0) },
        Vec2::new(100.0, 0.0),
        Color::srgb_u8(0xFF, 0, 0)
    );

     gizmos.primitive_2d(
        &RegularPolygon { sides: 5, circumcircle: Circle::new(40.0) },
        Vec2::new(200.0, 0.0),
        Color::srgb_u8(0xFF, 0, 0)
    );

    gizmos.primitive_2d(
        &Polygon { vertices: vec![Vec2::ZERO, Vec2::new(90.0, 0.), Vec2::new(75.0, 60.0), Vec2::new(15.0, 60.0) ] },
        Vec2::new(275.0, -30.0),
        Color::srgb_u8(0xFF, 0, 0)
    );
}

Showcase

Before:

Image

After:

Image

@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior A-Gizmos Visual editor and debug gizmos D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Dec 10, 2025
@alice-i-cecile
Copy link
Member

Once this is merged, we should make a follow-up issue to remove the overdraw.

@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 Dec 10, 2025
  * Gizmos.rect_2d
  * GizmosPrimitive2d<T>::primitive_2d for
    * Triangle2d
    * Rhombus
    * Rectangle,
    * Polygon
    * RegularPolygon

now use lineloop_2d instead of linestrip_2d.
drawing the first segment a second time. (Joints are drawn only for
corners that have segments before *and* after them. The first time the
first corner appears, it has no segment before it.)
auto-merge was automatically disabled December 10, 2025 22:04

Head branch was pushed to by a user without write access

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Dec 11, 2025
Merged via the queue into bevyengine:main with commit c7b752a Dec 11, 2025
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Gizmos Visual editor and debug gizmos C-Bug An unexpected or incorrect behavior D-Straightforward Simple bug fixes and API improvements, docs, test and examples 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.

Add explicit gizmo mechanism for drawing closed shapes

4 participants