physis + ship movement

This commit is contained in:
2025-05-13 21:32:14 -04:00
parent b5cbf8e386
commit a8f0793865
3 changed files with 354 additions and 10 deletions

282
Cargo.lock generated
View File

@@ -111,6 +111,12 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4aa90d7ce82d4be67b64039a3d588d38dbcc6736577de4a847025ce5b0c468d1"
[[package]]
name = "allocator-api2"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
[[package]]
name = "alsa"
version = "0.9.1"
@@ -373,6 +379,38 @@ dependencies = [
"v_frame",
]
[[package]]
name = "avian3d"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8ba10c23fee3d432f3c6cf8d036e269d952b8840c1ca4fa6a021ab926786dc4"
dependencies = [
"avian_derive",
"bevy",
"bevy_heavy",
"bevy_math",
"bevy_transform_interpolation",
"bitflags 2.9.0",
"derive_more",
"itertools 0.13.0",
"nalgebra",
"parry3d",
"parry3d-f64",
"thread_local",
]
[[package]]
name = "avian_derive"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12b257f601a1535e0d4a7a7796f535e3a13de62fd422b16dff7c14d27f0d4048"
dependencies = [
"proc-macro-error2",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "avif-serialize"
version = "0.8.3"
@@ -838,6 +876,16 @@ dependencies = [
"tracing",
]
[[package]]
name = "bevy_heavy"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34ccc861fea2ff58c67f4df119512e204050bd7631a3a9c65e1a5e9d162cce28"
dependencies = [
"bevy_math",
"bevy_reflect",
]
[[package]]
name = "bevy_image"
version = "0.16.0"
@@ -1378,6 +1426,15 @@ dependencies = [
"thiserror 2.0.12",
]
[[package]]
name = "bevy_transform_interpolation"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72c7c6c4e6a3d5415b3a29a17bd20c17cd0e2f068b96b24e263316d58d5346ea"
dependencies = [
"bevy",
]
[[package]]
name = "bevy_ui"
version = "0.16.0"
@@ -1493,7 +1550,7 @@ dependencies = [
"proc-macro2",
"quote",
"regex",
"rustc-hash",
"rustc-hash 1.1.0",
"shlex",
"syn",
]
@@ -1899,7 +1956,7 @@ dependencies = [
"fontdb",
"log",
"rangemap",
"rustc-hash",
"rustc-hash 1.1.0",
"rustybuzz",
"self_cell",
"smol_str",
@@ -2145,6 +2202,15 @@ dependencies = [
"bytemuck",
]
[[package]]
name = "ena"
version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d248bdd43ce613d87415282f69b9bb99d947d290b10962dd6c56233312c2ad5"
dependencies = [
"log",
]
[[package]]
name = "encase"
version = "0.10.0"
@@ -2709,6 +2775,7 @@ version = "0.15.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84b26c544d002229e640969970a2e74021aadf6e2f96372b9c58eff97de08eb3"
dependencies = [
"allocator-api2",
"equivalent",
"foldhash",
"serde",
@@ -3127,6 +3194,16 @@ dependencies = [
"regex-automata 0.1.10",
]
[[package]]
name = "matrixmultiply"
version = "0.3.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06de3016e9fae57a36fd14dba131fccf49f74b40b7fbdb472f96e361ec71a08"
dependencies = [
"autocfg",
"rawpointer",
]
[[package]]
name = "maybe-rayon"
version = "0.1.1"
@@ -3210,7 +3287,7 @@ dependencies = [
"indexmap",
"log",
"pp-rs",
"rustc-hash",
"rustc-hash 1.1.0",
"spirv",
"strum",
"termcolor",
@@ -3232,12 +3309,40 @@ dependencies = [
"once_cell",
"regex",
"regex-syntax 0.8.5",
"rustc-hash",
"rustc-hash 1.1.0",
"thiserror 1.0.69",
"tracing",
"unicode-ident",
]
[[package]]
name = "nalgebra"
version = "0.33.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26aecdf64b707efd1310e3544d709c5c0ac61c13756046aaaba41be5c4f66a3b"
dependencies = [
"approx",
"glam",
"matrixmultiply",
"nalgebra-macros",
"num-complex",
"num-rational",
"num-traits",
"simba",
"typenum",
]
[[package]]
name = "nalgebra-macros"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "254a5372af8fc138e36684761d3c0cdb758a4410e938babcff1c860ce14ddbfc"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "ndk"
version = "0.8.0"
@@ -3404,6 +3509,15 @@ dependencies = [
"num-traits",
]
[[package]]
name = "num-complex"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495"
dependencies = [
"num-traits",
]
[[package]]
name = "num-derive"
version = "0.4.2"
@@ -3855,6 +3969,60 @@ dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "parry3d"
version = "0.17.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6aeb9659a05b1783fb2e9bc94f48225ae5b40817eb45b62569c0e4dd767a6e51"
dependencies = [
"approx",
"arrayvec",
"bitflags 2.9.0",
"downcast-rs 1.2.1",
"either",
"ena",
"log",
"nalgebra",
"num-derive",
"num-traits",
"ordered-float",
"rayon",
"rstar",
"rustc-hash 2.1.1",
"simba",
"slab",
"smallvec",
"spade",
"thiserror 1.0.69",
]
[[package]]
name = "parry3d-f64"
version = "0.17.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4484c8ad93ff03c0e57aa1a4f3ff5406ab6301a1eb838ef6dea90e94f00a6c7"
dependencies = [
"approx",
"arrayvec",
"bitflags 2.9.0",
"downcast-rs 1.2.1",
"either",
"ena",
"log",
"nalgebra",
"num-derive",
"num-traits",
"ordered-float",
"rayon",
"rstar",
"rustc-hash 2.1.1",
"simba",
"slab",
"smallvec",
"spade",
"thiserror 1.0.69",
]
[[package]]
name = "paste"
version = "1.0.15"
@@ -4008,6 +4176,28 @@ dependencies = [
"toml_edit",
]
[[package]]
name = "proc-macro-error-attr2"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96de42df36bb9bba5542fe9f1a054b8cc87e172759a1868aa05c1f3acc89dfc5"
dependencies = [
"proc-macro2",
"quote",
]
[[package]]
name = "proc-macro-error2"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "11ec05c52be0a07b08061f7dd003e7d7092e0472bc731b4af7bb1ef876109802"
dependencies = [
"proc-macro-error-attr2",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "proc-macro2"
version = "1.0.95"
@@ -4189,6 +4379,12 @@ version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20675572f6f24e9e76ef639bc5552774ed45f1c30e2951e1e99c59888861c539"
[[package]]
name = "rawpointer"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3"
[[package]]
name = "rayon"
version = "1.10.0"
@@ -4299,6 +4495,12 @@ version = "0.8.50"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57397d16646700483b67d2dd6511d79318f9d057fdbd21a4066aeac8b41d310a"
[[package]]
name = "robust"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e27ee8bb91ca0adcf0ecb116293afa12d393f9c2b9b9cd54d33e8078fe19839"
[[package]]
name = "rodio"
version = "0.20.1"
@@ -4327,12 +4529,29 @@ version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c20b6793b5c2fa6553b250154b78d6d0db37e72700ae35fad9387a46f487c97"
[[package]]
name = "rstar"
version = "0.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "421400d13ccfd26dfa5858199c30a5d76f9c54e0dba7575273025b43c5175dbb"
dependencies = [
"heapless",
"num-traits",
"smallvec",
]
[[package]]
name = "rustc-hash"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]]
name = "rustc-hash"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d"
[[package]]
name = "rustix"
version = "0.38.44"
@@ -4384,6 +4603,15 @@ version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f"
[[package]]
name = "safe_arch"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96b02de82ddbe1b636e6170c21be622223aea188ef2e139be0a5b219ec215323"
dependencies = [
"bytemuck",
]
[[package]]
name = "same-file"
version = "1.0.6"
@@ -4486,6 +4714,19 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "simba"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3a386a501cd104797982c15ae17aafe8b9261315b5d07e3ec803f2ea26be0fa"
dependencies = [
"approx",
"num-complex",
"num-traits",
"paste",
"wide",
]
[[package]]
name = "simd-adler32"
version = "0.3.7"
@@ -4573,10 +4814,23 @@ dependencies = [
name = "space-game"
version = "0.1.0"
dependencies = [
"avian3d",
"bevy",
"bevy-inspector-egui",
]
[[package]]
name = "spade"
version = "2.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ece03ff43cd2a9b57ebf776ea5e78bd30b3b4185a619f041079f4109f385034"
dependencies = [
"hashbrown",
"num-traits",
"robust",
"smallvec",
]
[[package]]
name = "spin"
version = "0.9.8"
@@ -4983,6 +5237,12 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc7d623258602320d5c55d1bc22793b57daff0ec7efc270ea7d55ce1d5f5471c"
[[package]]
name = "typenum"
version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f"
[[package]]
name = "unicode-bidi"
version = "0.3.18"
@@ -5376,7 +5636,7 @@ dependencies = [
"parking_lot",
"profiling",
"raw-window-handle",
"rustc-hash",
"rustc-hash 1.1.0",
"smallvec",
"thiserror 2.0.12",
"wgpu-hal",
@@ -5419,7 +5679,7 @@ dependencies = [
"range-alloc",
"raw-window-handle",
"renderdoc-sys",
"rustc-hash",
"rustc-hash 1.1.0",
"smallvec",
"thiserror 2.0.12",
"wasm-bindgen",
@@ -5442,6 +5702,16 @@ dependencies = [
"web-sys",
]
[[package]]
name = "wide"
version = "0.7.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41b5576b9a81633f3e8df296ce0063042a73507636cbe956c61133dd7034ab22"
dependencies = [
"bytemuck",
"safe_arch",
]
[[package]]
name = "winapi"
version = "0.3.9"

View File

@@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2024"
[dependencies]
avian3d = { version = "0.3.0", features = ["debug-plugin"] }
bevy = { version = "0.16.0", features = ["file_watcher"] }
# bevy_rapier3d = { version = "0.29.0", features = ["simd-stable", "parallel"] }
bevy-inspector-egui = "0.31.0"

View File

@@ -1,5 +1,6 @@
use std::f32::consts::FRAC_PI_2;
use avian3d::{PhysicsPlugins, prelude::*};
use bevy::{
input::mouse::MouseMotion,
prelude::*,
@@ -20,13 +21,18 @@ pub struct CameraPitch(pub f32);
#[derive(Component)]
pub struct Unfocused;
#[derive(Component)]
pub struct Ship;
impl Plugin for GamePlugin {
fn build(&self, app: &mut bevy::app::App) {
// app.add_plugins(RapierPhysicsPlugin::<NoUserData>::default());
app.add_plugins((PhysicsPlugins::default(), PhysicsDebugPlugin::default()));
app.insert_resource(Gravity::ZERO);
app.add_systems(Startup, (setup_scene, spawn_ship));
app.add_systems(Update, ((camera_yaw, camera_pitch).chain(), camera_toggle, fly_camera));
app.add_systems(Update, ship_controls);
}
}
@@ -65,7 +71,13 @@ fn setup_scene(
let cube = meshes.add(Cuboid::new(100.0, 0.1, 100.0));
let material = materials.add(Color::WHITE);
commands.spawn((Mesh3d(cube), MeshMaterial3d(material), Transform::default()));
commands.spawn((
Mesh3d(cube),
MeshMaterial3d(material),
Transform::default(),
RigidBody::Static,
Collider::cuboid(100.0, 0.1, 100.0),
));
}
fn spawn_ship(
@@ -81,35 +93,44 @@ fn spawn_ship(
Mesh3d(meshes.add(Cuboid::new(3.0, 0.1, 6.0))),
MeshMaterial3d(material.clone()),
Name::new("Ship"),
Ship,
LinearDamping::default(),
AngularDamping::default(),
Transform::from_xyz(0.0, 1.0, 0.0),
RigidBody::Dynamic,
children![
(
Name::new("Back Wall"),
Mesh3d(meshes.add(Cuboid::new(3.0, 2.0, 0.1))),
Collider::cuboid(3.0, 2.0, 0.1),
MeshMaterial3d(material.clone()),
Transform::from_xyz(0.0, 1.0, 6.0 / 2.0),
),
(
Name::new("Front Window"),
Mesh3d(meshes.add(Cuboid::new(3.0, 2.0, 0.1))),
Collider::cuboid(3.0, 2.0, 0.1),
MeshMaterial3d(window_material),
Transform::from_xyz(0.0, 1.0, -6.0 / 2.0),
),
(
Name::new("Right Wall"),
Mesh3d(meshes.add(Cuboid::new(0.1, 2.0, 6.0))),
Collider::cuboid(0.1, 2.0, 6.0),
MeshMaterial3d(material.clone()),
Transform::from_xyz(3.0 / 2.0, 1.0, 0.0),
),
(
Name::new("Left Wall"),
Mesh3d(meshes.add(Cuboid::new(0.1, 2.0, 6.0))),
Collider::cuboid(0.1, 2.0, 6.0),
MeshMaterial3d(material.clone()),
Transform::from_xyz(-3.0 / 2.0, 1.0, 0.0),
),
(
Name::new("Roof"),
Mesh3d(meshes.add(Cuboid::new(3.0, 0.1, 6.0))),
Collider::cuboid(3.0, 0.1, 6.0),
MeshMaterial3d(material.clone()),
Transform::from_xyz(0.0, 2.0, 0.0),
)
@@ -119,11 +140,10 @@ fn spawn_ship(
pub fn camera_toggle(
key: Res<ButtonInput<KeyCode>>,
window_query: Single<&mut Window, With<PrimaryWindow>>,
mut window: Single<&mut Window, With<PrimaryWindow>>,
camera: Single<Entity, With<CameraRoot>>,
mut commands: Commands,
) {
let mut window = window_query.into_inner();
if key.just_pressed(KeyCode::Escape) {
if window.cursor_options.visible {
commands.entity(camera.into_inner()).remove::<Unfocused>();
@@ -197,3 +217,56 @@ fn get_mouse_delta(mut mouse_motion: EventReader<MouseMotion>) -> Vec2 {
}
return delta;
}
fn ship_controls(
ship_query: Single<
(
&Transform,
&mut LinearVelocity,
&mut AngularVelocity,
&mut LinearDamping,
&mut AngularDamping,
),
With<Ship>,
>,
window: Single<&Window, With<PrimaryWindow>>,
key: Res<ButtonInput<KeyCode>>,
time: Res<Time>,
) {
if !window.cursor_options.visible {
return;
}
let (transform, mut vel, mut ang, mut ldamp, mut adamp) = ship_query.into_inner();
let mut move_vec = Vec3::ZERO;
if key.pressed(KeyCode::KeyW) {
move_vec.z = -1.0;
} else if key.pressed(KeyCode::KeyS) {
move_vec.z = 1.0;
}
if key.pressed(KeyCode::KeyA) {
move_vec.x = -1.0;
} else if key.pressed(KeyCode::KeyD) {
move_vec.x = 1.0;
}
if key.pressed(KeyCode::Space) {
move_vec.y = 1.0;
} else if key.pressed(KeyCode::ShiftLeft) {
move_vec.y = -1.0;
}
if key.pressed(KeyCode::ControlLeft) {
ldamp.0 = 0.8;
adamp.0 = 0.8;
} else {
ldamp.0 = 0.0;
adamp.0 = 0.0;
}
move_vec = transform.rotation * move_vec.normalize_or_zero();
vel.0 += move_vec * time.delta_secs();
}