From c7ba106a4aae52c7ac5ebe022ec4c34a72195809 Mon Sep 17 00:00:00 2001 From: copygirl Date: Fri, 6 Jun 2025 16:40:47 +0200 Subject: [PATCH] Super basic test physics --- Cargo.lock | 333 +++++++++++++++++++++++++++++++++++++++-- Cargo.toml | 1 + src/bloxel/mesh/mod.rs | 8 +- src/main.rs | 28 ++++ 4 files changed, 358 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 333e448..3e5d0f0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -76,6 +76,12 @@ dependencies = [ "memchr", ] +[[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" @@ -281,6 +287,38 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" +[[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.1", + "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 = "base64" version = "0.21.7" @@ -306,6 +344,7 @@ dependencies = [ name = "bevy-bloxel-test" version = "0.1.0" dependencies = [ + "avian3d", "bevy", "bitvec", "cart_prod", @@ -351,7 +390,7 @@ dependencies = [ "bevy_utils", "blake3", "derive_more", - "downcast-rs", + "downcast-rs 2.0.1", "either", "petgraph", "ron", @@ -378,7 +417,7 @@ dependencies = [ "cfg-if", "console_error_panic_hook", "ctrlc", - "downcast-rs", + "downcast-rs 2.0.1", "log", "thiserror 2.0.12", "variadics_please", @@ -409,7 +448,7 @@ dependencies = [ "crossbeam-channel", "derive_more", "disqualified", - "downcast-rs", + "downcast-rs 2.0.1", "either", "futures-io", "futures-lite", @@ -671,6 +710,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" @@ -956,7 +1005,7 @@ dependencies = [ "bevy_utils", "derive_more", "disqualified", - "downcast-rs", + "downcast-rs 2.0.1", "erased-serde", "foldhash", "glam", @@ -1012,7 +1061,7 @@ dependencies = [ "bytemuck", "codespan-reporting", "derive_more", - "downcast-rs", + "downcast-rs 2.0.1", "encase", "fixedbitset", "futures-lite", @@ -1211,6 +1260,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" @@ -1326,7 +1384,7 @@ dependencies = [ "proc-macro2", "quote", "regex", - "rustc-hash", + "rustc-hash 1.1.0", "shlex", "syn", ] @@ -1695,7 +1753,7 @@ dependencies = [ "fontdb", "log", "rangemap", - "rustc-hash", + "rustc-hash 1.1.0", "rustybuzz", "self_cell", "smol_str", @@ -1755,6 +1813,25 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "crossbeam-deque" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "crossbeam-queue" version = "0.3.12" @@ -1855,6 +1932,12 @@ dependencies = [ "litrs", ] +[[package]] +name = "downcast-rs" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2" + [[package]] name = "downcast-rs" version = "2.0.1" @@ -1873,6 +1956,15 @@ version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" +[[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" @@ -2380,6 +2472,7 @@ version = "0.15.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "84b26c544d002229e640969970a2e74021aadf6e2f96372b9c58eff97de08eb3" dependencies = [ + "allocator-api2", "equivalent", "foldhash", "serde", @@ -2809,7 +2902,7 @@ dependencies = [ "indexmap", "log", "pp-rs", - "rustc-hash", + "rustc-hash 1.1.0", "spirv", "strum", "termcolor", @@ -2831,12 +2924,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 = "ndarray" version = "0.16.1" @@ -3008,6 +3129,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "num-bigint" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" +dependencies = [ + "num-integer", + "num-traits", +] + [[package]] name = "num-complex" version = "0.4.6" @@ -3037,6 +3168,17 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-rational" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -3390,6 +3532,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.1", + "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.1", + "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" @@ -3543,6 +3739,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" @@ -3678,6 +3896,26 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" +[[package]] +name = "rayon" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + [[package]] name = "read-fonts" version = "0.29.2" @@ -3762,6 +4000,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "19b30a45b0cd0bcca8037f3d0dc3421eaf95327a17cad11964fb8179b4fc4832" +[[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" @@ -3790,12 +4034,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" @@ -3860,6 +4121,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" @@ -3934,6 +4204,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" @@ -3983,6 +4266,18 @@ dependencies = [ "serde", ] +[[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" @@ -4311,6 +4606,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" @@ -4572,7 +4873,7 @@ dependencies = [ "parking_lot", "profiling", "raw-window-handle", - "rustc-hash", + "rustc-hash 1.1.0", "smallvec", "thiserror 2.0.12", "wgpu-hal", @@ -4615,7 +4916,7 @@ dependencies = [ "range-alloc", "raw-window-handle", "renderdoc-sys", - "rustc-hash", + "rustc-hash 1.1.0", "smallvec", "thiserror 2.0.12", "wasm-bindgen", @@ -4638,6 +4939,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" diff --git a/Cargo.toml b/Cargo.toml index 60ddf71..c17efa2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ opt-level = 1 opt-level = 3 [dependencies] +avian3d = "0.3.0" bevy = { version = "0.16.0", features = [ "file_watcher", "embedded_watcher" ] } bitvec = "1.0.1" cart_prod = "0.1.0" diff --git a/src/bloxel/mesh/mod.rs b/src/bloxel/mesh/mod.rs index 7dca29a..ab0e8f8 100644 --- a/src/bloxel/mesh/mod.rs +++ b/src/bloxel/mesh/mod.rs @@ -1,5 +1,6 @@ use std::ops::Deref; +use avian3d::prelude::{ColliderConstructor, RigidBody}; use bevy::prelude::*; use crate::{ @@ -62,10 +63,15 @@ pub fn generate_chunk_mesh( &terrain_atlas.sources, &block_lookup, ); + let is_empty = mesh.count_vertices() == 0; - commands.entity(entity).insert(( + let mut entity = commands.entity(entity); + entity.insert(( Mesh3d(meshes.add(mesh)), MeshMaterial3d(terrain_material.0.clone()), )); + if !is_empty { + entity.insert((RigidBody::Static, ColliderConstructor::TrimeshFromMesh)); + } } } diff --git a/src/main.rs b/src/main.rs index 2c54f06..6ac9226 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ mod camera_controller; use bloxel::block::BlockTexture; use bloxel::prelude::*; use camera_controller::{CameraControllerPlugin, ControlledCamera}; +use cart_prod::specs::Hom2FCartProd; #[derive(Resource)] pub struct TerrainTextures(Handle); @@ -33,6 +34,7 @@ fn main() { App::new() .add_plugins(( DefaultPlugins.set(ImagePlugin::default_nearest()), + avian3d::PhysicsPlugins::default(), CameraControllerPlugin, BloxelPlugin, WorldGenPlugin, @@ -44,12 +46,38 @@ fn main() { setup_camera, setup_terrain_blocks, load_terrain_textures, + spawn_physics_cubes, ), ) .add_systems(Update, check_terrain_textures) .run(); } +fn spawn_physics_cubes( + mut commands: Commands, + mut meshes: ResMut>, + mut materials: ResMut>, +) { + use avian3d::prelude::*; + + let collider = Collider::cuboid(1.0, 1.0, 1.0); + let mesh = Mesh3d(meshes.add(Cuboid::from_length(1.0))); + let material = MeshMaterial3d(materials.add(Color::srgb_u8(124, 144, 255))); + + for [x, z] in Hom2FCartProd::new(-8..=8, -8..=8) { + let transform = Transform::from_xyz(x as f32 * 2., 12.0, z as f32 * 2.); + let ang_vel = AngularVelocity(Vec3::new(x as f32, 3.5, z as f32)); + commands.spawn(( + RigidBody::Dynamic, + collider.clone(), + ang_vel, + mesh.clone(), + material.clone(), + transform, + )); + } +} + fn setup_terrain_blocks(mut commands: Commands, asset_server: Res) { let air = commands.spawn((Block, Name::new("air"))).id();