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

Overview

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

bevy bevy_sepax2d
0.7 0.1

Usage

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

sepax2d = "0.3"
bevy_sepax2d = "0.1"

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.1", features = ["debug"] }

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

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()
.insert(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, and Capsule. Each variant contains an instance of the corresponding shape. Check the sepax2d documentation for information about each one.

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.

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(wall.shape(), bullet.shape())
            {
                //Bullet hit target, now react appropriately
            }
        }
    }
}

If included, the SepaxPlugin provides the following basic features, which occur during Bevy's PostUpdate stage:

  • Resets the collision information from the previous frame
  • Updates the location of any Sepax component attached to a Transform
  • Provides inelastic collision between entities with a Sepax shape which are tagged Movable and those that are not movable.

These systems are public, so you may include them manually if you do not want all of them. This is likely to happen when you want to introduce finer control over which objects collide with which, but still want to reset collision data and update locations. Or, you may want to collide and update locations, but want to maintain old collision data until it has been processed.

The Movable component signifies that an entity is dynamic. By contrast, the absence of a Movable component denotes a static object which is treated like a "wall" that movable entities should collide with. Use the NoCollision marker component on either a Movable or non-Movable entity to exclude it from the collision process.

The Movable struct contains a list of normalized collision resolution vectors from the previous frame during the Update stage for you to react to in your code. These vectors represent the direction AWAY from the object that was collided with. For example, the following code zeroes out the y-component of an entity's velocity when it lands on or hits the bottom of a platform:

fn velocity_correction_system(mut query: Query<(&mut Velocity, &Movable)>)
{
    for (mut velocity, correction) in query.iter_mut()
    {
        for (_x, y) in correction.axes.iter()
        {
            if y.abs() > f32::EPSILON && velocity.y.is_sign_positive() != y.is_sign_positive()
            {
                velocity.y = 0.0;
            }
        }
    }
}

Debug Rendering

If you enable the debug feature, then you can render your shapes with the help of bevy_prototype_lyon. The following convenience methods help with rendering:

  • Sepax::as_shape_bundle will take in a reference to a Sepax struct and a DrawMode and return a ShapeBundle to be added to an entity.
  • Sepax::shape_geometry will take in a reference to a Sepax struct and return a Path representing the given shape. Use this in conjunction with ShapePath to change the rendered shape at runtime. Check out the platformer example to see this in action!
let circle = Circle::new((0.0, 0.0), 15.0);
let convex = Convex::Circle(circle);

let player = DrawMode::Fill(FillMode::color(Color::WHITE));

commands.spawn()
.insert_bundle(Sepax::as_shape_bundle(&convex, player))
.insert(Sepax { convex })
.insert(Movable { axes: Vec::new() });

Features

debug - Enables rendering of shapes.

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

Examples

The repository includes two example applications showcasing a basic platformer (which only uses the basic plugin), and a shmup which demonstrates some custom systems.

cargo run --features="debug" --example platformer

cargo run --features="debug" --example shmup

Contribution

Please feel free to suggest additional features, bug fixes, or optimizations. Thanks!

You might also like...
A game of snake written in Rust using the Bevy game engine, targeting WebGL2

Snake using the Bevy Game Engine Prerequisites cargo install cargo-make Build and serve WASM version Set your local ip address in Makefile.toml (loca

Simple island generator written in rust using bevy engine
Simple island generator written in rust using bevy engine

Getting Started Easy enough to run cargo run --release Change generation speed Find the system set that looks like this .add_system_set(

Brine is my attempt at writing a Minecraft client in Rust using the Bevy game engine.
Brine is my attempt at writing a Minecraft client in Rust using the Bevy game engine.

Brine Brine is my attempt at writing a Minecraft client in Rust using the Bevy game engine. It's EXTREMELY work-in-progress. The thing that makes Brin

Wheeled vehicle simulation using Bevy engine with Rapier's joints
Wheeled vehicle simulation using Bevy engine with Rapier's joints

⚠️ Legacy branch! ⚠️ 'master' branch is currently broken due to migration to newer rapier and bevy_rapier so unless you want to compare old and new ve

A crate for using Bevy with the Godot Engine.

bevy_godot A crate for using Bevy with the Godot Engine. This crate is in active development and is not ready for production use. Features Godot Scene

A Client/Server game networking plugin using QUIC, for the Bevy game engine.
A Client/Server game networking plugin using QUIC, for the Bevy game engine.

Bevy Quinnet A Client/Server game networking plugin using QUIC, for the Bevy game engine. Bevy Quinnet QUIC as a game networking protocol Features Roa

🌎 Demo for Minecraft-like procedural generation using the Bevy game engine
🌎 Demo for Minecraft-like procedural generation using the Bevy game engine

infinigen This is a demo for Minecraft-like procedural generation using the Bevy game engine. chunks along all axes (X, Y and Z) adjustable zoom level

Example implementation of a 3d hexagon tile-based map editor using bevy

bevy-hex-sandbox Example implementation of a 3d hexagon tile-based map editor using bevy v0.10. smol.mov This is not a maintained project. Instead, th

Rust boids simulation using Reynolds model running with Bevy engine.

example.mp4 About Rust boids simulation using Reynolds model running with Bevy engine. The following parameters can be updated while the simulation is

Comments
  • Heron/Rapier-style ShapeCasting

    Heron/Rapier-style ShapeCasting

    I'm looking into moving my bevy platformer game from heron to bevy_sepax2d, however from my current understanding sepax is currenly missing some convenient funcitonality. What I'm looking for is a simple way to do a shapecast using an entity's collider, apply masks and groups, and if the collision is hit check the exact position where the collision happened. I'm a beginner bevy/rust user so it's possible that sepax already allows all of this and I'm just not understanding how to do this in sepax. Sorry if that's the case! I know casting rays was added in the recent update but it seems to be something slightly different than what I'm looking for / missing some funcitonality?

    Example of how I'm currently doing this in heron: let hit = physics_world.shape_cast_with_filter( shape: collider, start_position: transform.translation, start_rotation: transform.rotation, ray: velocity_vector, layers: CollisionLayers::none() .with_group(ColliderTypes::Player) .with_mask(ColliderTypes::Wall), filter: |_enityty| true, );

    opened by cvvv24 1
Owner
null
An asset that provides 2d collision detector and kinematics, build from scratch in bevy

Busturi A physics engine An asset that provides 2d collision detector and kinematics, build from scratch in bevy How to use Add PhysicsPlugin to the p

NemuiSen 2 Jun 22, 2022
A simple camera for properly displaying tile-based low resolution pixel perfect 2D games in bevy.

Bevy Tiled Camera A simple camera for properly displaying low resolution pixel perfect 2D games in bevy. The camera will adjust the viewport to scale

sark 10 Oct 5, 2022
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
Using bevy and custom render pipelines in order to render many objects in a forest using chunks for performance.

bevy_efficient_forest_example Using bevy and custom render pipelines in order to render many objects in a forest using chunks for performance. Grass i

Henrik Djurestål 43 Jan 5, 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
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

研究社交 208 Dec 27, 2022
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

null 3 May 8, 2023
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

Sélène Amanita 11 May 28, 2023
An adventure survival voxel game written using Bevy and Rust.

An adventure survival voxel game written using Bevy and Rust.

Rigidity 3 Jun 4, 2022
Proof-of-concept of getting OpenXR rendering support for Bevy game engine using gfx-rs abstractions

Introduction Proof-of-concept of getting OpenXR rendering support for Bevy game engine using gfx-rs abstractions. (hand interaction with boxes missing

Mika 52 Nov 14, 2022