physis + ship movement

This commit is contained in:
2025-05-13 21:32:14 -04:00
parent b5cbf8e386
commit a8f0793865
3 changed files with 354 additions and 10 deletions

View File

@@ -1,5 +1,6 @@
use std::f32::consts::FRAC_PI_2;
use avian3d::{PhysicsPlugins, prelude::*};
use bevy::{
input::mouse::MouseMotion,
prelude::*,
@@ -20,13 +21,18 @@ pub struct CameraPitch(pub f32);
#[derive(Component)]
pub struct Unfocused;
#[derive(Component)]
pub struct Ship;
impl Plugin for GamePlugin {
fn build(&self, app: &mut bevy::app::App) {
// app.add_plugins(RapierPhysicsPlugin::<NoUserData>::default());
app.add_plugins((PhysicsPlugins::default(), PhysicsDebugPlugin::default()));
app.insert_resource(Gravity::ZERO);
app.add_systems(Startup, (setup_scene, spawn_ship));
app.add_systems(Update, ((camera_yaw, camera_pitch).chain(), camera_toggle, fly_camera));
app.add_systems(Update, ship_controls);
}
}
@@ -65,7 +71,13 @@ fn setup_scene(
let cube = meshes.add(Cuboid::new(100.0, 0.1, 100.0));
let material = materials.add(Color::WHITE);
commands.spawn((Mesh3d(cube), MeshMaterial3d(material), Transform::default()));
commands.spawn((
Mesh3d(cube),
MeshMaterial3d(material),
Transform::default(),
RigidBody::Static,
Collider::cuboid(100.0, 0.1, 100.0),
));
}
fn spawn_ship(
@@ -81,35 +93,44 @@ fn spawn_ship(
Mesh3d(meshes.add(Cuboid::new(3.0, 0.1, 6.0))),
MeshMaterial3d(material.clone()),
Name::new("Ship"),
Ship,
LinearDamping::default(),
AngularDamping::default(),
Transform::from_xyz(0.0, 1.0, 0.0),
RigidBody::Dynamic,
children![
(
Name::new("Back Wall"),
Mesh3d(meshes.add(Cuboid::new(3.0, 2.0, 0.1))),
Collider::cuboid(3.0, 2.0, 0.1),
MeshMaterial3d(material.clone()),
Transform::from_xyz(0.0, 1.0, 6.0 / 2.0),
),
(
Name::new("Front Window"),
Mesh3d(meshes.add(Cuboid::new(3.0, 2.0, 0.1))),
Collider::cuboid(3.0, 2.0, 0.1),
MeshMaterial3d(window_material),
Transform::from_xyz(0.0, 1.0, -6.0 / 2.0),
),
(
Name::new("Right Wall"),
Mesh3d(meshes.add(Cuboid::new(0.1, 2.0, 6.0))),
Collider::cuboid(0.1, 2.0, 6.0),
MeshMaterial3d(material.clone()),
Transform::from_xyz(3.0 / 2.0, 1.0, 0.0),
),
(
Name::new("Left Wall"),
Mesh3d(meshes.add(Cuboid::new(0.1, 2.0, 6.0))),
Collider::cuboid(0.1, 2.0, 6.0),
MeshMaterial3d(material.clone()),
Transform::from_xyz(-3.0 / 2.0, 1.0, 0.0),
),
(
Name::new("Roof"),
Mesh3d(meshes.add(Cuboid::new(3.0, 0.1, 6.0))),
Collider::cuboid(3.0, 0.1, 6.0),
MeshMaterial3d(material.clone()),
Transform::from_xyz(0.0, 2.0, 0.0),
)
@@ -119,11 +140,10 @@ fn spawn_ship(
pub fn camera_toggle(
key: Res<ButtonInput<KeyCode>>,
window_query: Single<&mut Window, With<PrimaryWindow>>,
mut window: Single<&mut Window, With<PrimaryWindow>>,
camera: Single<Entity, With<CameraRoot>>,
mut commands: Commands,
) {
let mut window = window_query.into_inner();
if key.just_pressed(KeyCode::Escape) {
if window.cursor_options.visible {
commands.entity(camera.into_inner()).remove::<Unfocused>();
@@ -197,3 +217,56 @@ fn get_mouse_delta(mut mouse_motion: EventReader<MouseMotion>) -> Vec2 {
}
return delta;
}
fn ship_controls(
ship_query: Single<
(
&Transform,
&mut LinearVelocity,
&mut AngularVelocity,
&mut LinearDamping,
&mut AngularDamping,
),
With<Ship>,
>,
window: Single<&Window, With<PrimaryWindow>>,
key: Res<ButtonInput<KeyCode>>,
time: Res<Time>,
) {
if !window.cursor_options.visible {
return;
}
let (transform, mut vel, mut ang, mut ldamp, mut adamp) = ship_query.into_inner();
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;
}
if key.pressed(KeyCode::Space) {
move_vec.y = 1.0;
} else if key.pressed(KeyCode::ShiftLeft) {
move_vec.y = -1.0;
}
if key.pressed(KeyCode::ControlLeft) {
ldamp.0 = 0.8;
adamp.0 = 0.8;
} else {
ldamp.0 = 0.0;
adamp.0 = 0.0;
}
move_vec = transform.rotation * move_vec.normalize_or_zero();
vel.0 += move_vec * time.delta_secs();
}