jcornaz / heron

[DISCONTINUED] An ergonomic physics API for bevy games
MIT License
292 stars 44 forks source link

ColliderBundle #283

Open L1ghtingBolt opened 2 years ago

L1ghtingBolt commented 2 years ago

I'm making a tilemap and that would be really useful, where is it, or something similar?

jcornaz commented 2 years ago

There is none... Yet. Currently you pick the components you need, and only that.

We could potentially add bundle types. Though, I am not yet sure what to provide exactly.

Basically, only two components are required: RigidBody and CollisionShape, but the CollisionShape may be in a child entity.

L1ghtingBolt commented 2 years ago

There is none... Yet. Currently you pick the components you need, and only that.

We could potentially add bundle types. Though, I am not yet sure what to provide exactly.

Basically, only two components are required: RigidBody and CollisionShape, but the CollisionShape may be in a child entity.

Hey. I'm using bevy_ecs_ldtk which recommends using heron. I saw that older versions of rapier included ColliderBundle, which was a bundle of colliders that you could use to make a more detailed collider. I wanted to use that in my tiled map. bevy_ecs_ldtk makes reference to ColliderBundle on the platformer example. Would it be possible to add a collider bundle or anything similar to make a tilemap?

L1ghtingBolt commented 2 years ago
/// Spawns heron collisions for the walls of a level
///
/// You could just insert a ColliderBundle in to the WallBundle,
/// but this spawns a different collider for EVERY wall tile.
/// This approach leads to bad performance.
///
/// Instead, by flagging the wall tiles and spawning the collisions later,
/// we can minimize the amount of colliding entities.
///
/// The algorithm used here is a nice compromise between simplicity, speed,
/// and a small number of rectangle colliders.
/// In basic terms, it will:
/// 1. consider where the walls are
/// 2. combine wall tiles into flat "plates" in each individual row
/// 3. combine the plates into rectangles across multiple rows wherever possible
/// 4. spawn colliders for each rectangle"

Comment in the bevy_ecs_ldtk example

jcornaz commented 2 years ago

As I just said above, yes we could potentially add a "bundle" type.

But I am still not so sure about which components it should contain. Should it be RigidBody and a CollisionShape? Should it include transforms?

I gess these lines from bevy_ecs_ldtk could change from this:

commands
    .spawn()
    .insert(CollisionShape::Cuboid {
        half_extends: Vec3::new(
            (wall_rect.right as f32 - wall_rect.left as f32 + 1.)
                * grid_size as f32
                / 2.,
            (wall_rect.top as f32 - wall_rect.bottom as f32 + 1.)
                * grid_size as f32
                / 2.,
            0.,
        ),
        border_radius: None,
    })
    .insert(RigidBody::Static)

to that:

commands
    .spawn_bundle(ColliderBundle {
       body: RigidBody::Static,
       collider: CollisionShape::Cuboid {
        half_extends: Vec3::new(
            (wall_rect.right as f32 - wall_rect.left as f32 + 1.)
                * grid_size as f32
                / 2.,
            (wall_rect.top as f32 - wall_rect.bottom as f32 + 1.)
                * grid_size as f32
                / 2.,
            0.,
        ),
        border_radius: None,
      }
    }))

It doesn't change much...

And in practice, such a bundle may not be used so often, given that more complex usage would often need to spawn the collision shape in a child entity.

L1ghtingBolt commented 2 years ago

As I said, the onli components for that bundle would be various colliders, no rigidbody, just various colliders

jcornaz commented 2 years ago

As I said, the onli components for that bundle would be various colliders, no rigidbody, just various colliders

That doesn't make sense to me.

I assume that by "collider" you mean CollisionShape?

You cannot insert multiple CollisionShape into a single entity. And why does it have to be a bundle if you only want to insert a single component?

L1ghtingBolt commented 2 years ago

Well then how to add collision to a tilemap

jcornaz commented 2 years ago

Well then how to add collision to a tilemap

Insert a RigidBody (probably the Static variant) and a CollisionShape component to each tile entity that should have collision. Depending on your use case, you may also add other components such as CollisionLayers.

Maybe read the example of bevy_ecs_ldtk that you mentioned.

I think the documentation is pretty clear. If you think it isn't, please suggest a concrete improvement.

When it comes to adding a bundle type, if you want to continue the discussion, please be more precise about what components the bundle should contain in your opinion. Maybe share here your user-code version of that bundle. And explain why that would be an improvement over the current situation.