added a test map, note: switch character to kinematic body

This commit is contained in:
Mikolaj Wojciech Gorski 2024-03-22 19:38:35 +01:00
parent 8640e29b4e
commit f5ca4128e3
8 changed files with 14405 additions and 37 deletions

8
.vscode/launch.json vendored
View file

@ -2,10 +2,16 @@
"version": "0.2.0",
"configurations": [
{
"command": "nix-shell --run 'cargo run'",
"command": "steam-run ./target/debug/ttt-ce",
"name": "Run program",
"request": "launch",
"type": "node-terminal"
},
{
"command": "nix-shell --run 'cargo run'",
"name": "Build and Run program",
"request": "launch",
"type": "node-terminal"
}
],
"compounds": []

253
Cargo.lock generated
View file

@ -706,6 +706,17 @@ dependencies = [
"thiserror",
]
[[package]]
name = "bevy_gltf_components"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7ae31d0e0a111b003f42f44f504ed24153f1341c1989a4e418bdb52da7b6b88"
dependencies = [
"bevy",
"ron",
"serde",
]
[[package]]
name = "bevy_hierarchy"
version = "0.13.1"
@ -899,6 +910,19 @@ dependencies = [
"uuid",
]
[[package]]
name = "bevy_registry_export"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c971f8bd4115568455a0f27b4a808d24f600e30d2de7fbc10461d21170058df"
dependencies = [
"bevy",
"bevy_app",
"bevy_ecs",
"bevy_reflect",
"serde_json",
]
[[package]]
name = "bevy_render"
version = "0.13.1"
@ -1379,6 +1403,18 @@ dependencies = [
"thiserror",
]
[[package]]
name = "calloop-wayland-source"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f0ea9b9476c7fad82841a8dbb380e2eae480c21910feba80725b46931ed8f02"
dependencies = [
"calloop",
"rustix",
"wayland-backend",
"wayland-client",
]
[[package]]
name = "cc"
version = "1.0.90"
@ -2636,6 +2672,15 @@ version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
[[package]]
name = "memmap2"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe751422e4a8caa417e13c3ea66452215d7d63e19e604f4980461212f3ae1322"
dependencies = [
"libc",
]
[[package]]
name = "metal"
version = "0.27.0"
@ -3279,6 +3324,15 @@ version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43d84d1d7a6ac92673717f9f6d1518374ef257669c24ebc5ac25d5033828be58"
[[package]]
name = "quick-xml"
version = "0.31.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1004a344b30a54e2ee58d66a71b32d2db2feb0a31f9a2d302bf0536f15de2a33"
dependencies = [
"memchr",
]
[[package]]
name = "quote"
version = "1.0.35"
@ -3503,12 +3557,31 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "scoped-tls"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294"
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "sctk-adwaita"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82b2eaf3a5b264a521b988b2e73042e742df700c4f962cde845d1541adb46550"
dependencies = [
"ab_glyph",
"log",
"memmap2",
"smithay-client-toolkit",
"tiny-skia",
]
[[package]]
name = "semver"
version = "1.0.22"
@ -3607,6 +3680,31 @@ dependencies = [
"serde",
]
[[package]]
name = "smithay-client-toolkit"
version = "0.18.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "922fd3eeab3bd820d76537ce8f582b1cf951eceb5475c28500c7457d9d17f53a"
dependencies = [
"bitflags 2.5.0",
"calloop",
"calloop-wayland-source",
"cursor-icon",
"libc",
"log",
"memmap2",
"rustix",
"thiserror",
"wayland-backend",
"wayland-client",
"wayland-csd-frame",
"wayland-cursor",
"wayland-protocols",
"wayland-protocols-wlr",
"wayland-scanner",
"xkeysym",
]
[[package]]
name = "smol_str"
version = "0.2.1"
@ -3643,6 +3741,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "strict-num"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731"
[[package]]
name = "svg_fmt"
version = "0.4.2"
@ -3747,6 +3851,31 @@ dependencies = [
"weezl",
]
[[package]]
name = "tiny-skia"
version = "0.11.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83d13394d44dae3207b52a326c0c85a8bf87f1541f23b0d143811088497b09ab"
dependencies = [
"arrayref",
"arrayvec",
"bytemuck",
"cfg-if",
"log",
"tiny-skia-path",
]
[[package]]
name = "tiny-skia-path"
version = "0.11.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c9e7fc0c2e86a30b117d0462aa261b72b7a99b7ebd7deb3a14ceda95c5bdc93"
dependencies = [
"arrayref",
"bytemuck",
"strict-num",
]
[[package]]
name = "tinyvec"
version = "1.6.0"
@ -3874,6 +4003,8 @@ version = "0.1.0"
dependencies = [
"bevy",
"bevy_editor_pls",
"bevy_gltf_components",
"bevy_registry_export",
"bevy_xpbd_3d",
]
@ -4053,6 +4184,114 @@ version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96"
[[package]]
name = "wayland-backend"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d50fa61ce90d76474c87f5fc002828d81b32677340112b4ef08079a9d459a40"
dependencies = [
"cc",
"downcast-rs",
"rustix",
"scoped-tls",
"smallvec",
"wayland-sys",
]
[[package]]
name = "wayland-client"
version = "0.31.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82fb96ee935c2cea6668ccb470fb7771f6215d1691746c2d896b447a00ad3f1f"
dependencies = [
"bitflags 2.5.0",
"rustix",
"wayland-backend",
"wayland-scanner",
]
[[package]]
name = "wayland-csd-frame"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "625c5029dbd43d25e6aa9615e88b829a5cad13b2819c4ae129fdbb7c31ab4c7e"
dependencies = [
"bitflags 2.5.0",
"cursor-icon",
"wayland-backend",
]
[[package]]
name = "wayland-cursor"
version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71ce5fa868dd13d11a0d04c5e2e65726d0897be8de247c0c5a65886e283231ba"
dependencies = [
"rustix",
"wayland-client",
"xcursor",
]
[[package]]
name = "wayland-protocols"
version = "0.31.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f81f365b8b4a97f422ac0e8737c438024b5951734506b0e1d775c73030561f4"
dependencies = [
"bitflags 2.5.0",
"wayland-backend",
"wayland-client",
"wayland-scanner",
]
[[package]]
name = "wayland-protocols-plasma"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23803551115ff9ea9bce586860c5c5a971e360825a0309264102a9495a5ff479"
dependencies = [
"bitflags 2.5.0",
"wayland-backend",
"wayland-client",
"wayland-protocols",
"wayland-scanner",
]
[[package]]
name = "wayland-protocols-wlr"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad1f61b76b6c2d8742e10f9ba5c3737f6530b4c243132c2a2ccc8aa96fe25cd6"
dependencies = [
"bitflags 2.5.0",
"wayland-backend",
"wayland-client",
"wayland-protocols",
"wayland-scanner",
]
[[package]]
name = "wayland-scanner"
version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63b3a62929287001986fb58c789dce9b67604a397c15c611ad9f747300b6c283"
dependencies = [
"proc-macro2",
"quick-xml",
"quote",
]
[[package]]
name = "wayland-sys"
version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15a0c8eaff5216d07f226cb7a549159267f3467b289d9a2e52fd3ef5aae2b7af"
dependencies = [
"dlib",
"log",
"pkg-config",
]
[[package]]
name = "web-sys"
version = "0.3.69"
@ -4535,6 +4774,7 @@ version = "0.29.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d59ad965a635657faf09c8f062badd885748428933dad8e8bdd64064d92e5ca"
dependencies = [
"ahash",
"android-activity",
"atomic-waker",
"bitflags 2.5.0",
@ -4548,6 +4788,7 @@ dependencies = [
"js-sys",
"libc",
"log",
"memmap2",
"ndk",
"ndk-sys",
"objc2 0.4.1",
@ -4557,10 +4798,16 @@ dependencies = [
"raw-window-handle 0.6.0",
"redox_syscall 0.3.5",
"rustix",
"sctk-adwaita",
"smithay-client-toolkit",
"smol_str",
"unicode-segmentation",
"wasm-bindgen",
"wasm-bindgen-futures",
"wayland-backend",
"wayland-client",
"wayland-protocols",
"wayland-protocols-plasma",
"web-sys",
"web-time",
"windows-sys 0.48.0",
@ -4610,6 +4857,12 @@ version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e63e71c4b8bd9ffec2c963173a4dc4cbde9ee96961d4fcb4429db9929b606c34"
[[package]]
name = "xcursor"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a0ccd7b4a5345edfcd0c3535718a4e9ff7798ffc536bb5b5a0e26ff84732911"
[[package]]
name = "xi-unicode"
version = "0.3.0"

View file

@ -15,6 +15,8 @@ opt-level = 3
[dependencies]
bevy = { version = "0.13.1"}
bevy = { version = "0.13.1", features = ["wayland"]}
bevy_editor_pls = "0.8.1"
bevy_gltf_components = "0.5.1"
bevy_registry_export = "0.3.1"
bevy_xpbd_3d = {git = "https://git.opencodebox.com/MikolajG/bevy_xpbd", branch = "reflect-serialize"}

12745
assets/registry.json Normal file

File diff suppressed because it is too large Load diff

BIN
assets/test_map.bin Normal file

Binary file not shown.

1318
assets/test_map.gltf Normal file

File diff suppressed because it is too large Load diff

View file

@ -53,9 +53,9 @@ pub struct CharacterController{
impl Default for CharacterController {
fn default() -> Self {
CharacterController{
movement_acceleration: 30.0,
movement_acceleration: 20.0,
movement_dampening_factor: 0.95,
jump_impulse: 7.0,
jump_impulse: 4.0,
max_slope_angle: (30.0 as Scalar).to_radians(),
}
}
@ -68,6 +68,7 @@ pub struct CharacterControllerBundle{
collider: Collider,
ground_caster: ShapeCaster,
locked_axis: LockedAxes,
restitution: Restitution,
}
impl CharacterControllerBundle {
@ -85,8 +86,9 @@ impl CharacterControllerBundle {
Vector::ZERO,
Quaternion::default(),
Direction3d::NEG_Y,
),
).with_max_time_of_impact(0.2),
locked_axis: LockedAxes::ROTATION_LOCKED,
restitution: Restitution::new(0.0),
}
}
}
@ -96,30 +98,24 @@ impl Default for CharacterControllerBundle {
let collider = Collider::capsule(1.0, 0.5);
let mut caster_shape = collider.clone();
caster_shape.set_scale(Vector::ONE * 0.99, 10);
CharacterControllerBundle{
character_controller: CharacterController::default(),
rigid_body: RigidBody::Dynamic,
collider: collider,
ground_caster: ShapeCaster::new(
caster_shape,
Vector::ZERO,
Quaternion::default(),
Direction3d::NEG_Y,
),
locked_axis: LockedAxes::ROTATION_LOCKED,
}
CharacterControllerBundle::new(collider, CharacterController::default())
}
}
pub fn update_grounded(mut commands: Commands, mut query: Query<(Entity, &CharacterController, &Rotation, &ShapeHits)>){
for (entity, character_controller, rotation, hits) in &mut query {
let is_grounded = hits.iter().any(|hit| {
rotation.rotate(-hit.normal2).angle_between(Vector::Y).abs() <= character_controller.max_slope_angle
let angle = rotation.rotate(-hit.normal2).angle_between(Vector::Y).abs();
println!("slope angle: {} / {}", angle, character_controller.max_slope_angle);
angle <= character_controller.max_slope_angle
});
//println!("{}", is_grounded);
println!("is grounded: {}", is_grounded);
if is_grounded {
commands.entity(entity).insert(Grounded);
@ -150,22 +146,36 @@ pub fn keyboard_input(mut movement_event_writer: EventWriter<MovementAction>, ke
}
pub fn move_character(time: Res<Time>, mut movement_event_reader: EventReader<MovementAction>,
mut qeury: Query<(&mut LinearVelocity, &CharacterController)>
mut qeury: Query<(&mut LinearVelocity, &CharacterController, Has<Grounded>, &Rotation, &ShapeHits)>
){
let delta_time = time.delta_seconds_f64().adjust_precision();
for event in movement_event_reader.read() {
for (mut velocity, character_controller) in &mut qeury{
for (mut velocity, character_controller, is_grounded, rotation, shape_hits) in &mut qeury{
match event{
MovementAction::Move(direction) => {
if is_grounded {
velocity.x += direction.x * character_controller.movement_acceleration * delta_time;
velocity.z -= direction.y * character_controller.movement_acceleration * delta_time;
}
else {
shape_hits.iter().any(|hit| {
let angle = rotation.rotate(-hit.normal2).angle_between(Vector::Y).abs();
velocity.x += direction.x * (character_controller.movement_acceleration - (character_controller.movement_acceleration / angle)) * delta_time;
velocity.z -= direction.y * (character_controller.movement_acceleration - (character_controller.movement_acceleration / angle)) * delta_time;
angle <= character_controller.max_slope_angle
});
}
},
MovementAction::Jump => {
if is_grounded {
velocity.y = character_controller.jump_impulse;
}
}
}
}
}

View file

@ -20,16 +20,25 @@ use bevy_xpbd_3d::prelude::*;
use bevy_editor_pls::prelude::*;
mod charcacter_controller;
use charcacter_controller::*;
use bevy::diagnostic::FrameTimeDiagnosticsPlugin;
use bevy_gltf_components::ComponentsFromGltfPlugin;
//use bevy_registry_export::ExportRegistryPlugin;
fn main() {
App::new()
.add_plugins((DefaultPlugins, PhysicsPlugins::default(), EditorPlugin::default(), CharacterControllerPlugin))
let mut app = App::new();
app
.add_plugins((DefaultPlugins, PhysicsPlugins::default(), EditorPlugin::default(), CharacterControllerPlugin, ComponentsFromGltfPlugin::default()))
.add_systems(Startup, setup)
.add_plugins(FrameTimeDiagnosticsPlugin::default())
.run();
.add_systems(Update, spawn_map);
app.world.init_resource::<IsMapSpawned>();
app.run();
}
#[derive(Resource, Default, Debug)]
struct IsMapSpawned(bool);
/// set up a simple 3D scene
fn setup(
mut commands: Commands,
@ -37,12 +46,13 @@ fn setup(
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// plane
commands.spawn((PbrBundle {
mesh: meshes.add(Plane3d::default().mesh().size(50.0, 50.0)),
material: materials.add(Color::WHITE),
transform: Transform::from_xyz(0., 0., 0.),
..default()
}, Collider::cuboid(50.0, 0.1, 50.0), RigidBody::Static));
// commands.spawn((PbrBundle {
// mesh: meshes.add(Plane3d::default().mesh().size(50.0, 50.0)),
// material: materials.add(Color::WHITE),
// transform: Transform::from_xyz(0., 0., 0.),
// ..default()
// }, Collider::cuboid(50.0, 0.1, 50.0), RigidBody::Static));
// cube
// commands.spawn((RigidBody::Dynamic, PbrBundle {
// mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)),
@ -72,3 +82,27 @@ fn setup(
..default()
});
}
fn spawn_map(
asset_server: Res<AssetServer>,
mut is_map_spawned: ResMut<IsMapSpawned>,
mut commands: Commands,
){
if is_map_spawned.0 {return;}
let scene = asset_server.load("test_map.gltf#Scene0");
commands.spawn((
SceneBundle {
scene: scene.clone(),
transform: Transform::from_xyz(0.0, 0.0, 0.0),
..default()
},
AsyncSceneCollider::new(Some(ComputedCollider::TriMesh)),
RigidBody::Static,
));
println!("map spawned!");
is_map_spawned.0 = true
}