This commit is contained in:
2025-06-23 20:08:05 -04:00
parent 9ac58fe9b9
commit 67a8eb09cd
5 changed files with 102 additions and 25 deletions

View File

@@ -7,6 +7,7 @@ use bevy::prelude::*;
RigidBody, RigidBody,
Transform, Transform,
ExternalForce, ExternalForce,
LinearVelocity,
GravityScale, GravityScale,
CharacterMotion, CharacterMotion,
CharacterRotation CharacterRotation
@@ -16,6 +17,7 @@ pub struct CharacterController {
pub max_slope: f32, pub max_slope: f32,
height: f32, height: f32,
radius: f32, radius: f32,
pub is_grounded: bool,
} }
impl Default for CharacterController { impl Default for CharacterController {
@@ -25,6 +27,7 @@ impl Default for CharacterController {
radius: 0.5, radius: 0.5,
max_slope: 0.5, max_slope: 0.5,
step_height: 0.25, step_height: 0.25,
is_grounded: false,
}; };
} }
} }

View File

@@ -1,14 +1,19 @@
use avian3d::prelude::{Collider, ExternalForce, GravityScale, LockedAxes, Mass}; use avian3d::prelude::{
Collider, ExternalForce, GravityScale, LinearVelocity, LockedAxes, Mass, RayCaster, RayHits, RigidBody,
};
use bevy::prelude::*; use bevy::prelude::*;
use crate::components::character_controller::{CharacterController, GravityDirection}; use crate::components::character_controller::{
CharacterController, CharacterMotion, CharacterRotation, GravityDirection,
};
pub struct CharacterControllerPlugin; pub struct CharacterControllerPlugin;
impl Plugin for CharacterControllerPlugin { impl Plugin for CharacterControllerPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(PreStartup, setup_hooks); app.add_systems(PreStartup, setup_hooks);
app.add_systems(Update, apply_gravity); app.add_systems(Update, is_grounded);
app.add_systems(Update, (apply_gravity, apply_forces, apply_motion).chain());
} }
} }
@@ -23,24 +28,60 @@ fn setup_hooks(world: &mut World) {
let mut commands = world.commands(); let mut commands = world.commands();
let mut entity_commands = commands.entity(ctx.entity); let mut entity_commands = commands.entity(ctx.entity);
entity_commands.insert(( entity_commands.insert((
LockedAxes::ROTATION_LOCKED, RigidBody::Kinematic,
Collider::capsule(radius, height), Collider::capsule(radius, height),
ExternalForce::ZERO.with_persistence(false), ExternalForce::ZERO.with_persistence(false),
RayCaster::new(Vec3::ZERO, Dir3::NEG_Y).with_max_distance(1.0),
)); ));
}); });
} }
fn is_grounded(controllers: Query<(&mut CharacterController, &RayHits)>) {
for (mut controller, hits) in controllers {
controller.is_grounded = !hits.is_empty();
println!("{}", controller.is_grounded);
}
}
fn apply_gravity( fn apply_gravity(
controllers: Query< controllers: Query<(
(&mut ExternalForce, &GravityScale, &GravityDirection, Option<&Mass>), &mut ExternalForce,
With<CharacterController>, &GravityScale,
>, &GravityDirection,
Option<&Mass>,
&CharacterController,
)>,
time: Res<Time>,
) { ) {
for (mut force, grav_scale, grav_dir, mass) in controllers { for (mut force, grav_scale, grav_dir, mass, controller) in controllers {
if grav_scale.0 == 0.0 { if controller.is_grounded {
continue; continue;
} }
let m = if let Some(mass) = mass { mass.0 } else { 1.0 }; let m = if let Some(mass) = mass { mass.0 } else { 1.0 };
force.apply_force(grav_dir.0 * grav_scale.0 * 9.3 * m); force.apply_force(grav_dir.0 * grav_scale.0 * 9.3 * m * time.delta_secs());
}
}
fn apply_motion(
controllers: Query<
(&LinearVelocity, &CharacterMotion, &CharacterRotation, &mut Transform),
With<CharacterController>,
>,
time: Res<Time>,
) {
for (lin_vel, motion, _rotation, mut transform) in controllers {
let move_vec = transform.rotation * motion.0;
let vel = (lin_vel.0 + move_vec) * time.delta_secs();
transform.translation += vel;
}
}
fn apply_forces(
controllers: Query<(&mut LinearVelocity, &mut ExternalForce), With<CharacterController>>,
time: Res<Time>,
) {
for (mut vel, mut force) in controllers {
vel.0 += force.force() * time.delta_secs();
force.clear();
} }
} }

