switch to rapier character controller
it already supports custom gravity and custom up direction
This commit is contained in:
177
Cargo.lock
generated
177
Cargo.lock
generated
@@ -379,38 +379,6 @@ 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"
|
||||
@@ -900,16 +868,6 @@ 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.1"
|
||||
@@ -1183,6 +1141,19 @@ version = "0.16.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "df7370d0e46b60e071917711d0860721f5347bc958bf325975ae6913a5dfcf01"
|
||||
|
||||
[[package]]
|
||||
name = "bevy_rapier3d"
|
||||
version = "0.30.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cdf74109573c2c82b05b217cb6101f7e71e6c53ad622aed6c370cc5783c59eb8"
|
||||
dependencies = [
|
||||
"bevy",
|
||||
"bitflags 2.9.0",
|
||||
"log",
|
||||
"nalgebra",
|
||||
"rapier3d",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bevy_reflect"
|
||||
version = "0.16.1"
|
||||
@@ -1451,15 +1422,6 @@ 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.1"
|
||||
@@ -2032,6 +1994,19 @@ version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b"
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam"
|
||||
version = "0.8.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1137cd7e7fc0fb5d3c5a8678be38ec56e819125d8d7907411fe24ccb943faca8"
|
||||
dependencies = [
|
||||
"crossbeam-channel",
|
||||
"crossbeam-deque",
|
||||
"crossbeam-epoch",
|
||||
"crossbeam-queue",
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-channel"
|
||||
version = "0.5.15"
|
||||
@@ -3950,6 +3925,15 @@ dependencies = [
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ordered-float"
|
||||
version = "5.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2c1f9f56e534ac6a9b8a4600bdf0f530fb393b5f393e7b4d03489c3cf0c3f01"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "overload"
|
||||
version = "0.1.1"
|
||||
@@ -3996,56 +3980,27 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "parry3d"
|
||||
version = "0.17.6"
|
||||
version = "0.20.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6aeb9659a05b1783fb2e9bc94f48225ae5b40817eb45b62569c0e4dd767a6e51"
|
||||
checksum = "bec55ce6f725367f8149f750575e79a8879d71b7257c02273259f9375822821f"
|
||||
dependencies = [
|
||||
"approx",
|
||||
"arrayvec",
|
||||
"bitflags 2.9.0",
|
||||
"downcast-rs 1.2.1",
|
||||
"downcast-rs 2.0.1",
|
||||
"either",
|
||||
"ena",
|
||||
"hashbrown",
|
||||
"log",
|
||||
"nalgebra",
|
||||
"num-derive",
|
||||
"num-traits",
|
||||
"ordered-float",
|
||||
"rayon",
|
||||
"ordered-float 5.0.0",
|
||||
"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",
|
||||
"thiserror 2.0.12",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -4201,28 +4156,6 @@ 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"
|
||||
@@ -4348,6 +4281,30 @@ version = "1.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f60fcc7d6849342eff22c4350c8b9a989ee8ceabc4b481253e8946b9fe83d684"
|
||||
|
||||
[[package]]
|
||||
name = "rapier3d"
|
||||
version = "0.25.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a35ec3d01c4f918675411442024a1fbfb7eafdd878a6e82479ff6e461a9092fc"
|
||||
dependencies = [
|
||||
"approx",
|
||||
"arrayvec",
|
||||
"bit-vec 0.8.0",
|
||||
"bitflags 2.9.0",
|
||||
"crossbeam",
|
||||
"downcast-rs 2.0.1",
|
||||
"log",
|
||||
"nalgebra",
|
||||
"num-derive",
|
||||
"num-traits",
|
||||
"ordered-float 5.0.0",
|
||||
"parry3d",
|
||||
"profiling",
|
||||
"rustc-hash 2.1.1",
|
||||
"simba",
|
||||
"thiserror 2.0.12",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rav1e"
|
||||
version = "0.7.1"
|
||||
@@ -4839,9 +4796,9 @@ dependencies = [
|
||||
name = "space-game"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"avian3d",
|
||||
"bevy",
|
||||
"bevy-inspector-egui",
|
||||
"bevy_rapier3d",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -5698,7 +5655,7 @@ dependencies = [
|
||||
"ndk-sys 0.5.0+25.2.9519653",
|
||||
"objc",
|
||||
"once_cell",
|
||||
"ordered-float",
|
||||
"ordered-float 4.6.0",
|
||||
"parking_lot",
|
||||
"profiling",
|
||||
"range-alloc",
|
||||
|
||||
@@ -4,15 +4,15 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
avian3d = { version = "0.3.0", features = [] }
|
||||
# avian3d = { version = "0.3.0", features = [] }
|
||||
bevy = { version = "0.16.1", features = [] }
|
||||
# bevy_rapier3d = { version = "0.29.0", features = ["simd-stable", "parallel"] }
|
||||
bevy-inspector-egui = "0.31.0"
|
||||
bevy_rapier3d = "0.30.0"
|
||||
|
||||
[features]
|
||||
default = ["dev"]
|
||||
dev = [
|
||||
"avian3d/debug-plugin",
|
||||
"bevy/bevy_dev_tools",
|
||||
"bevy/bevy_ui_debug",
|
||||
"bevy/track_location",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use avian3d::prelude::*;
|
||||
use bevy::prelude::*;
|
||||
use bevy_rapier3d::prelude::*;
|
||||
|
||||
#[derive(Component, Reflect, Clone, Copy)]
|
||||
#[require(
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
pub mod camera;
|
||||
pub mod character_controller;
|
||||
// pub mod character_controller;
|
||||
pub mod tags;
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
use avian3d::prelude::{
|
||||
Collider, ExternalForce, GravityScale, LinearVelocity, LockedAxes, Mass, RayCaster, RayHits, RigidBody,
|
||||
};
|
||||
use bevy::prelude::*;
|
||||
use bevy_rapier3d::prelude::*;
|
||||
|
||||
use crate::components::character_controller::{
|
||||
CharacterController, CharacterMotion, CharacterRotation, GravityDirection,
|
||||
@@ -28,10 +26,9 @@ fn setup_hooks(world: &mut World) {
|
||||
let mut commands = world.commands();
|
||||
let mut entity_commands = commands.entity(ctx.entity);
|
||||
entity_commands.insert((
|
||||
RigidBody::Kinematic,
|
||||
Collider::capsule(radius, height),
|
||||
ExternalForce::ZERO.with_persistence(false),
|
||||
RayCaster::new(Vec3::ZERO, Dir3::NEG_Y).with_max_distance(1.0),
|
||||
RigidBody::KinematicPositionBased,
|
||||
Collider::capsule_y(radius, height),
|
||||
ExternalForce::default(),
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,18 +1,15 @@
|
||||
use avian3d::{PhysicsPlugins, prelude::*};
|
||||
use bevy::{
|
||||
prelude::*,
|
||||
window::{CursorGrabMode, PrimaryWindow},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
components::{
|
||||
camera::{CameraPitch, CameraRoot, FollowCam, MainCamera, Unfocused},
|
||||
character_controller::CharacterController,
|
||||
tags::{Player, Ship},
|
||||
},
|
||||
plugins::*,
|
||||
};
|
||||
// use bevy_rapier3d::prelude::*;
|
||||
use bevy::{
|
||||
prelude::*,
|
||||
window::{CursorGrabMode, PrimaryWindow},
|
||||
};
|
||||
use bevy_rapier3d::prelude::*;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct GamePlugin;
|
||||
@@ -24,14 +21,13 @@ impl Plugin for GamePlugin {
|
||||
// ShipPlugin,
|
||||
TypesPlugin,
|
||||
PlayerPlugin,
|
||||
CharacterControllerPlugin,
|
||||
// CharacterControllerPlugin,
|
||||
));
|
||||
app.add_plugins((
|
||||
PhysicsPlugins::default(),
|
||||
RapierPhysicsPlugin::<NoUserData>::default(),
|
||||
#[cfg(feature = "dev-phys")]
|
||||
PhysicsDebugPlugin::default(),
|
||||
RapierDebugRenderPlugin::default(),
|
||||
));
|
||||
app.insert_resource(Gravity::ZERO);
|
||||
app.add_systems(Startup, (setup_scene).chain());
|
||||
|
||||
app.add_systems(Update, camera_toggle);
|
||||
@@ -43,18 +39,17 @@ fn setup_scene(
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
mut window: Single<&mut Window, With<PrimaryWindow>>,
|
||||
mut gravity: ResMut<Gravity>,
|
||||
) {
|
||||
gravity.0 = Vec3::ZERO;
|
||||
|
||||
// window.cursor_options.visible = false;
|
||||
// window.cursor_options.grab_mode = CursorGrabMode::Locked;
|
||||
|
||||
let player = commands
|
||||
.spawn((
|
||||
Name::new("Player"),
|
||||
CharacterController::default(),
|
||||
Player,
|
||||
Collider::capsule_y(0.5, 0.5),
|
||||
RigidBody::KinematicPositionBased,
|
||||
KinematicCharacterController::default(),
|
||||
Mesh3d(meshes.add(Capsule3d::new(0.5, 1.0))),
|
||||
MeshMaterial3d(materials.add(Color::linear_rgb(1.0, 0.0, 0.2))),
|
||||
Transform::from_translation(Vec3::new(0.0, 10.0, 10.0)),
|
||||
@@ -96,7 +91,7 @@ fn setup_scene(
|
||||
Mesh3d(cube),
|
||||
MeshMaterial3d(material),
|
||||
Transform::default(),
|
||||
RigidBody::Static,
|
||||
RigidBody::Fixed,
|
||||
Collider::cuboid(100.0, 0.1, 100.0),
|
||||
));
|
||||
}
|
||||
@@ -117,8 +112,8 @@ fn spawn_ship(
|
||||
MeshMaterial3d(material.clone()),
|
||||
Name::new("Ship"),
|
||||
Ship,
|
||||
LinearDamping::default(),
|
||||
AngularDamping::default(),
|
||||
Velocity::zero(),
|
||||
Damping::default(),
|
||||
Transform::from_xyz(0.0, 1.0, 0.0),
|
||||
RigidBody::Dynamic,
|
||||
children![
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
mod character_controller;
|
||||
// mod character_controller;
|
||||
mod follow_cam;
|
||||
mod free_cam;
|
||||
mod game;
|
||||
@@ -7,7 +7,7 @@ mod ship;
|
||||
mod ship_cam;
|
||||
mod types;
|
||||
|
||||
pub use character_controller::*;
|
||||
// pub use character_controller::*;
|
||||
pub use follow_cam::*;
|
||||
pub use free_cam::*;
|
||||
pub use game::*;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use bevy::prelude::*;
|
||||
use bevy_rapier3d::prelude::*;
|
||||
|
||||
use crate::components::{character_controller::CharacterMotion, tags::Player};
|
||||
use crate::components::tags::Player;
|
||||
|
||||
pub struct PlayerPlugin;
|
||||
|
||||
@@ -10,7 +11,12 @@ impl Plugin for PlayerPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
fn keyboard_input(key: Res<ButtonInput<KeyCode>>, mut player: Single<&mut CharacterMotion, With<Player>>) {
|
||||
fn keyboard_input(
|
||||
key: Res<ButtonInput<KeyCode>>,
|
||||
mut player: Single<(&mut KinematicCharacterController, &mut Transform), With<Player>>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
let (mut controller, mut transform) = player.into_inner();
|
||||
let mut move_vec = Vec3::ZERO;
|
||||
|
||||
if key.pressed(KeyCode::KeyW) {
|
||||
@@ -25,5 +31,15 @@ fn keyboard_input(key: Res<ButtonInput<KeyCode>>, mut player: Single<&mut Charac
|
||||
move_vec.x = 1.0;
|
||||
}
|
||||
|
||||
player.0 = move_vec;
|
||||
let angle = if key.pressed(KeyCode::KeyQ) {
|
||||
-0.1
|
||||
} else if key.pressed(KeyCode::KeyE) {
|
||||
0.1
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
transform.rotate_z(angle * time.delta_secs());
|
||||
controller.up = transform.up().as_vec3();
|
||||
move_vec += transform.down().as_vec3();
|
||||
controller.translation = Some(move_vec * time.delta_secs());
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use avian3d::prelude::*;
|
||||
use bevy::{input::mouse::MouseMotion, prelude::*};
|
||||
use bevy_rapier3d::prelude::*;
|
||||
|
||||
use crate::{components::tags::Ship, utils::input::get_mouse_delta};
|
||||
|
||||
@@ -14,21 +14,12 @@ impl Plugin for ShipPlugin {
|
||||
}
|
||||
|
||||
fn ship_controls(
|
||||
ship_query: Single<
|
||||
(
|
||||
&Transform,
|
||||
&mut LinearVelocity,
|
||||
&mut AngularVelocity,
|
||||
&mut LinearDamping,
|
||||
&mut AngularDamping,
|
||||
),
|
||||
With<Ship>,
|
||||
>,
|
||||
ship_query: Single<(&Transform, &mut Velocity, &mut Damping), With<Ship>>,
|
||||
key: Res<ButtonInput<KeyCode>>,
|
||||
time: Res<Time>,
|
||||
mouse_motion: EventReader<MouseMotion>,
|
||||
) {
|
||||
let (transform, mut vel, mut ang, mut ldamp, mut adamp) = ship_query.into_inner();
|
||||
let (transform, mut vel, mut damp) = ship_query.into_inner();
|
||||
|
||||
let mut move_vec = Vec3::ZERO;
|
||||
|
||||
@@ -51,11 +42,11 @@ fn ship_controls(
|
||||
}
|
||||
|
||||
if key.pressed(KeyCode::ControlLeft) {
|
||||
ldamp.0 = 0.8;
|
||||
adamp.0 = 0.8;
|
||||
damp.linear_damping = 0.8;
|
||||
damp.angular_damping = 0.8;
|
||||
} else {
|
||||
ldamp.0 = 0.0;
|
||||
adamp.0 = 0.0;
|
||||
damp.linear_damping = 0.0;
|
||||
damp.angular_damping = 0.0;
|
||||
}
|
||||
|
||||
const ROLL_SPEED: f32 = 1.0;
|
||||
@@ -79,11 +70,11 @@ fn ship_controls(
|
||||
let roll = Vec3::NEG_Z * mouse_input.x;
|
||||
let pitch = Vec3::X * mouse_input.y;
|
||||
|
||||
ang.0 += (transform.rotation * (roll + yaw + pitch)) * time.delta_secs();
|
||||
vel.angvel += (transform.rotation * (roll + yaw + pitch)) * time.delta_secs();
|
||||
|
||||
move_vec = transform.rotation * move_vec.normalize_or_zero();
|
||||
|
||||
vel.0 += move_vec * time.delta_secs();
|
||||
vel.linvel += move_vec * time.delta_secs();
|
||||
}
|
||||
|
||||
#[cfg(feature = "dev-viz")]
|
||||
|
||||
Reference in New Issue
Block a user