Nilirad / bevy_prototype_lyon

Draw 2D shapes in Bevy
Apache License 2.0
702 stars 87 forks source link

Fix rectangle coordinates #182

Closed rparrett closed 2 years ago

rparrett commented 2 years ago

Fixes #179

The old Rect was defined by an origin and a size, but Box2d is defined by its minimums and maximums.

rparrett commented 2 years ago

Verified that the behavior is the same as v0.6.0 with the following example code. Same code runs on bevy 0.8 and 0.9.

use bevy::prelude::*;
use bevy_prototype_lyon::prelude::*;

fn main() {
    App::new()
        .insert_resource(Msaa { samples: 4 })
        .add_plugins(DefaultPlugins)
        .add_plugin(ShapePlugin)
        .add_startup_system(setup_system)
        .run();
}

const X_EXTENT: f32 = 1100.;

fn setup_system(mut commands: Commands) {
    commands.spawn_bundle(Camera2dBundle::default());

    let circle = shapes::Circle {
        radius: 5.,
        ..default()
    };

    let extents = Vec2::new(100.0, 100.0);

    let shapes = [
        shapes::Rectangle {
            origin: RectangleOrigin::Center,
            extents,
        },
        shapes::Rectangle {
            origin: RectangleOrigin::BottomLeft,
            extents,
        },
        shapes::Rectangle {
            origin: RectangleOrigin::BottomRight,
            extents,
        },
        shapes::Rectangle {
            origin: RectangleOrigin::TopLeft,
            extents,
        },
        shapes::Rectangle {
            origin: RectangleOrigin::TopRight,
            extents,
        },
        shapes::Rectangle {
            origin: RectangleOrigin::CustomCenter(Vec2::new(25., -25.)),
            extents,
        },
    ];

    let num_shapes = shapes.len();

    for (i, shape) in shapes.into_iter().enumerate() {
        commands.spawn_bundle(GeometryBuilder::build_as(
            &shape,
            DrawMode::Fill(FillMode::color(Color::RED)),
            Transform::from_xyz(
                -X_EXTENT / 2. + i as f32 / (num_shapes - 1) as f32 * X_EXTENT,
                2.0,
                0.0,
            ),
        ));
        commands.spawn_bundle(GeometryBuilder::build_as(
            &circle,
            DrawMode::Fill(FillMode::color(Color::BLACK)),
            Transform::from_xyz(
                -X_EXTENT / 2. + i as f32 / (num_shapes - 1) as f32 * X_EXTENT,
                2.0,
                0.0,
            ),
        ));
    }
}
image
rparrett commented 2 years ago

Oops, closing in favor of #180 which is a much nicer solution.

Nilirad commented 2 years ago

You forgot to close 😄

rparrett commented 2 years ago

Thanks!