View File

@@ -19,14 +19,20 @@ pub struct GamePlugin;
impl Plugin for GamePlugin { impl Plugin for GamePlugin {
fn build(&self, app: &mut bevy::app::App) { fn build(&self, app: &mut bevy::app::App) {
app.add_plugins((FollowCamPlugin, ShipPlugin, TypesPlugin, CharacterControllerPlugin)); app.add_plugins((
FollowCamPlugin,
// ShipPlugin,
TypesPlugin,
PlayerPlugin,
CharacterControllerPlugin,
));
app.add_plugins(( app.add_plugins((
PhysicsPlugins::default(), PhysicsPlugins::default(),
#[cfg(feature = "dev-phys")] #[cfg(feature = "dev-phys")]
PhysicsDebugPlugin::default(), PhysicsDebugPlugin::default(),
)); ));
app.insert_resource(Gravity::ZERO); app.insert_resource(Gravity::ZERO);
app.add_systems(Startup, (setup_scene, spawn_ship).chain()); app.add_systems(Startup, (setup_scene).chain());
app.add_systems(Update, camera_toggle); app.add_systems(Update, camera_toggle);
} }
@@ -41,23 +47,30 @@ fn setup_scene(
) { ) {
gravity.0 = Vec3::ZERO; gravity.0 = Vec3::ZERO;
window.cursor_options.visible = false; // window.cursor_options.visible = false;
window.cursor_options.grab_mode = CursorGrabMode::Locked; // window.cursor_options.grab_mode = CursorGrabMode::Locked;
commands.spawn(( let player = commands
Name::new("Player"), .spawn((
CharacterController::default(), Name::new("Player"),
Player, CharacterController::default(),
Mesh3d(meshes.add(Capsule3d::new(0.5, 1.0))), Player,
MeshMaterial3d(materials.add(Color::linear_rgb(1.0, 0.0, 0.2))), Mesh3d(meshes.add(Capsule3d::new(0.5, 1.0))),
Transform::from_translation(Vec3::new(0.0, 10.0, 0.0)), MeshMaterial3d(materials.add(Color::linear_rgb(1.0, 0.0, 0.2))),
)); Transform::from_translation(Vec3::new(0.0, 10.0, 10.0)),
))
.id();
commands.spawn(( commands.spawn((
Name::new("Camera Root"), Name::new("Camera Root"),
Transform::from_xyz(0.0, 1.3, 0.0), Transform::from_xyz(0.0, 1.3, 0.0),
CameraRoot, CameraRoot,
Visibility::default(), Visibility::default(),
FollowCam {
distance: 20.,
height: 10.,
target: player,
},
children![( children![(
Camera3d::default(), Camera3d::default(),
CameraPitch::default(), CameraPitch::default(),

View File

@@ -1,9 +1,29 @@
use bevy::prelude::*; use bevy::prelude::*;
use crate::components::{character_controller::CharacterMotion, tags::Player};
pub struct PlayerPlugin; pub struct PlayerPlugin;
impl Plugin for PlayerPlugin { impl Plugin for PlayerPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
todo!() app.add_systems(PreUpdate, keyboard_input);
} }
} }
fn keyboard_input(key: Res<ButtonInput<KeyCode>>, mut player: Single<&mut CharacterMotion, With<Player>>) {
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;
}
player.0 = move_vec;
}

View File

@@ -7,7 +7,7 @@ pub struct ShipPlugin;
impl Plugin for ShipPlugin { impl Plugin for ShipPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
// app.add_systems(Update, ship_controls); app.add_systems(Update, ship_controls);
#[cfg(feature = "dev-viz")] #[cfg(feature = "dev-viz")]
app.add_systems(Update, ship_debug); app.add_systems(Update, ship_debug);
} }