2 Commits

Author SHA1 Message Date
ab5f489450 Enable collision sensing for bodyless entities
Some checks failed
Basic checks / Basic build-and-test supertask (push) Has been cancelled
Rapier expects to have a RigidBody attached to the entity, but I do not.

I'm not going to make one, either, because the objects in a game of
Asteroids don't need collision handling the way most games do. I just
need to know if two objects have started overlapping.

According to this: https://rapier.rs/docs/user_guides/bevy_plugin/colliders#collision-groups-and-solver-groups
only one of the two objects involved needs to have the ActiveEvents and
ActiveCollisionTypes components attached, so I've placed them on the
player ship.
2025-08-06 15:21:12 -05:00
d4ceaa6312 Add Sensor component to colliders 2025-08-06 14:44:38 -05:00
2 changed files with 18 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
use bevy_rapier2d::prelude::Collider; use bevy_rapier2d::prelude::*;
use rand::{Rng, SeedableRng}; use rand::{Rng, SeedableRng};
use std::time::Duration; use std::time::Duration;
@@ -118,6 +118,7 @@ pub fn spawn_asteroid(
commands.spawn(( commands.spawn((
Asteroid(AsteroidSize::Small), Asteroid(AsteroidSize::Small),
Collider::ball(collider_radius), Collider::ball(collider_radius),
Sensor,
Position(spawn.pos), Position(spawn.pos),
Velocity(spawn.vel), Velocity(spawn.vel),
Rotation(0.0), Rotation(0.0),

View File

@@ -10,7 +10,11 @@ use bevy::prelude::*;
use bevy_inspector_egui::InspectorOptions; use bevy_inspector_egui::InspectorOptions;
use bevy_inspector_egui::prelude::ReflectInspectorOptions; use bevy_inspector_egui::prelude::ReflectInspectorOptions;
use bevy_rapier2d::{plugin::{NoUserData, RapierPhysicsPlugin}, prelude::Collider, render::RapierDebugRenderPlugin}; use bevy_rapier2d::{
plugin::{NoUserData, RapierPhysicsPlugin},
prelude::*,
render::RapierDebugRenderPlugin,
};
use config::{ASTEROID_SMALL_COLOR, SHIP_THRUSTER_COLOR_ACTIVE, SHIP_THRUSTER_COLOR_INACTIVE}; use config::{ASTEROID_SMALL_COLOR, SHIP_THRUSTER_COLOR_ACTIVE, SHIP_THRUSTER_COLOR_INACTIVE};
pub struct AsteroidPlugin; pub struct AsteroidPlugin;
@@ -43,6 +47,8 @@ impl Plugin for AsteroidPlugin {
wrap_entities, wrap_entities,
asteroids::tick_asteroid_manager, asteroids::tick_asteroid_manager,
asteroids::spawn_asteroid.after(asteroids::tick_asteroid_manager), asteroids::spawn_asteroid.after(asteroids::tick_asteroid_manager),
// TODO: Remove debug printing
debug_collision_event_printer,
) )
.run_if(in_state(GameState::Playing)), .run_if(in_state(GameState::Playing)),
) )
@@ -56,6 +62,12 @@ impl Plugin for AsteroidPlugin {
} }
} }
fn debug_collision_event_printer(mut collision_events: EventReader<CollisionEvent>) {
for event in collision_events.read() {
dbg!(event);
}
}
#[derive(Clone, Debug, Eq, Hash, PartialEq, States)] #[derive(Clone, Debug, Eq, Hash, PartialEq, States)]
pub enum GameState { pub enum GameState {
TitleScreen, // Program is started. Present title screen and await user start TitleScreen, // Program is started. Present title screen and await user start
@@ -180,6 +192,9 @@ fn spawn_player(mut commands: Commands, game_assets: Res<GameAssets>) {
commands commands
.spawn(( .spawn((
Collider::ball(0.7), Collider::ball(0.7),
Sensor,
ActiveEvents::COLLISION_EVENTS,
ActiveCollisionTypes::STATIC_STATIC,
Ship, Ship,
Wrapping, Wrapping,
Position(Vec2::default()), Position(Vec2::default()),