Helper functions and structs for working with 2D space in Bevy.

Overview

About

Baffled by quaternions? Want to accelerate an object in 2D? Wish that there was a simple way to work with grids? Just want to know if two axis-aligned bounding boxes intersect?

Try out leafwing_2d: a flexible, ergonomics-first solution!

Features:

  • Flexible Position<C: Coordinate> type for working with 2D coordinates
    • Use the DiscreteCoordinate trait for important grid-based tasks like finding neighbours
  • Say goodbye to quaternions: use the Direction (unit vector) and Rotation (angle from midnight) types instead!
    • Jam-packed with powerful, tested convenience methods like Orientation::orientation_to and Rotation::towards
    • Slice and dice the unit circle into cardinal, hexagonal or octagonal directions with the DirectionPartitioning trait
  • Simply scale your sprites with the LeafwingSpriteBundle and the Scale type.
    • Perform basic collision checks using the AxisAlignedBoundingBox type.
  • Use TwoDPlugin to automatically synchronize your Transforms with Position, Direction and Rotation, but modify whichever one you'd like
  • Accelerate your game creation with Velocity<C>, Acceleration<C> and their angular analogues
  • Convert to and from screen space in whatever coordinate system you want using the Positionlike trait
  • Made with Leafwing Studios' trademark #![forbid(missing_docs)]

Instructions

Getting started

  1. Add leafwing_2d to your Cargo.toml.
  2. Pick a coordinate type C.
    1. Any of the built-in float (e.g. f32) or integer (e.g. u8 or i64) types work.
    2. Or try out our DiscreteCoordinate types like OrthogonalGrid)!
  3. Add the TwoDBundle bundle to your entities, or toss on a Position, Direction or Rotation component.
  4. Add TwoDPlugin to your App to synchronize these easy-to-work with 2D geometry types with Bevy's Transform.

Running examples

To run an example, use cargo run --example_name, where example_name is the file name of the example without the .rs extension.

Contributing

This repository is open to community contributions! There are a few options if you'd like to help:

  1. File issues for bugs you find or new features you'd like.
  2. Read over and discuss issues, then make a PR that fixes them. Use "Fixes #X" in your PR description to automatically close the issue when the PR is merged.
  3. Review existing PRs, and leave thoughtful feedback. If you think a PR is ready to merge, hit "Approve" in your review!

Any contributions made are provided under the license(s) listed in this repo at the time of their contribution, and do not require separate attribution.

Testing

  1. Use doc tests aggressively to show how APIs should be used. You can use # to hide a setup line from the doc tests.
  2. Unit test belong near the code they are testing. Use #[cfg(test)] on the test module to ignore it during builds, and #[test] on the test functions to ensure they are run.
  3. Integration tests should be stored in the top level tests folder, importing functions from lib.rs.

Use cargo test to run all tests.

CI

The CI will:

  1. Ensure the code is formatted with cargo fmt.
  2. Ensure that the code compiles.
  3. Ensure that (almost) all clippy lints pass.
  4. Ensure all tests pass on Windows, MacOS and Ubuntu.

Check this locally with:

  1. cargo run -p ci
  2. cargo test --workspace

To manually rerun CI:

  1. Navigate to the Actions tab.
  2. Use the dropdown menu in the CI run of interest and select "View workflow file".
  3. In the top-right corner, select "Rerun workflow".

Documentation

Reference documentation is handled with standard Rust doc strings. Use cargo doc --open to build and then open the docs.

Design docs (or other book-format documentation) is handled with mdBook. Install it with cargo install mdbook, then use mdbook serve --open to launch the docs.

You might also like...
bevy-hikari is an implementation of voxel cone tracing global illumination with anisotropic mip-mapping in Bevy
bevy-hikari is an implementation of voxel cone tracing global illumination with anisotropic mip-mapping in Bevy

Bevy Voxel Cone Tracing bevy-hikari is an implementation of voxel cone tracing global illumination with anisotropic mip-mapping in Bevy. Bevy Version

A simple extension for `bevy-editor-pls` to support tilemap editing right inside the bevy app.

What is this This is a simple tilemap editor plugin, that hooks right into bevy_editor_pls to work with bevy_ecs_tilemap. It works completely within i

Bevy Simple Portals is a Bevy game engine plugin aimed to create portals.
Bevy Simple Portals is a Bevy game engine plugin aimed to create portals.

Portals for Bevy Bevy Simple Portals is a Bevy game engine plugin aimed to create portals. Those portals are (for now) purely visual and can be used t

Rust SDK for working with RIS-Live real-time BGP data stream.
Rust SDK for working with RIS-Live real-time BGP data stream.

ris-live-rs Provides parsing functions for RIS-Live real-time BGP message stream JSON data. The main parsing function, parse_ris_live_message converts

Coordination repository of the Game Development Working Group

Rust Game Development Working Group 🕹️ The game development working group's main purpose is to make Rust a first-class option for game developers. Wh

Tools for working with Retro game formats. Currently only supports Metroid Prime Remastered.

retrotool Warning Under active development, not guaranteed to be useful or even function. Tools for working with Retro game formats. Currently only su

Plugins and helpful methods for using sepax2d with Bevy for 2d overlap detection and collision resolution.

bevy_sepax2d Plugins and helpful methods for using sepax2d with Bevy for 2d overlap detection and collision resolution. Compatible Versions bevy bevy_

Pixel-Perfect, 2D Renderer for Bevy that Seamlessly Targets Desktop and Web
Pixel-Perfect, 2D Renderer for Bevy that Seamlessly Targets Desktop and Web

bevy_retro ( Screenshot of Bounty Bros. game made with Bevy Retro and Skip'n Go ) Bevy Retro is a 2D, pixel-perfect renderer for Bevy that can target

Bevy plugin helping with asset loading and organisation

Bevy asset loader This Bevy plugin reduces boilerplate when loading game assets. The crate offers the AssetCollection trait and can automatically load

Comments
  • Rename from/into_xy to from/into_vec2

    Rename from/into_xy to from/into_vec2

    This brings these more inline with the equivalent into_vec2 methods on other types, and makes the return value of the function more clear.

    I also updated the doc comments to be a bit more clear and to reflect the change.

    ⚠️ Breaking Changes

    Every change made in this PR is breaking. Two publicly exposed methods are renamed, though their parameter and return types remain unaltered.

    opened by alterae 0
  • Bump Bevy version to 0.7

    Bump Bevy version to 0.7

    What was the problem?

    Crate was depending on Bevy 0.6, rendering it incompatible with projects using Bevy 0.7.

    How did you fix it?

    Bumped Bevy dependency to version 0.7.

    opened by alterae 0
  • Investigate the max safe storable integer size for discrete coordinates

    Investigate the max safe storable integer size for discrete coordinates

    Needs to be able to be converted losslessly to and from f32. Once this is established, swap Coordinate bound to simple From<f32> and remove FloatCoordinateConversionError.

    bug code-quality 
    opened by alice-i-cecile 0
  • Add oriented bounding box and convex hull types

    Add oriented bounding box and convex hull types

    /// A 2-dimensional oriented bounding box with coordinate type C
    #[derive(Debug, Component, Clone, PartialEq)]
    pub struct OrientedBoundingBox<C: Coordinate> {
        center: Position<C>,
        local_x: Direction,
        local_y: Direction,
        halfwidth_extent_x: C,
        halfwidth_extent_y: C,
    }
    
    /// A 2-dimensional convex hull with coordinate type C
    #[derive(Debug, Component, Clone)]
    pub struct ConvexHull<C: Coordinate> {
        /// The points which define a convex region
        pub hull_points: Vec<Position<C>>,
    }
    
    enhancement 
    opened by alice-i-cecile 0
Owner
Leafwing Studios
Leafwing Studios
A simple helper for spawning objects in Bevy.

Spew A simple helper for spawning objects in Bevy. Usage First, create an enum that holds objects you might want to spawn: #[derive(Debug, Eq, Partial

Jan Hohenheim 6 Mar 20, 2023
A Bevy helper to easily manage resources that need to persist across game sessions.

bevy-persistent A Bevy helper to easily manage resources that need to persist across game sessions. Background In games, there are a lot of resources

Umut 5 Mar 25, 2023
A simple helper to run cronjobs (at repeated schedule) in Bevy.

bevy_cronjob bevy_cronjob is a simple helper to run cronjobs (at repeated schedule) in Bevy. Usage use std::time::Duration; use bevy::{ MinimalPlugins

ZoOL 5 Aug 5, 2023
A quick and dirty Space Invaders type game in Bevy, with attached tutorial.

This article is in-development and will be released in full form soon. It'll appear on Medium (my publisher likes that), with this as a the accompanyi

Herbert 17 Oct 18, 2022
Windows game hack helper utilities in rust.

⚗️ toy-arms Windows game hack helper utilities in rust. This crate has some useful macros, functions and traits. ?? How to use this crate? With this c

s3pt3mb3r 100 Jan 1, 2023
Minecraft using Bevy and Bevy-Meshem

minecraft_bevy Minecraft_bevy was built to showcase bevy_meshem. After a week of developing it has: Chunk loading / unloading each chunk's mesh is bei

Adam 7 Oct 4, 2023
A space-themed shoot 'em up game.

Zenith A space-themed shoot 'em up game made with Bevy. Building from source After you clone this repository, ensure you have Rust installed. Dependin

Amelia Clarke 22 Dec 15, 2022
Find out what takes most of the space in your executable.

cargo-bloat Find out what takes most of the space in your executable. Supports ELF (Linux, BSD), Mach-O (macOS) and PE (Windows) binaries. WASM is not

Yevhenii Reizner 1.7k Jan 4, 2023
A simple space shooter game. Runs in the terminal using characters-based UI. Fully written in Rust, using the "ruscii" library.

Thrust - a terminal shooter game Originally created as a project for the "Missing Semester" course at JKU Linz (338.006). The game is entirely written

Mathias Wöß 3 Jan 16, 2023
Minecraft-esque voxel engine prototype made with the bevy game engine. Pending bevy 0.6 release to undergo a full rewrite.

vx_bevy A voxel engine prototype made using the Bevy game engine. Goals and features Very basic worldgen Animated chunk loading (ala cube world) Optim

Lucas Arriesse 125 Dec 31, 2022