Beginning work with collision detection
Some checks failed
Basic checks / Basic build-and-test supertask (push) Failing after 31s
Some checks failed
Basic checks / Basic build-and-test supertask (push) Failing after 31s
I'm going to grab the Rapier physics library so that I don't have to do my own collision detection mechanism. The last time I did this, I simplified everything into circles. This time I'd like to have convex hulls, particularly for the player ship. Also the last time, I ended up rolling my own quadtree impl. I'm not particularly interested in doing that again, and I'd like to learn more of the broader Bevy ecosystem.
This commit is contained in:
@@ -6,4 +6,5 @@ edition = "2024"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
bevy = { version = "0.16", features = ["dynamic_linking"] }
|
bevy = { version = "0.16", features = ["dynamic_linking"] }
|
||||||
bevy-inspector-egui = "0.32.0"
|
bevy-inspector-egui = "0.32.0"
|
||||||
|
bevy_rapier2d = { version = "0.31.0", features = ["debug-render-2d"] }
|
||||||
rand = "0.9.2"
|
rand = "0.9.2"
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use bevy_rapier2d::prelude::Collider;
|
||||||
use rand::{Rng, SeedableRng};
|
use rand::{Rng, SeedableRng};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
@@ -107,8 +108,16 @@ pub fn spawn_asteroid(
|
|||||||
AsteroidSize::Medium => game_assets.asteroid_medium(),
|
AsteroidSize::Medium => game_assets.asteroid_medium(),
|
||||||
AsteroidSize::Large => game_assets.asteroid_large(),
|
AsteroidSize::Large => game_assets.asteroid_large(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let collider_radius = match spawn.size {
|
||||||
|
AsteroidSize::Small => 10.0,
|
||||||
|
AsteroidSize::Medium => 20.0,
|
||||||
|
AsteroidSize::Large => 40.0,
|
||||||
|
};
|
||||||
|
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
Asteroid(AsteroidSize::Small),
|
Asteroid(AsteroidSize::Small),
|
||||||
|
Collider::ball(collider_radius),
|
||||||
Position(spawn.pos),
|
Position(spawn.pos),
|
||||||
Velocity(spawn.vel),
|
Velocity(spawn.vel),
|
||||||
Rotation(0.0),
|
Rotation(0.0),
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ 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 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;
|
||||||
@@ -19,6 +20,8 @@ impl Plugin for AsteroidPlugin {
|
|||||||
app.add_plugins((
|
app.add_plugins((
|
||||||
title_screen::GameMenuPlugin,
|
title_screen::GameMenuPlugin,
|
||||||
preparation_widget::preparation_widget_plugin,
|
preparation_widget::preparation_widget_plugin,
|
||||||
|
RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(10.0),
|
||||||
|
RapierDebugRenderPlugin::default(),
|
||||||
))
|
))
|
||||||
.insert_resource(ClearColor(BACKGROUND_COLOR))
|
.insert_resource(ClearColor(BACKGROUND_COLOR))
|
||||||
.insert_resource(WorldSize {
|
.insert_resource(WorldSize {
|
||||||
@@ -176,6 +179,7 @@ fn spawn_camera(mut commands: Commands) {
|
|||||||
fn spawn_player(mut commands: Commands, game_assets: Res<GameAssets>) {
|
fn spawn_player(mut commands: Commands, game_assets: Res<GameAssets>) {
|
||||||
commands
|
commands
|
||||||
.spawn((
|
.spawn((
|
||||||
|
Collider::ball(0.7),
|
||||||
Ship,
|
Ship,
|
||||||
Wrapping,
|
Wrapping,
|
||||||
Position(Vec2::default()),
|
Position(Vec2::default()),
|
||||||
|
|||||||
Reference in New Issue
Block a user