Crate bevy_sepax2d

source ·
Expand description

Bevy tracking Crates.io MIT/Apache 2.0

§bevy_sepax2d

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

§Compatible Versions

bevybevy_sepax2d
0.120.5
0.90.4
0.80.2, 0.3
0.70.1

§Usage

Add the following to the [dependencies] section of your Cargo.toml:

sepax2d = "0.3"
bevy_sepax2d = "0.5"

There is an additional debug feature which can be used to render collision shapes to the screen. This relies on Bevy’s default features as well as bevy_prototype_lyon for rendering. This can be enabled in your Cargo.toml:

bevy_sepax2d = { version = "0.5", features = ["debug"] }

Documentation for version 0.4: check the examples for code working with 0.5 (Bevy 0.12)

To add a shape to your world, simply insert a Sepax struct into any entity.

use bevy::prelude::*;
use sepax2d::prelude::*;
use bevy_sepax2d::prelude::*;
 
fn spawn_system(mut commands: Commands)
{
 
    let polygon = Polygon::from_vertices((0.0, 0.0), vec![(0.0, -25.0), (15.0, 15.0), (-15.0, 15.0)]);
    let convex = Convex::Polygon(polygon);
 
    commands.spawn(Sepax { convex });
 
}

Sepax has one field, convex: This is an instance of the Convex enum, which has possible values for each shape supported by sepax2d: Polygon, Circle, AABB, Parallelogram, and Capsule. Each variant contains an instance of the corresponding shape.

The underlying shape can be conveniently accessed through the shape and shape_mut methods, which provide easy access to references to the underlying shapes without need to match the enum.

use bevy::prelude::*;
use sepax2d::prelude::*;
use bevy_sepax2d::prelude::*;
 
#[derive(Component)]
struct Bullet;
 
fn bullet_system
(
    mut bullets: Query<(&Bullet, &Sepax)>,
    targets: Query<&Sepax, Without<Bullet>>
)
{
    for (_b, bullet) in bullets.iter()
   {
        for target in targets.iter()
        {
            if sat_overlap(target.shape(), bullet.shape())
            {
                //Bullet hit target, now react appropriately
            }
        }
    }
}

§Features

debug - Enables rendering of shapes.

serde - Enables (De)Serialization of Convex and Sepax types for easy loading.

Modules§

Enums§

  • An enum for the different types of shapes supported by sepax2d. For most use cases, you will store a Convex inside of a Sepax and will be able to use the shape method to avoid matching the enum directly. For use cases where your behavior depends on the type of shape, then you will need to use match.