This commit is contained in:
nora 2023-09-16 13:19:06 +02:00
parent e4a76c7932
commit c97d11b072
3 changed files with 352 additions and 14 deletions

241
Cargo.lock generated
View file

@ -719,6 +719,19 @@ version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15702dff420fac72c2ab92428a8600e079ae89c5845401c4e39b843665a3d2d0"
[[package]]
name = "bevy_rapier3d"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12402872b857ba490f1040ab6212915bd9bf25f8584b31f2c43cef41b33f3be4"
dependencies = [
"bevy",
"bitflags 1.3.2",
"log",
"nalgebra",
"rapier3d",
]
[[package]]
name = "bevy_reflect"
version = "0.11.2"
@ -1358,6 +1371,20 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "crossbeam"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2801af0d36612ae591caa9568261fddce32ce6e08a7275ea334a06a4ad021a2c"
dependencies = [
"cfg-if",
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-epoch",
"crossbeam-queue",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-channel"
version = "0.5.8"
@ -1368,6 +1395,40 @@ dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef"
dependencies = [
"cfg-if",
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7"
dependencies = [
"autocfg",
"cfg-if",
"crossbeam-utils",
"memoffset",
"scopeguard",
]
[[package]]
name = "crossbeam-queue"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d1cfb3ea8a53f37c40dea2c7bedcbd88bdfae54f5e2175d6ecaff1c988353add"
dependencies = [
"cfg-if",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.16"
@ -1412,6 +1473,12 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650"
[[package]]
name = "either"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]]
name = "encase"
version = "0.6.1"
@ -2051,6 +2118,12 @@ dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "libm"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7012b1bbb0719e1097c47611d3898568c546d597c2e74d66f6087edd5233ff4"
[[package]]
name = "libudev-sys"
version = "0.1.4"
@ -2104,12 +2177,31 @@ dependencies = [
"regex-automata 0.1.10",
]
[[package]]
name = "matrixmultiply"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "090126dc04f95dc0d1c1c91f61bdd474b3930ca064c1edc8a849da2c6cbe1e77"
dependencies = [
"autocfg",
"rawpointer",
]
[[package]]
name = "memchr"
version = "2.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c"
[[package]]
name = "memoffset"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c"
dependencies = [
"autocfg",
]
[[package]]
name = "metal"
version = "0.24.0"
@ -2193,6 +2285,34 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "nalgebra"
version = "0.32.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "307ed9b18cc2423f29e83f84fd23a8e73628727990181f18641a8b5dc2ab1caa"
dependencies = [
"approx",
"glam",
"matrixmultiply",
"nalgebra-macros",
"num-complex",
"num-rational",
"num-traits",
"simba",
"typenum",
]
[[package]]
name = "nalgebra-macros"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91761aed67d03ad966ef783ae962ef9bbaca728d2dd7ceb7939ec110fffad998"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "ndk"
version = "0.7.0"
@ -2292,6 +2412,15 @@ dependencies = [
"winapi",
]
[[package]]
name = "num-complex"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ba157ca0885411de85d6ca030ba7e2a83a28636056c7c699b07c8b6f7383214"
dependencies = [
"num-traits",
]
[[package]]
name = "num-derive"
version = "0.3.3"
@ -2331,6 +2460,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2"
dependencies = [
"autocfg",
"libm",
]
[[package]]
@ -2467,6 +2597,12 @@ version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
[[package]]
name = "optional"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "978aa494585d3ca4ad74929863093e87cac9790d81fe7aba2b3dc2890643a0fc"
[[package]]
name = "orbclient"
version = "0.3.46"
@ -2520,6 +2656,27 @@ dependencies = [
"windows-targets 0.48.5",
]
[[package]]
name = "parry3d"
version = "0.13.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b55dc0e6db79bddbc5fd583569f7356cdcc63e1e9b2b93a9ab70dd8e717160e0"
dependencies = [
"approx",
"arrayvec",
"bitflags 1.3.2",
"downcast-rs",
"either",
"nalgebra",
"num-derive",
"num-traits",
"rustc-hash",
"simba",
"slab",
"smallvec",
"spade",
]
[[package]]
name = "paste"
version = "1.0.14"
@ -2628,12 +2785,39 @@ version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8a99fddc9f0ba0a85884b8d14e3592853e787d581ca1816c91349b10e4eeab"
[[package]]
name = "rapier3d"
version = "0.17.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62a8a0bd9d3135f7b4eb45d0796540e7bab47b6b7c974f90567ccc5a0454f42b"
dependencies = [
"approx",
"arrayvec",
"bit-vec",
"bitflags 1.3.2",
"crossbeam",
"downcast-rs",
"nalgebra",
"num-derive",
"num-traits",
"parry3d",
"rustc-hash",
"simba",
"vec_map",
]
[[package]]
name = "raw-window-handle"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2ff9a1f06a88b01621b7ae906ef0211290d1c8a168a15542486a8f61c0833b9"
[[package]]
name = "rawpointer"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3"
[[package]]
name = "rectangle-pack"
version = "0.4.2"
@ -2699,6 +2883,12 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "216080ab382b992234dda86873c18d4c48358f5cfcb70fd693d7f6f2131b628b"
[[package]]
name = "robust"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5864e7ef1a6b7bcf1d6ca3f655e65e724ed3b52546a0d0a663c991522f552ea"
[[package]]
name = "rodio"
version = "0.17.1"
@ -2750,6 +2940,15 @@ version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741"
[[package]]
name = "safe_arch"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f398075ce1e6a179b46f51bd88d0598b92b00d3551f1a2d4ac49e771b56ac354"
dependencies = [
"bytemuck",
]
[[package]]
name = "same-file"
version = "1.0.6"
@ -2811,6 +3010,19 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7cee0529a6d40f580e7a5e6c495c8fbfe21b7b52795ed4bb5e62cdf92bc6380"
[[package]]
name = "simba"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "061507c94fc6ab4ba1c9a0305018408e312e17c041eb63bef8aa726fa33aceae"
dependencies = [
"approx",
"num-complex",
"num-traits",
"paste",
"wide",
]
[[package]]
name = "simd-adler32"
version = "0.3.7"
@ -2858,6 +3070,19 @@ name = "spaceflight"
version = "0.1.0"
dependencies = [
"bevy",
"bevy_rapier3d",
]
[[package]]
name = "spade"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88e65803986868d2372c582007c39ba89936a36ea5f236bf7a7728dc258f04f9"
dependencies = [
"num-traits",
"optional",
"robust",
"smallvec",
]
[[package]]
@ -3110,6 +3335,12 @@ dependencies = [
"static_assertions",
]
[[package]]
name = "typenum"
version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
[[package]]
name = "unicode-ident"
version = "1.0.12"
@ -3365,6 +3596,16 @@ dependencies = [
"web-sys",
]
[[package]]
name = "wide"
version = "0.7.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa469ffa65ef7e0ba0f164183697b89b854253fd31aeb92358b7b6155177d62f"
dependencies = [
"bytemuck",
"safe_arch",
]
[[package]]
name = "widestring"
version = "1.0.2"

View file

@ -12,4 +12,8 @@ opt-level = 1
opt-level = 3
[dependencies]
bevy = { version = "0.11.2", features = ["dynamic_linking"]}
bevy = { version = "0.11.2", features = ["dynamic_linking"] }
bevy_rapier3d = { version = "0.22.0", features = [
"simd-stable",
"debug-render-3d",
] }

View file

@ -1,14 +1,68 @@
//! A simple 3D scene with light shining over a cube sitting on a plane.
use bevy::prelude::*;
use bevy_rapier3d::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(RapierPhysicsPlugin::<NoUserData>::default())
.add_plugins(RapierDebugRenderPlugin::default())
.add_systems(Startup, setup)
.add_systems(
FixedUpdate,
(
apply_velocity,
fire_thrusters.after(apply_velocity),
),
)
.run();
}
#[derive(Bundle)]
struct SpaceshipBundle {
ship_marker: Spaceship,
model: PbrBundle,
vel: Velocity,
gravity: Gravity,
body: RigidBody,
collider: Collider,
restitution: Restitution,
}
#[derive(Component, Deref, DerefMut)]
struct Velocity(Vec3);
#[derive(Component)]
struct Spaceship;
#[derive(Component)]
struct Gravity;
#[derive(Component)]
struct Ground;
fn fire_thrusters(
keyboard_input: Res<Input<KeyCode>>,
mut query: Query<&mut Velocity, With<Spaceship>>,
time_step: Res<FixedTime>,
) {
let mut transform = query.single_mut();
if keyboard_input.pressed(KeyCode::Space) {
transform.y += 10.0 * time_step.period.as_secs_f32();
}
}
// TODO: DELETE
fn apply_velocity(mut query: Query<(&mut Transform, &Velocity)>, time_step: Res<FixedTime>) {
for (mut trans, vel) in &mut query {
trans.translation.x += vel.x * time_step.period.as_secs_f32();
trans.translation.y += vel.y * time_step.period.as_secs_f32();
trans.translation.z += vel.z * time_step.period.as_secs_f32();
}
}
/// set up a simple 3D scene
fn setup(
mut commands: Commands,
@ -16,18 +70,22 @@ fn setup(
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(shape::Plane::from_size(5.0).into()),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
// cube
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..default()
});
commands.spawn((
PbrBundle {
mesh: meshes.add(shape::Plane::from_size(5.0).into()),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
transform: Transform::from_scale(Vec3 {
x: 5.0,
y: 1.0,
z: 5.0,
}),
..default()
},
Ground,
Collider::cuboid(5.0, 0.1, 5.0),
));
commands.spawn(SpaceshipBundle::new(&mut meshes, &mut materials));
// light
commands.spawn(PointLightBundle {
point_light: PointLight {
@ -44,3 +102,38 @@ fn setup(
..default()
});
}
impl SpaceshipBundle {
fn new(meshes: &mut Assets<Mesh>, materials: &mut Assets<StandardMaterial>) -> Self {
Self {
ship_marker: Spaceship,
model: PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
transform: Transform::from_xyz(0.0, 0.5, 0.0).with_scale(Vec3 {
x: 1.0,
y: 1.0,
z: 1.0,
}),
..default()
},
vel: Velocity(Vec3::default()),
gravity: Gravity,
body: RigidBody::Dynamic,
collider: Collider::ball(0.5),
restitution: Restitution::coefficient(0.1),
}
}
}
fn collides(a_pos: Vec3, a_size: Vec3, b_pos: Vec3, b_size: Vec3) -> bool {
let a_min = a_pos - a_size / 2.0;
let a_max = a_pos + a_size * 2.0;
let b_min = b_pos - b_size / 2.0;
let b_max = b_pos + b_size / 2.0;
let axis_collides =
|axis: fn(Vec3) -> f32| axis(a_min) < axis(b_max) && axis(a_max) > axis(b_min);
axis_collides(|v| v.x) && axis_collides(|v| v.y) && axis_collides(|v| v.z)
}