Skip to content

Instantly share code, notes, and snippets.

@Maximetinu
Last active August 15, 2023 23:58
Show Gist options
  • Save Maximetinu/ecb4941485a731e021e8bd536767b199 to your computer and use it in GitHub Desktop.
Save Maximetinu/ecb4941485a731e021e8bd536767b199 to your computer and use it in GitHub Desktop.
Example for bevy_rapier to illustrate an issue that I have but haven't yet figured out how to solve
use bevy::prelude::*;
use bevy_rapier2d::prelude::*;
// use bevy_inspector_egui::quick::WorldInspectorPlugin;
fn main() {
App::new()
.insert_resource(ClearColor(Color::rgb(
0xF9 as f32 / 255.0,
0xF9 as f32 / 255.0,
0xFF as f32 / 255.0,
)))
.add_plugins((
DefaultPlugins,
// using the inspector to discern what are the entities detected by the collision event
// WorldInspectorPlugin::new(),
RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(100.0),
RapierDebugRenderPlugin::default(),
))
.add_systems(Startup, (setup_graphics, setup_physics))
.add_systems(PostUpdate, display_events)
.run();
}
fn setup_graphics(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
}
pub fn setup_physics(mut commands: Commands) {
/*
* Ground
*/
let ground_size = 500.0;
let ground_height = 1.0;
commands.spawn((
TransformBundle::from(Transform::from_xyz(0.0, -ground_height, 0.0)),
Collider::cuboid(ground_size, ground_height),
));
/*
* Create a Cube with the shape of a |_|
*/
commands
.spawn((
TransformBundle::from(Transform::from_xyz(0.0, 100.0, 0.0)),
RigidBody::Dynamic,
))
.with_children(|children| {
// Left Piece
children.spawn((
TransformBundle::from(Transform::from_xyz(-20.0, 0.0, 0.0)),
Collider::cuboid(2.0, 20.0),
));
// Right Piece
children.spawn((
TransformBundle::from(Transform::from_xyz(20.0, 0.0, 0.0)),
Collider::cuboid(2.0, 20.0),
));
// Bottom Piece
// since it's at the bottom, it's the only one colliding with the ground
children.spawn((
TransformBundle::from(Transform::from_xyz(0.0, -20.0, 0.0)),
Collider::cuboid(20.0, 2.0),
// We can enable collision events here or at the Ground collider so they are detected
ActiveEvents::COLLISION_EVENTS,
));
// little cube at the center, just for reference
children.spawn((TransformBundle::IDENTITY, Collider::cuboid(2.0, 2.0)));
});
}
// We detect a collision here between Bottom Piece and Ground
// due to the cube having a Rididbody::Dynamic causing gravity
// and to the ActiveEvents::COLLISION_EVENTS on the Bottom Piece
fn display_events(mut collision_events: EventReader<CollisionEvent>) {
for collision_event in collision_events.iter() {
println!("Received collision event: {collision_event:?}");
}
}
// ... However, I want to destroy the whole cube entity when touching the ground...
// but I only detect Bottom Piece and Ground entities... :(
// How can I detect any collisions that the children of a Rigidbody have?
// So I can detect here a pair of Cube and Ground
// Sure, I can use the Parent component and despawn its Entity
// but, what if the children colliders are in a deeper descendant?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment